Search code examples
flutterdartvarpublicsetstate

Flutter setState public var to another page?


how to setState public var to another page?

int x = 1;

that was in public in the first page text(x) i want to setstate from the other page my first page is

class AddFullRequest extends StatefulWidget {
  @override
  _AddFullRequestState createState() => _AddFullRequestState();
}

class _AddFullRequestState extends State<AddFullRequest> {
  Widget build(BuildContext context) {
    return Scaffold(
 body: Column(
     children: <Widget>[
                  Text(x),
                   GestureDetector(
                          onTap: (){
                            Navigator.of(context).push(new MaterialPageRoute(
                                builder: (BuildContext context) => AddItemScr()));

                          },
                          child: Text('goto'),
                          ),
                      ],
              ),
        );
  }

in the other page button to ++ the var in the first page my other page is


class AddItemScr extends StatefulWidget {
  @override
  _AddItemScrState createState() => _AddItemScrState();
}

class _AddItemScrState extends State<AddItemScr> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: WillPopScope(onWillPop: (){
        Navigator.of(context).pop();
      },
      child: Column(
        children: <Widget>[
          FlatButton(onPressed: (){setState(() {
            x++;
          });}, child: Text('pluss'),)
        ],
      ),
      ),
    );
  }
}

please help me with this


Solution

  • You can pass variables between screens. NavigatorState#pop supports passing objects that you can await in the previous screen and set it to it's value.

    class AddFullRequest extends StatefulWidget {
      @override
      _AddFullRequestState createState() => _AddFullRequestState();
    }
    
    class _AddFullRequestState extends State<AddFullRequest> {
      int x = 0;
    
      Widget build(BuildContext context) {
        return Scaffold(
          body: Column(
            children: <Widget>[
              Text('$x'),
              GestureDetector(
                onTap: () async {
                  final result = await Navigator.of(context).push<int>(
                    MaterialPageRoute(
                      builder: (_) => AddItemScr(variable: x),
                    ),
                  );
                  x = result;
                  setState(() {});
                },
                child: Text('goto'),
              ),
            ],
          ),
        );
      }
    }
    
    class AddItemScr extends StatefulWidget {
      final int variable;
    
      AddItemScr({this.variable});
    
      @override
      _AddItemScrState createState() => _AddItemScrState();
    }
    
    class _AddItemScrState extends State<AddItemScr> {
      int _variable;
    
      @override
      void initState() {
        _variable = widget.variable;
        super.initState();
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Column(
            children: <Widget>[
              FlatButton(
                onPressed: () {
                  setState(() {
                    _variable++;
                  });
                },
                child: Text('pluss'),
              ),
              FlatButton(
                onPressed: () {
                  Navigator.of(context).pop(_variable);
                },
                child: Text('go back'),
              ),
            ],
          ),
        );
      }
    }