Stateless widget and stateful widget | Flutter tutorial for beginners step by step | SCODE



what is a Stateless widget?


A StatefulWidget tracks its own internal state.

A StatelessWidget doesn’t have any internal state that changes during the lifetime of the widget.
It doesn’t care about its configuration or what data it’s displaying.

It might be passed configuration from its parent, or the configuration might be defined within the widget, but it cannot change its own configuration. A stateless widget is immutable.

a stateless widget shouldn’t be responsible for any data you don’t want to lose. Once it’s gone, it’s gone. 

Stateful widgets


A stateful widget has an inside state and may manage that state. All stateful widgets have
corresponding state objects.

class MyHomePage extends StatefulWidget {
@override
    _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
    // ..
    }
}

almost every widget class has a build() method

the StatefulWidget class doesn’t have a build method. But every stateful widget has an associated state object, which has a build method.

stateful widgets are immutable (just like stateless widgets) which means we can not change state, but their internal objects are mutable and may hold onto state whilst Flutter re-renders the widgets.


How to Create private values in Flutter?


notice that the class name is _MyMainPageState. It starts with an underscore, which is used to mark the class as private. All statements can be private. 

A top-level value that’s private, such as this class, is only available within the current file. If a class member, such as a variable and function, is marked as private,
it’s available only to use within that class itself.

Consider this Cat class:

class Vehicle{
    String name;
    String _color;
    void Bike() => print("bike");
    void _car() => print("car");
}

what is the setState method?


setState is the important Flutter method, after build and createState. It is available only for the state object in stateful widgets.

this method executes when the user calls an event or performs any operation in the application screen, (like, increase the counter variable by one).

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


initState


State.initState is the method in which you initialize any data is needed before Flutter tries to paint it the screen.

BuildContext


 Every build() method in a widget takes one argument, which is BuildContext, which is a reference to a widget’s location in the widget tree. Remember, build is called by the framework itself, so you don’t have to manage the build context yourself.



Visit other pages:

>> How To Install WordPress on localhost 
 

Post a Comment

Previous Post Next Post