text
stringlengths
1
372
),
);
}
<code_end>
the runApp() function takes the given
widget and makes it the root of the widget tree.
in this example, the widget tree consists of two widgets,
the center widget and its child, the text widget.
the framework forces the root widget to cover the screen,
which means the text “hello, world” ends up centered on screen.
the text direction needs to be specified in this instance;
when the MaterialApp widget is used,
this is taken care of for you, as demonstrated later.
when writing an app, you’ll commonly author new widgets that
are subclasses of either StatelessWidget or StatefulWidget,
depending on whether your widget manages any state.
a widget’s main job is to implement a build() function,
which describes the widget in terms of other, lower-level widgets.
the framework builds those widgets in turn until the process
bottoms out in widgets that represent the underlying RenderObject,
which computes and describes the geometry of the widget.
<topic_end>
<topic_start>
basic widgets
flutter comes with a suite of powerful basic widgets,
of which the following are commonly used:
below are some simple widgets that combine these and other widgets:
<code_start>
import 'package:flutter/material.dart';
class MyAppBar extends StatelessWidget {
const MyAppBar({required this.title, super.key});
// fields in a widget subclass are always marked "final".
final widget title;
@override
widget build(BuildContext context) {
return container(
height: 56, // in logical pixels
padding: const EdgeInsets.symmetric(horizontal: 8),
decoration: BoxDecoration(color: colors.blue[500]),
// row is a horizontal, linear layout.
child: row(
children: [
const IconButton(
icon: Icon(Icons.menu),
tooltip: 'navigation menu',
onPressed: null, // null disables the button
),
// expanded expands its child
// to fill the available space.
expanded(
child: title,
),
const IconButton(
icon: Icon(Icons.search),
tooltip: 'search',
onPressed: null,
),
],
),
);
}
}
class MyScaffold extends StatelessWidget {
const MyScaffold({super.key});
@override
widget build(BuildContext context) {
// material is a conceptual piece
// of paper on which the UI appears.
return material(
// column is a vertical, linear layout.
child: column(
children: [
MyAppBar(
title: text(
'example title',
style: theme.of(context) //
.primarytexttheme
.titlelarge,
),
),
const expanded(
child: center(
child: Text('Hello, world!'),
),
),
],
),
);
}
}
void main() {
runApp(
const MaterialApp(
title: 'my app', // used by the OS task switcher
home: SafeArea(
child: MyScaffold(),
),
),
);
}