MaterialPageRoute 使用 bottomNavigationBar 进入没有 appbar 的新页面

MaterialPageRoute goes to a new page without the appbar using it with bottomNavigationBar

我有一个包含三个路线的应用程序,并使用 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 的第二页

当使用导航栏在页面之间导航时,您正在点击 BottomNavigationBarItem 以通过调用 setState() 更改索引,结果,使用新的 _selectedIndex 触发构建方法并且该索引用于呈现您合适的小部件。

_widgetOptions.elementAt(_selectedIndex)

另一方面,

Navigator.push 只是将一条新路线推到导航堆栈的顶部。您没有获得 AppBar 或 BottomNavigationBar,因为您在 PageTwo 上没有它们。我建议您在 PageTwo 中创建一个回调函数,并在点击按钮时调用该函数。您现在可以在 MyStatefulWidget 中使用该回调来通过 setState 更改索引。这是一个例子

在您的页面中像下面这样声明一个 final。

final void Function(int index) pageChanged;

在你的按钮的 onTap 事件中,调用这个函数。

widget.pageChanged(1); // PageTwo

在MyStatefulWidget中,创建页面时,传入该函数

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

使用 GlobalKey 并在 PageTwo Widget 中调用 MyStatefulWidgetState 的 _onItemTapped 函数
您可以在下面看到工作演示和完整代码
代码片段

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

完整代码

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()),
          );*/
        },
      ),
    );
  }
}