Flutter Navigation and routing 翻

2020-12-24  本文已影响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);
}

上一篇 下一篇

猜你喜欢

热点阅读