Stateful widgets in code

MyHomePage is a stateful widget, and so it is defined with a State object, _MyHomePageState, which contains properties that affect how the widget looks:

class MyHomePage extends statefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;

@override
_MyHomePageState createState() => _MyHomePageState();
}

By extending statefulWidget, MyHomePage must return a valid State object in its createState() method. In our example, it returns an instance of _MyHomePageState

Normally, stateful widgets define their corresponding State classes in the same file. Also, state is typically private to the widget library, as external clients do not need to interact with it directly.

The following _MyHomePageState class represents the State object of the MyHomePage widget: 

class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;

void _incrementCounter() {
setState(() {
_counter++;
});
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.display1,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer.
);
}
}

A valid widget state is a class that extends the framework State class, which is defined in the documentation as follows:

"The logic and internal state for a statefulWidget."

The state of the MyHomePage widget is defined by a single property, _counter. The _counter property retains the number of presses of the increment button at the bottom-right corner of the screen. This time, the  State widget'sdescendant class is responsible for building the widget. It is composed of a Text widget that displays the _counter value. 

Text is a built-in widget used to display text on the screen. More on built-in widgets will be covered in the next section.

A stateful widget is meant to change its appearance during its lifetime – that is, what defines it will change – and so it needs to be rebuilt to reflect such changes. Here, the change occurs in the _incementCounter() method, which is called every time the increment button is tapped.

Notice the usage of the onPressed property of the FloatingActionButton widget. FloatingActionButton is the material design floating action button, and this property receives a function callback that will be executed on press:

Flutter Demo Home Page (This is an image of Flutter Demo Home Page. The other (overlapped) information is not important here

How does the framework know when something in the widget changes and it needs to rebuild it? setState is the answer. This method receives a function as a parameter where you should update the widget's corresponding State (that is, the _incrementCounter method).  By calling setState, the framework is notified that it needs to rebuild the widget. In the previous example, it is called to reflect the new value of the _counter property.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.191.22