text stringlengths 1 372 |
|---|
for example, on iOS, you can use the cupertino widgets |
to produce an interface that looks like apple’s iOS design language. |
<topic_end> |
<topic_start> |
how do i update widgets? |
in Xamarin.Forms, each page or element is a stateful class, |
that has properties and methods. |
you update your element by updating a property, |
and this is propagated down to the native control. |
in flutter, widgets are immutable and you can’t directly update them |
by changing a property, instead you have to work with the widget’s state. |
this is where the concept of stateful vs stateless widgets comes from. |
a StatelessWidget is just what it sounds like— |
a widget with no state information. |
StatelessWidgets are useful when the part of the user interface |
you are describing doesn’t depend on anything |
other than the configuration information in the object. |
for example, in Xamarin.Forms, this is similar |
to placing an image with your logo. |
the logo is not going to change during runtime, |
so use a StatelessWidget in flutter. |
if you want to dynamically change the UI based on data received |
after making an HTTP call or a user interaction, |
then you have to work with StatefulWidget |
and tell the flutter framework that |
the widget’s state has been updated, |
so it can update that widget. |
the important thing to note here is at the core |
both stateless and stateful widgets behave the same. |
they rebuild every frame, the difference is |
the StatefulWidget has a state object |
that stores state data across frames and restores it. |
if you are in doubt, then always remember this rule: if a widget changes |
(because of user interactions, for example) it’s stateful. |
however, if a widget reacts to change, the containing parent widget can |
still be stateless if it doesn’t itself react to change. |
the following example shows how to use a StatelessWidget. |
a common StatelessWidget is the text widget. |
if you look at the implementation of the text widget |
you’ll find it subclasses StatelessWidget. |
<code_start> |
const text( |
'i like flutter!', |
style: TextStyle(fontWeight: FontWeight.bold), |
); |
<code_end> |
as you can see, the text widget has no state information associated 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, |
as shown in the following example: |
<code_start> |
import 'package:flutter/material.dart'; |
void main() { |
runApp(const SampleApp()); |
} |
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> |
how do i lay out my widgets? what is the equivalent of an XAML file? |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.