Based on the photo and code below, how do I make the ingredient list align at the center along with the ingredients title? I tried using custom width for both of the SizedBox for the ingredients, but it doesn’t work well for different ingredients. How do I take the maximum length for the current ingredient to set the width? or any other solutions are welcome. 🙏 🤗
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:meals/models/meal.dart';
class MealDetailsScreen extends StatelessWidget {
const MealDetailsScreen({
super.key,
required this.meal,
});
final Meal meal;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(meal.title),
),
body: SingleChildScrollView(
child: Container(
margin: const EdgeInsets.symmetric(
vertical: 15,
horizontal: 15,
),
decoration: BoxDecoration(
color: Theme.of(context).colorScheme.onSurface, // Background color of the container
borderRadius: BorderRadius.circular(30), // Rounded corners
),
child: Column(
children: [
Padding(
padding: const EdgeInsets.all(16.0),
child: ClipRRect(
borderRadius: BorderRadius.circular(20),
child: Image.network(
meal.imageUrl,
height: 300,
width: double.infinity,
fit: BoxFit.cover,
),
),
),
const SizedBox(height: 14),
Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text(
'Ingredients',
style: Theme.of(context).textTheme.titleLarge!.copyWith(
color: Theme.of(context).colorScheme.onPrimary,
fontFamily: GoogleFonts.borel().fontFamily,
fontWeight: FontWeight.bold,
fontSize: 24,
),
textAlign: TextAlign.center,
),
const SizedBox(height: 14),
Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
for (final ingredient in meal.ingredients)
Padding(
padding: const EdgeInsets.only(bottom: 6),
child: Row(
children: [
SizedBox(
// width: MediaQuery.of(context).size.width * 0.2,
width: 50,
child: Text(
ingredient.contains(' ') ? ingredient.split(' ').first : '',
style: Theme.of(context).textTheme.bodyMedium!.copyWith(
color: Theme.of(context).colorScheme.surface,
),
textAlign: TextAlign.right,
),
),
const SizedBox(width: 8),
// Ingredient name
SizedBox(
width: MediaQuery.of(context).size.width * 0.6,
child: Text(
ingredient.contains(' ') ? ingredient.split(' ').skip(1).join(' ') : ingredient,
style: Theme.of(context).textTheme.bodyMedium!.copyWith(
color: Theme.of(context).colorScheme.surface,
),
textAlign: TextAlign.left,
),
),
],
),
),
],
),
],
),
// Steps Section
Padding(
padding: const EdgeInsets.all(20),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text(
'Steps',
textAlign: TextAlign.center,
style: Theme.of(context).textTheme.titleLarge!.copyWith(
color: Theme.of(context).colorScheme.onPrimary,
fontFamily: GoogleFonts.borel().fontFamily,
fontWeight: FontWeight.bold,
fontSize: 24,
),
),
const SizedBox(height: 10),
Align(
alignment: Alignment.center,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
for (final entry in meal.steps.asMap().entries)
Padding(
padding: const EdgeInsets.only(bottom: 6),
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// Step number
Text(
'${entry.key + 1}. ', // Display step number
style: Theme.of(context).textTheme.bodyMedium!.copyWith(
color: Theme.of(context).colorScheme.surface,
fontWeight: FontWeight.bold,
),
),
const SizedBox(width: 6),
// Step description
Expanded(
child: Text(
entry.value,
textAlign: TextAlign.start,
style: Theme.of(context).textTheme.bodyMedium!.copyWith(
color: Theme.of(context).colorScheme.surface,
),
),
),
],
),
),
],
),
),
// ),
],
),
),
],
),
),
)
);
}
}