MaterialPageRoute转到没有应用栏的新页面,将它与bottomNavigationBar一起使用

时间:2019-10-30 06:24:25

标签: flutter materialpageroute

我有一个具有三个路线的应用,并使用bottomNavigationBar在三个路线之间导航。在其中一种路线中,我在页面中有一个按钮,该按钮也将导航到其中一个页面。

这是我的主页

import 'package:flutter/material.dart';

import 'page_two.dart';
import 'page_three.dart';

void main() {
  return runApp(MyApp());
}

/// This Widget is the main application widget.
class MyApp extends StatelessWidget {
  static const String _title = 'Flutter Code Sample';

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: _title,
      home: MyStatefulWidget(),
    );
  }
}

class MyStatefulWidget extends StatefulWidget {
  MyStatefulWidget({Key key}) : super(key: key);

  @override
  _MyStatefulWidgetState createState() => _MyStatefulWidgetState();
}

class _MyStatefulWidgetState extends State<MyStatefulWidget> {
  int _selectedIndex = 0;

  List<Widget> _widgetOptions = <Widget>[
    Text('Main'),
    PageTwo(),
    PageThree(),
  ];

  void _onItemTapped(int index) {
    setState(() {
      _selectedIndex = index;

    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('BottomNavigationBar Sample'),
      ),
      body: Center(
        child: _widgetOptions.elementAt(_selectedIndex),
      ),
      bottomNavigationBar: BottomNavigationBar(
        items: const <BottomNavigationBarItem>[
          BottomNavigationBarItem(
            icon: Icon(Icons.home, color: Colors.black),
            title: Text('Home'),
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.business, color: Colors.black),
            title: Text('Business'),
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.business, color: Colors.black),
            title: Text('Business'),
          ),
        ],
        currentIndex: _selectedIndex,
        selectedItemColor: Colors.amber[800],
        onTap: _onItemTapped,
      ),
    );
  }
}

第二页

import 'package:flutter/material.dart';

import 'main.dart';

class PageTwo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: RaisedButton(
        child: Text('Go page 1'),
        onPressed: () {
          Navigator.push(
            context,
            MaterialPageRoute(builder: (context) => MyApp()),
          );
        },
      ),
    );
  }
}

和第三页,并带有一个导航到第二页的按钮

import 'package:flutter/material.dart';

import 'page_two.dart';

class PageThree extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: RaisedButton(
        child: Text('Go page 1'),
        onPressed: () {
          Navigator.push(
            context,
            MaterialPageRoute(builder: (context) => PageTwo()),
          );
        },
      ),
    );
  }
}

当我按下第三页上的按钮时,它将转到第二页,而没有AppBar和BottomNavigationBar

2 个答案:

答案 0 :(得分:1)

使用GlobalKey和在PageTwo Widget中调用MyStatefulWidgetState的_onItemTapped函数
您可以在下面查看正在运行的演示和完整代码
代码段

final scakey = new GlobalKey<_MyStatefulWidgetState>();
...
child: Text('Go page 2'),
    onPressed: () {
      scakey.currentState._onItemTapped(1);  

enter image description here

完整代码

import 'package:flutter/material.dart';

void main() {
  return runApp(MyApp());
}

final scakey = new GlobalKey<_MyStatefulWidgetState>();

/// This Widget is the main application widget.
class MyApp extends StatelessWidget {
  static const String _title = 'Flutter Code Sample';

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: _title,
      home: MyStatefulWidget(key: scakey),
    );
  }
}

class MyStatefulWidget extends StatefulWidget {
  MyStatefulWidget({Key key}) : super(key: key);

  @override
  _MyStatefulWidgetState createState() => _MyStatefulWidgetState();
}

class _MyStatefulWidgetState extends State<MyStatefulWidget> {
  int _selectedIndex = 0;

  final myKey = new GlobalKey<_MyStatefulWidgetState>();

  List<Widget> _widgetOptions = <Widget>[
    Text('Main'),
    PageTwo(),
    PageThree(),
  ];

  void _onItemTapped(int index) {
    setState(() {
      _selectedIndex = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: myKey,
      appBar: AppBar(
        title: const Text('BottomNavigationBar Sample'),
      ),
      body: Center(
        child: _widgetOptions.elementAt(_selectedIndex),
      ),
      bottomNavigationBar: BottomNavigationBar(
        items: const <BottomNavigationBarItem>[
          BottomNavigationBarItem(
            icon: Icon(Icons.home, color: Colors.black),
            title: Text('Home'),
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.business, color: Colors.black),
            title: Text('Business'),
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.business, color: Colors.black),
            title: Text('Business'),
          ),
        ],
        currentIndex: _selectedIndex,
        selectedItemColor: Colors.amber[800],
        onTap: _onItemTapped,
      ),
    );
  }
}

class PageTwo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: RaisedButton(
        child: Text('Go page 1'),
        onPressed: () {
          Navigator.push(
            context,
            MaterialPageRoute(builder: (context) => MyApp()),
          );
        },
      ),
    );
  }
}

class PageThree extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: RaisedButton(
        child: Text('Go page 2'),
        onPressed: () {
          scakey.currentState._onItemTapped(1);
          /*Navigator.push(
            context,
            MaterialPageRoute(builder: (context) => PageTwo()),
          );*/
        },
      ),
    );
  }
}

答案 1 :(得分:0)

使用导航栏在页面之间导航时,您可以通过调用setState()来轻击BottomNavigationBarItem来更改索引,结果是,使用新的_selectedIndex触发了build方法,该索引用于呈现适当的窗口小部件。

_widgetOptions.elementAt(_selectedIndex)

另一方面,

Navigator.push只是将新路线推入导航堆栈的顶部。您没有在第2页上没有AppBar或BottomNavigationBar。我建议您在PageTwo中创建一个回调函数,并在单击按钮时调用该函数。现在,您可以在MyStatefulWidget中使用该回调来通过setState更改索引。这是一个示例

在页面中声明一个最终的像。

final void Function(int index) pageChanged;

在按钮的onTap事件中,调用此函数。

widget.pageChanged(1); // PageTwo

在MyStatefulWidget中,当您创建页面时,传递该函数。

PageTwo(pageChanged:(index){
  setState(){_selectedIndex = index;}
});