text
stringlengths
1
372
<topic_start>
build a flutter layout
<topic_end>
<topic_start>
what you’ll learn
this tutorial explains how to design and build layouts in flutter.
if you use the example code provided, you can build the following app.
photo by dino reichmuth on unsplash.
text by switzerland tourism.
to get a better overview of the layout mechanism, start with
flutter’s approach to layout.
<topic_end>
<topic_start>
diagram the layout
in this section, consider what type of user experience you want for
your app users.
consider how to position the components of your user interface.
a layout consists of the total end result of these positionings.
consider planning your layout to speed up your coding.
using visual cues to know where something goes on screen can be a great help.
use whichever method you prefer, like an interface design tool or a pencil
and a sheet of paper. figure out where you want to place elements on your
screen before writing code. it’s the programming version of the adage:
“measure twice, cut once.”
ask these questions to break the layout down to its basic elements.
identify the larger elements. in this example, you arrange the image, title,
buttons, and description into a column.
diagram each row.
row 1, the title section, has three children:
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: [