text stringlengths 1 474 |
|---|
Widget build(BuildContext context) { |
return Container( |
decoration: const BoxDecoration(color: Colors.white), |
child: const Center( |
child: Text( |
'Hello World', |
textDirection: TextDirection.ltr, |
style: TextStyle( |
fontSize: 32, |
color: Colors.black87, |
), |
), |
), |
); |
} |
}<code_end> |
By default, a non-Material app doesn’t include an AppBar, title, |
or background color. If you want these features in a non-Material app, |
you have to build them yourself. This app changes the background |
color to white and the text to dark grey to mimic a Material app.That’s it! When you run the app, you should see Hello World.App source code:<topic_end> |
<topic_start> |
Lay out multiple widgets vertically and horizontally |
One of the most common layout patterns is to arrange |
widgets vertically or horizontally. You can use a |
Row widget to arrange widgets horizontally, |
and a Column widget to arrange widgets vertically.<topic_end> |
<topic_start>What's the point? |
To create a row or column in Flutter, you add a list of children |
widgets to a Row or Column widget. In turn, |
each child can itself be a row or column, and so on. |
The following example shows how it is possible to nest rows or |
columns inside of rows or columns.This layout is organized as a Row. The row contains two children: |
a column on the left, and an image on the right:The left column’s widget tree nests rows and columns.You’ll implement some of Pavlova’s layout code in |
Nesting rows and columns.info Note |
Row and Column are basic primitive widgets for horizontal |
and vertical layouts—these low-level widgets allow for maximum |
customization. Flutter also offers specialized, higher level widgets |
that might be sufficient for your needs. For example, |
instead of Row you might prefer ListTile, |
an easy-to-use widget with properties for leading and trailing icons, |
and up to 3 lines of text. Instead of Column, you might prefer |
ListView, a column-like layout that automatically scrolls |
if its content is too long to fit the available space. |
For more information, see Common layout widgets.<topic_end> |
<topic_start> |
Aligning widgets |
You control how a row or column aligns its children using the |
mainAxisAlignment and crossAxisAlignment properties. |
For a row, the main axis runs horizontally and the cross axis runs |
vertically. For a column, the main axis runs vertically and the cross |
axis runs horizontally.The MainAxisAlignment and CrossAxisAlignment |
enums offer a variety of constants for controlling alignment.info Note |
When you add images to your project, |
you need to update the pubspec.yaml file to access |
them—this example uses Image.asset to display |
the images. For more information, see this example’s |
pubspec.yaml file or Adding assets and images. |
You don’t need to do this if you’re referencing online |
images using Image.network.In the following example, each of the 3 images is 100 pixels wide. |
The render box (in this case, the entire screen) |
is more than 300 pixels wide, so setting the main axis |
alignment to spaceEvenly divides the free horizontal |
space evenly between, before, and after each image.App source: row_columnColumns work the same way as rows. The following example shows a column |
of 3 images, each is 100 pixels high. The height of the render box |
(in this case, the entire screen) is more than 300 pixels, so |
setting the main axis alignment to spaceEvenly divides the free vertical |
space evenly between, above, and below each image.App source: row_column<topic_end> |
<topic_start> |
Sizing widgets |
When a layout is too large to fit a device, a yellow |
and black striped pattern appears along the affected edge. |
Here is an example of a row that is too wide:Widgets can be sized to fit within a row or column by using the |
Expanded widget. To fix the previous example where the |
row of images is too wide for its render box, |
wrap each image with an Expanded widget.App source: sizingPerhaps you want a widget to occupy twice as much space as its |
siblings. For this, use the Expanded widget flex property, |
an integer that determines the flex factor for a widget. |
The default flex factor is 1. The following code sets |
the flex factor of the middle image to 2:App source: sizing<topic_end> |
<topic_start> |
Packing widgets |
By default, a row or column occupies as much space along its main axis |
as possible, but if you want to pack the children closely together, |
set its mainAxisSize to MainAxisSize.min. The following example |
uses this property to pack the star icons together.App source: pavlova<topic_end> |
<topic_start> |
Nesting rows and columns |
The layout framework allows you to nest rows and columns |
inside of rows and columns as deeply as you need. |
Let’s look at the code for the outlined |
section of the following layout:The outlined section is implemented as two rows. The ratings row contains |
five stars and the number of reviews. The icons row contains three |
columns of icons and text.The widget tree for the ratings row:The ratings variable creates a row containing a smaller row |
of 5 star icons, and text: |
<code_start>final stars = Row( |
mainAxisSize: MainAxisSize.min, |
children: [ |
Icon(Icons.star, color: Colors.green[500]), |
Icon(Icons.star, color: Colors.green[500]), |
Icon(Icons.star, color: Colors.green[500]), |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.