text
stringlengths 1
372
|
---|
describing does not depend on anything other than the initial configuration
|
information in the widget.
|
for example, with UIKit, this is similar to placing a UIImageView
|
with your logo as the image. if the logo is not changing during runtime,
|
use a StatelessWidget in flutter.
|
if you want to dynamically change the UI based on data received
|
after making an HTTP call, use a StatefulWidget.
|
after the HTTP call has completed, tell the flutter framework
|
that the widget’s state is updated, so it can update the UI.
|
the important difference between stateless and
|
stateful widgets is that StatefulWidgets have a state object
|
that stores state data and carries it over across tree rebuilds,
|
so it’s not lost.
|
if you are in doubt, remember this rule:
|
if a widget changes outside of the build method
|
(because of runtime user interactions, for example),
|
it’s stateful.
|
if the widget never changes, once built, it’s stateless.
|
however, even if a widget is stateful, the containing parent widget
|
can still be stateless if it isn’t itself reacting to those changes
|
(or other inputs).
|
the following example shows how to use a StatelessWidget.
|
a commonStatelessWidget is the text widget.
|
if you look at the implementation of the text widget,
|
you’ll find it subclasses StatelessWidget.
|
<code_start>
|
text(
|
'i like flutter!',
|
style: TextStyle(fontWeight: FontWeight.bold),
|
);
|
<code_end>
|
if you look at the code above, you might notice that the text widget
|
carries no explicit state with it. it renders what is passed in its
|
constructors and nothing more.
|
but, what if you want to make “i like flutter” change dynamically,
|
for example when clicking a FloatingActionButton?
|
to achieve this, wrap the text widget in a StatefulWidget and
|
update it when the user clicks the button.
|
for example:
|
<code_start>
|
class SampleApp extends StatelessWidget {
|
// this widget is the root of your application.
|
const SampleApp({super.key});
|
@override
|
widget build(BuildContext context) {
|
return const MaterialApp(
|
title: 'sample app',
|
home: SampleAppPage(),
|
);
|
}
|
}
|
class SampleAppPage extends StatefulWidget {
|
const SampleAppPage({super.key});
|
@override
|
State<SampleAppPage> createState() => _SampleAppPageState();
|
}
|
class _SampleAppPageState extends State<SampleAppPage> {
|
// default placeholder text
|
string textToShow = 'i like flutter';
|
void _updateText() {
|
setState(() {
|
// update the text
|
textToShow = 'flutter is awesome!';
|
});
|
}
|
@override
|
widget build(BuildContext context) {
|
return scaffold(
|
appBar: AppBar(title: const Text('Sample app')),
|
body: center(child: Text(textToShow)),
|
floatingActionButton: FloatingActionButton(
|
onPressed: _updateText,
|
tooltip: 'update text',
|
child: const Icon(Icons.update),
|
),
|
);
|
}
|
}
|
<code_end>
|
<topic_end>
|
<topic_start>
|
widget layout
|
in UIKit, you might use a storyboard file
|
to organize your views and set constraints,
|
or you might set your constraints programmatically in your view controllers.
|
in flutter, declare your layout in code by composing a widget tree.
|
the following example shows how to display a simple widget with padding:
|
<code_start>
|
@override
|
widget build(BuildContext context) {
|
return scaffold(
|
appBar: AppBar(title: const Text('Sample app')),
|
body: center(
|
child: CupertinoButton(
|
onPressed: () {},
|
padding: const EdgeInsets.only(left: 10, right: 10),
|
child: const Text('Hello'),
|
),
|
),
|
);
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.