gpt4 book ai didi

flutter 导航回所选页面

转载 作者:行者123 更新时间:2023-12-04 02:45:42 27 4
gpt4 key购买 nike

我有一个包含底部导航的应用程序,当应用程序加载时,它默认为第一页。如果我单击第二页,然后从第二页导航到另一个未链接到底部导航栏的单独页面。当我单击此页面中 Appbar 中的后退按钮时,我将再次转到第一页而不是第二页。

导航 Controller :

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

页面:
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),
);
},
),

所以当我使用 AppBar 中的图标导航回来时。我会带我到第 1 页而不是第 2 页

最佳答案

使用 Navigator.of(context).pop();应该带您回到具有正确状态的前一个屏幕。为了更好地理解它,请查看这篇文章:https://medium.com/flutter-community/flutter-push-pop-push-1bb718b13c31

如果这不起作用,您必须保存 BottomNavigationBar 的当前索引并在返回后将其传递回您的导航 Controller 。

关于 flutter 导航回所选页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57362166/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com