text stringlengths 1 474 |
|---|
These can act as containers for other UIView classes, |
which form your layout.In Flutter, the rough equivalent to a UIView is a Widget. |
Widgets don’t map exactly to iOS views, |
but while you’re getting acquainted with how Flutter works |
you can think of them as “the way you declare and construct UI”.However, these have a few differences to a UIView. |
To start, widgets have a different lifespan: they are immutable |
and only exist until they need to be changed. |
Whenever widgets or their state change, |
Flutter’s framework creates a new tree of widget instances. |
In comparison, a UIKit view is not recreated when it changes, |
but rather it’s a mutable entity that is drawn once |
and doesn’t redraw until it is invalidated using setNeedsDisplay().Furthermore, unlike UIView, Flutter’s widgets are lightweight, |
in part due to their immutability. |
Because they aren’t views themselves, |
and aren’t directly drawing anything, |
but rather are a description of the UI and its semantics |
that get “inflated” into actual view objects under the hood.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. |
On iOS, you can use the Cupertino widgets |
to produce an interface that looks like |
Apple’s iOS design language.<topic_end> |
<topic_start> |
Updating Widgets |
To update your views in UIKit, you directly mutate them. |
In Flutter, widgets are immutable and not updated directly. |
Instead, you have to manipulate the widget’s state.This is where the concept of Stateful vs Stateless widgets |
comes in. A StatelessWidget is just what it sounds |
like—a widget with no state attached.StatelessWidgets are useful when the part of the user interface you are |
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> |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.