text
stringlengths
1
372
<code_end>
be sure to have a uses-material-design: true entry in the flutter
section of your pubspec.yaml file. it allows you to use the predefined
set of material icons. it’s generally a good idea to include this line
if you are using the materials library.
many material design widgets need to be inside of a MaterialApp
to display properly, in order to inherit theme data.
therefore, run the application with a MaterialApp.
the MyAppBar widget creates a container with a height of 56
device-independent pixels with an internal padding of 8 pixels,
both on the left and the right. inside the container,
MyAppBar uses a row layout to organize its children.
the middle child, the title widget, is marked as expanded,
which means it expands to fill any remaining available space
that hasn’t been consumed by the other children.
you can have multiple expanded children and determine the
ratio in which they consume the available space using the
flex argument to expanded.
the MyScaffold widget organizes its children in a vertical column.
at the top of the column it places an instance of MyAppBar,
passing the app bar a text widget to use as its title.
passing widgets as arguments to other widgets is a powerful technique
that lets you create generic widgets that can be reused in a wide
variety of ways. finally, MyScaffold uses an
expanded to fill the remaining space with its body,
which consists of a centered message.
for more information, check out layouts.
<topic_end>
<topic_start>
using material components
flutter provides a number of widgets that help you build apps
that follow material design. a material app starts with the
MaterialApp widget, which builds a number of useful widgets
at the root of your app, including a navigator,
which manages a stack of widgets identified by strings,
also known as “routes”. the navigator lets you transition smoothly
between screens of your application. using the MaterialApp
widget is entirely optional but a good practice.
<code_start>
import 'package:flutter/material.dart';
void main() {
runApp(
const MaterialApp(
title: 'flutter tutorial',
home: TutorialHome(),
),
);
}
class TutorialHome extends StatelessWidget {
const TutorialHome({super.key});
@override
widget build(BuildContext context) {
// scaffold is a layout for
// the major material components.
return scaffold(
appBar: AppBar(
leading: const IconButton(
icon: Icon(Icons.menu),
tooltip: 'navigation menu',
onPressed: null,
),
title: const Text('Example title'),
actions: const [
IconButton(
icon: Icon(Icons.search),
tooltip: 'search',
onPressed: null,
),
],
),
// body is the majority of the screen.
body: const center(
child: Text('Hello, world!'),
),
floatingActionButton: const FloatingActionButton(
tooltip: 'add', // used by assistive technologies
onPressed: null,
child: Icon(Icons.add),
),
);
}
}
<code_end>
now that the code has switched from MyAppBar and MyScaffold to the
AppBar and scaffold widgets, and from material.dart,
the app is starting to look a bit more material.
for example, the app bar has a shadow and the title text inherits the
correct styling automatically. a floating action button is also added.
notice that widgets are passed as arguments to other widgets.
the scaffold widget takes a number of different widgets as
named arguments, each of which are placed in the scaffold
layout in the appropriate place. similarly, the
AppBar widget lets you pass in widgets for the
leading widget, and the actions of the title widget.
this pattern recurs throughout the framework and is something you
might consider when designing your own widgets.
for more information, check out material components widgets.
info note
material is one of the 2 bundled designs included with flutter.
to create an iOS-centric design,