当我在 flutter 中单击 phone 中的后退按钮时,底部选项卡导航器会显示

Bottom tab navigator show up when I click back button from my phone in flutter

嗨,我是 flutter 的新手,现在我尝试使用此代码为所有选项卡声明我的数据,但是当我单击选项卡时,底部导航器被关闭并在我单击 phone,有没有人知道错误或问题?

class BottomTab extends StatefulWidget {
final String text;
BottomTab({this.text});
  @override
  _BottomTabState createState() => _BottomTabState();
}
class _BottomTabState extends State<BottomTab> {
void initState() {
    super.initState();
   }
_onTap(int index) {
    setState(() => _selectedIndex = index);
    if (index == 0) {
      Navigator.push(
          context,
          new MaterialPageRoute(
              builder: (BuildContext context) =>
                  new HomeScreen(value: widget.value)));
    } else if (index == 1) {
      Navigator.push(
          context,
          new MaterialPageRoute(
              builder: (BuildContext context) =>
                  new FirstPage(value: widget.value)));
    } else {
      Navigator.push(
          context,
          new MaterialPageRoute(
              builder: (BuildContext context) =>
                  new HomeScreen(value: widget.value)));
    }

  }
  final List<Widget> pages = [
    HomeScreen(),
FirstPage(),
  ];

  final PageStorageBucket bucket = PageStorageBucket();

  int _index = 0;

  Widget _myList(int index) => BottomNavigationBar(
        onTap: _onTap,
        currentIndex: index,
        items: const <BottomNavigationBarItem>[
          BottomNavigationBarItem(icon: Icon(Icons.home), title: Text('Home')),
BottomNavigationBarItem(icon: Icon(Icons.more), title: Text('More')),
        ],
      );

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      bottomNavigationBar: _myList(_index),
      body: PageStorage(
        child: pages[_index],
        bucket: bucket,
      ),
    );
  }
}

您可以复制粘贴 运行 下面的完整代码
在您的情况下,您不需要 Navigator.push 并更改此行
来自

setState(() => _selectedIndex = index);

setState(() => _index = index);

工作演示

完整代码

import 'package:flutter/material.dart';

class BottomTab extends StatefulWidget {
  final String text;
  BottomTab({this.text});
  @override
  _BottomTabState createState() => _BottomTabState();
}

class _BottomTabState extends State<BottomTab> {
  void initState() {
    super.initState();
  }

  _onTap(int index) {
    setState(() => _index = index);
    /*if (index == 0) {
      Navigator.push(
          context,
          new MaterialPageRoute(
              builder: (BuildContext context) =>
              new HomeScreen(value: widget.value)));
    } else if (index == 1) {
      Navigator.push(
          context,
          new MaterialPageRoute(
              builder: (BuildContext context) =>
              new FirstPage(value: widget.value)));
    } else {
      Navigator.push(
          context,
          new MaterialPageRoute(
              builder: (BuildContext context) =>
              new HomeScreen(value: widget.value)));
    }*/
  }

  final List<Widget> pages = [
    HomeScreen(),
    FirstPage(),
  ];

  final PageStorageBucket bucket = PageStorageBucket();

  int _index = 0;

  Widget _myList(int index) => BottomNavigationBar(
        onTap: _onTap,
        currentIndex: index,
        items: const <BottomNavigationBarItem>[
          BottomNavigationBarItem(icon: Icon(Icons.home), title: Text('Home')),
          BottomNavigationBarItem(icon: Icon(Icons.more), title: Text('More')),
        ],
      );

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      bottomNavigationBar: _myList(_index),
      body: PageStorage(
        child: pages[_index],
        bucket: bucket,
      ),
    );
  }
}

class HomeScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text("demo"),
        ),
        body: Text("HomesScreen"));
  }
}

class FirstPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text("demo"),
        ),
        body: Text("FirstPage"));
  }
}

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: BottomTab(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headline4,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}