text
stringlengths
1
372
const MyApp({super.key});
@override
widget build(BuildContext context) {
return const MaterialApp(
title: 'flutter demo',
home: MyHomePage(title: 'flutter demo home page'),
);
}
}
<code_end>
from here, your actual first page is another widget,
in which you create your state.
a stateful widget, such as MyHomePage below, consists of two parts.
the first part, which is itself immutable, creates a state object
that holds the state of the object. the state object persists over
the life of the widget.
<code_start>
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final string title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
<code_end>
the state object implements the build() method for the stateful widget.
when the state of the widget tree changes, call setState(),
which triggers a build of that portion of the UI.
make sure to call setState() only when necessary,
and only on the part of the widget tree that has changed,
or it can result in poor UI performance.
<code_start>
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
widget build(BuildContext context) {
return scaffold(
appBar: AppBar(
// take the value from the MyHomePage object that was created by
// the app.build method, and use it to set the appbar title.
title: text(widget.title),
),
body: center(
// center is a layout widget. it takes a single child and positions it
// in the middle of the parent.
child: column(
mainAxisAlignment: MainAxisAlignment.center,
children: <widget>[
const text(
'you have pushed the button this many times:',
),
text(
'$_counter',
style: Theme.of(context).textTheme.headlineMedium,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'increment',
child: const Icon(Icons.add),
),
);
}
}
<code_end>
in flutter, the UI (also known as widget tree), is immutable,
meaning you can’t change its state once it’s built.
you change fields in your state class, then call setState()
to rebuild the entire widget tree again.
this way of generating UI is different from Xamarin.Forms,
but there are many benefits to this approach.
<topic_end>
<topic_start>
views
<topic_end>
<topic_start>
what is the equivalent of a page or element in flutter?
how is react-style, or declarative, programming different from the
traditional imperative style?
for a comparison, see introduction to declarative UI.
ContentPage, TabbedPage, FlyoutPage are all types of pages
you might use in a Xamarin.Forms application.
these pages would then hold elements to display the various controls.
in Xamarin.Forms an entry or button are examples of an element.
in flutter, almost everything is a widget.
a page, called a route in flutter, is a widget.
buttons, progress bars, and animation controllers are all widgets.
when building a route, you create a widget tree.
flutter includes the material components library.
these are widgets that implement the material design guidelines.
material design is a flexible design system
optimized for all platforms, including iOS.
but flutter is flexible and expressive enough
to implement any design language.