我有一个包含底部导航的应用程序,当应用程序加载时,它默认为第一页。如果我单击第二页,然后从第二页导航到另一个未链接到底部导航栏的单独页面。当我单击此页面中 Appbar 中的后退按钮时,我将再次转到第一页而不是第二页。
导航控制器:
import 'package:flutter/material.dart';
import '../pages/TaxCalcPage.dart';
import '../pages/TaxInfoPage.dart';
import '../pages/SettingsPage.dart';
class BottomNavigationBarController extends StatefulWidget {
@override
_BottomNavigationBarControllerState createState() =>
_BottomNavigationBarControllerState();
}
class _BottomNavigationBarControllerState extends State<BottomNavigationBarController> {
final List<Widget> pages = [
TaxCalcPage(
key: PageStorageKey('Page1'),
),
TaxInfoPage(
key: PageStorageKey('Page2'),
),
SettingsPage(
key: PageStorageKey('Page3'),
)
];
final PageStorageBucket bucket = PageStorageBucket();
int _selectedIndex = 0;
Widget _bottomNavigationBar(int selectedIndex) => BottomNavigationBar(
onTap: (int index) => setState(() => _selectedIndex = index),
currentIndex: selectedIndex,
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.home), title: Text('Tax Calculators')),
BottomNavigationBarItem(
icon: Icon(Icons.info), title: Text('Tax Information')),
BottomNavigationBarItem(
icon: Icon(Icons.settings), title: Text('Settings')),
],
);
@override
Widget build(BuildContext context) {
return Scaffold(
bottomNavigationBar: _bottomNavigationBar(_selectedIndex),
body: PageStorage(
child: pages[_selectedIndex],
bucket: bucket,
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
页:
return Scaffold(
appBar: AppBar(
leading: Builder(
builder: (BuildContext context) {
return IconButton(
icon: const Icon(Icons.chevron_left),
iconSize: (0.06 * ratio) * width,
splashColor: Colors.transparent,
highlightColor: Colors.transparent,
onPressed: () => Navigator.of(context).pushAndRemoveUntil(MaterialPageRoute(builder: (BuildContext context) => BottomNavigationBarController()), (Route route) => route == null),
);
},
),
Run Code Online (Sandbox Code Playgroud)
所以当我使用 AppBar 中的图标导航回来时。我会带我到第 1 页而不是第 2 页
使用Navigator.of(context).pop();应该使您返回到具有正确状态的前一个屏幕。为了更好地理解它,请查看这篇文章:https : //medium.com/flutter-community/flutter-push-pop-push-1bb718b13c31
如果这不起作用,您必须保存您的当前索引,BottomNavigationBar并在返回后将其传递回导航控制器。
| 归档时间: |
|
| 查看次数: |
7873 次 |
| 最近记录: |