text stringlengths 1 474 |
|---|
), |
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. |
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(), |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.