Chrome Extension

Create stateful widget in Flutter

class Fruit extends StatefulWidget {
    const Fruit({
        Key key,
        this.color = const Color(0xffFF0000),,
    }) : super(key: key);

    final Color color;
    final String name;
    _FruitState createState() => _FruitState();

class _FruitState extends State<Fruit> {
    double _quantity = 3;

    void increaseQty() {
        setState(() { _quantity += 1; });

    Widget build(BuildContext context) {
        return Container(
            color: widget.color,
            child: FlatButton(
                onPressed: increaseQty(),
                child: Text(,
Best JSON Validator, JSON Tree Viewer, JSON Beautifier at same place. Check how cool is the tool

Stateful widgets are used to define states in Flutter that can be modified and bound with other widgets. Creating a stateful widget in flutter is different from creating a stateless widget. You can create stateful widgets by inheriting StatefulWidget class in your class and then create another class for the state definition of this class as created in the code snippet.

In the code snippet, we have created a Fruit class and its state class is _FruitState where we are defining a state _quantity which can be changed on button click.

Search Index Data (The code snippet can also be found with below search text)

Stateful widget example in Flutter
Was this helpful?
Join Devsheet Ask a Question