将setState public var摇晃到另一个页面?

时间:2020-07-29 23:08:27

标签: flutter dart var public setstate

如何将State public var设置为另一个页面?

int x = 1;
公开的

在第一页text(x)中,我想从另一页设置状态 我的第一页是

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'),
                          ),
                      ],
              ),
        );
  }
在另一页按钮中按

可以将第一页中的var ++ ++ 我的另一页是


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'),)
        ],
      ),
      ),
    );
  }
}

请帮助我

2 个答案:

答案 0 :(得分:1)

您可以使用回调模式。在此示例中,一个函数(onPressed)被传递给子级。按下按钮时,孩子会调用该函数:

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: () {
              Navigator.of(context).push(
                MaterialPageRoute(
                  builder: (context) => AddItemScr(
                    onPressed: () => setState(() => _x++),
                  ),
                ),
              );
            },
            child: Text('goto'),
          ),
        ],
      ),
    );
  }
}

class AddItemScr extends StatelessWidget {
  final VoidCallback onPressed;
  const AddItemScr({
    Key key,
    @required this.onPressed,
  }) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: <Widget>[
          FlatButton(
            onPressed: onPressed,
            child: Text('Increment'),
          ),
        ],
      ),
    );
  }
}

答案 1 :(得分:0)

您可以在屏幕之间传递变量。 NavigatorState#pop支持传递您可以在上一屏幕中等待的对象并将其设置为其值。

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'),
          ),
        ],
      ),
    );
  }
}