text
stringlengths
1
474
}<code_end>
<topic_end>
<topic_start>
Layouts
<topic_end>
<topic_start>
How do I use widgets to define layout properties?
In React Native, most of the layout can be done with the props
that are passed to a specific component.
For example, you could use the style prop on the View component
in order to specify the flexbox properties.
To arrange your components in a column, you would specify a prop such as:
flexDirection: 'column'.In Flutter, the layout is primarily defined by widgets
specifically designed to provide layout,
combined with control widgets and their style properties.For example, the Column and Row widgets
take an array of children and align them
vertically and horizontally respectively.
A Container widget takes a combination of
layout and styling properties, and a
Center widget centers its child widgets.
<code_start>@override
Widget build(BuildContext context) {
return Center(
child: Column(
children: <Widget>[
Container(
color: Colors.red,
width: 100,
height: 100,
),
Container(
color: Colors.blue,
width: 100,
height: 100,
),
Container(
color: Colors.green,
width: 100,
height: 100,
),
],
),
);<code_end>
Flutter provides a variety of layout widgets in its core widget library.
For example, Padding, Align, and Stack.For a complete list, see Layout Widgets.<topic_end>
<topic_start>
How do I layer widgets?
In React Native, components can be layered using absolute positioning.Flutter uses the Stack
widget to arrange children widgets in layers.
The widgets can entirely or partially overlap the base widget.The Stack widget positions its children relative to the edges of its box.
This class is useful if you simply want to overlap several children widgets.
<code_start>@override
Widget build(BuildContext context) {
return Stack(
alignment: const Alignment(0.6, 0.6),
children: <Widget>[
const CircleAvatar(
backgroundImage: NetworkImage(
'https://avatars3.githubusercontent.com/u/14101776?v=4',
),
),
Container(
color: Colors.black45,
child: const Text('Flutter'),
),
],
);<code_end>
The previous example uses Stack to overlay a Container
(that displays its Text on a translucent black background)
on top of a CircleAvatar.
The Stack offsets the text using the alignment property
and Alignment coordinates.For more information, see the Stack class documentation.<topic_end>
<topic_start>
Styling
<topic_end>
<topic_start>
How do I style my components?
In React Native, inline styling and stylesheets.create
are used to style components.In Flutter, a Text widget can take a TextStyle class
for its style property. If you want to use the same text
style in multiple places, you can create a
TextStyle class and use it for multiple Text widgets.
<code_start>const TextStyle textStyle = TextStyle(
color: Colors.cyan,
fontSize: 32,
fontWeight: FontWeight.w600,
);
return const Center(
child: Column(
children: <Widget>[
Text('Sample text', style: textStyle),
Padding(
padding: EdgeInsets.all(20),
child: Icon(
Icons.lightbulb_outline,
size: 48,
color: Colors.redAccent,
),
),
],