美文网首页
Flutter Navigation and routing 翻

Flutter Navigation and routing 翻

作者: 三只仓鼠 | 来源:发表于2020-12-24 13:26 被阅读0次

1.首先创建2个页面 也就是Widget

class FirstRoute extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('First Route'),
      ),
      body: Center(
        child: RaisedButton(
          child: Text('Open route'),
          onPressed: () {
            // Navigate to second route when tapped.
          },
        ),
      ),
    );
  }
}

class SecondRoute extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Second Route"),
      ),
      body: Center(
        child: RaisedButton(
          onPressed: () {
            // Navigate back to first route when tapped.
          },
          child: Text('Go back!'),
        ),
      ),
    );
  }
}

2.使用Navigator.push()导航到第二个页面 (添加到第一个页面的按钮点击事件中)

// Within the `FirstRoute` widget
onPressed: () {
  Navigator.push(
    context,
    MaterialPageRoute(builder: (context) => SecondRoute()),
  );
}

3.使用Navigator.pop()返回第一个页面(添加到第二个页面的按钮事件中)

// Within the SecondRoute widget
onPressed: () {
  Navigator.pop(context);
}

相关文章

网友评论

      本文标题:Flutter Navigation and routing 翻

      本文链接:https://www.haomeiwen.com/subject/uedhkhtx.html