如何在Flutter中用新内容重新加载页面?

时间:2020-05-15 19:59:35

标签: flutter

我如何创建一个按钮,当用户单击该按钮时,页面将刷新并显示新内容?

2 个答案:

答案 0 :(得分:0)

调用setState(() {})方法。为此,您需要一个StatefulWidget。 您的代码将类似于

 RaisedButton(
    child : Text("Tap"),
    onPressed : () {
        setState(() {
          // modifiy the state
        })
    }
  ),

答案 1 :(得分:0)

Here is a Codepen example of the code below

class _MyWidgetState extends State<MyWidget> {
  int counter = 0;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          children: <Widget>[
            FlatButton(
              onPressed: () {
                setState(() {
                  counter++;
                });
              },
              child: Text(
                "Flat Button",
              ),
            ),
            Text(
          'Counter',
          style: Theme.of(context).textTheme.headline4,
        ),
            Text(
          '$counter',
          style: Theme.of(context).textTheme.headline4,
        ),
          ],
        ),
      ),
    );
  }
}