text
stringlengths
1
474
a column of text, a star icon, and a number.
Its first child, the column, contains two lines of text.
That first column might need more space.Row 2, the Button section, has three children: each child contains
a column which then contains an icon and text.After diagramming the layout, consider how you would code it.Would you write all the code in one class?
Or, would you create one class for each part of the layout?To follow Flutter best practices, create one class, or Widget,
to contain each part of your layout.
When Flutter needs to re-render part of a UI,
it updates the smallest part that changes.
This is why Flutter makes “everything a widget”.
If only the text changes in a Text widget, Flutter redraws only that text.
Flutter changes the least amount of the UI possible in response to user input.For this tutorial, write each element you have identified as its own widget.<topic_end>
<topic_start>
Create the app base code
In this section, shell out the basic Flutter app code to start your app.Set up your Flutter environment.Create a new Flutter app.Replace the contents of lib/main.dart with the following code.
This app uses a parameter for the app title and the title shown
on the app’s appBar. This decision simplifies the code.
<code_start>import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
const String appTitle = 'Flutter layout demo';
return MaterialApp(
title: appTitle,
home: Scaffold(
appBar: AppBar(
title: const Text(appTitle),
),
body: const Center(
child: Text('Hello World'),
),
),
);
}
}<code_end>
<topic_end>
<topic_start>
Add the Title section
In this section, create a TitleSection widget that resembles
the following layout.<topic_end>
<topic_start>
Add the TitleSection Widget
Add the following code after the MyApp class.
<code_start>class TitleSection extends StatelessWidget {
const TitleSection({
super.key,
required this.name,
required this.location,
});
final String name;
final String location;
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(32),
child: Row(
children: [
Expanded(
/*1*/
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
/*2*/
Padding(
padding: const EdgeInsets.only(bottom: 8),
child: Text(
name,
style: const TextStyle(
fontWeight: FontWeight.bold,
),
),
),
Text(
location,
style: TextStyle(
color: Colors.grey[500],
),
),
],
),
),
/*3*/
Icon(
Icons.star,
color: Colors.red[500],
),
const Text('41'),
],
),
);
}
}<code_end>
<topic_end>
<topic_start>
Change the app body to a scrolling view
In the body property, replace the Center widget with a
SingleChildScrollView widget.
Within the SingleChildScrollView widget, replace the Text widget with a
Column widget.These code updates change the app in the following ways.<topic_end>