Flutter setState public var 到另一个页面?

Flutter setState public var to another page?

如何将 setState public var 设置到另一个页面?

int x = 1;

那是在 public 在第一页文本(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'),)
        ],
      ),
      ),
    );
  }
}

请帮我解决这个问题

您可以在屏幕之间传递变量。 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'),
          ),
        ],
      ),
    );
  }
}

您可以使用回调模式。在此示例中,函数 (onPressed) 被传递给 child。 child 在按下按钮时调用函数:

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