flutter - 如何在 Flutter 中使用 BottomNavigationBar 和 ElevatedButton 进行导航
问题描述
我BottomNavigationBar
在 Flutter 中使用,我想要的是通过BottomNavigationBarItem
正常点击在页面之间导航。同时,导航到其他页面但在同一个BottomNavigationBarItem
. 让我通过我在这里找到的这个例子来解释更多
说 myBottomNavigationBar
有两个BottomNavigationBarItem
:Call
和Message
. 并且Call
可以导航(例如使用Elevatedbotton
单击)到Page1
and Page2
,同时Message
可以导航(通过Elevatedbotton
单击)到Page3
and page4
。
像这样:
称呼
-第1页
-第2页
信息
-第3页
-第4页
这个解决方案解决了我 50% 的问题,我现在可以做的是从到导航Call
并page1
始终page2
保持BottomNavigationBar
活动状态,现在它仍然是第二部分,即单击另一个BottomNavigationBarItem
以导航到消息
解决方案
你试试这个方法。
class App extends StatefulWidget {
// This widget is the root of your application.
@override
_AppState createState() => _AppState();
}
class _AppState extends State<App> {
int _selectedIndex = 0;
final GlobalKey<NavigatorState> _navigatorKey = GlobalKey<NavigatorState>();
Route<dynamic> generateRoute(RouteSettings settings) {
switch (settings.name) {
case 'Site':
return MaterialPageRoute(builder: (context) => SiteScreen());
default:
return MaterialPageRoute(builder: (context) => Home());
}
}
void _onItemTapped(int index) {
switch (index) {
case 1:
_navigatorKey.currentState!
.pushNamedAndRemoveUntil("Site", (route) => false);
break;
default:
_navigatorKey.currentState!
.pushNamedAndRemoveUntil("Home", (route) => false);
}
setState(() {
_selectedIndex = index;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.black,
toolbarHeight: 0,
),
body: Navigator(key: _navigatorKey, onGenerateRoute: generateRoute),
bottomNavigationBar: BottomNavigationBar(
type: BottomNavigationBarType.fixed,
items: <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.home),
label: 'Home',
),
BottomNavigationBarItem(
icon: Icon(Icons.favorite_border),
label: 'Site',
),
],
showSelectedLabels: true,
showUnselectedLabels: false,
currentIndex: _selectedIndex,
selectedItemColor: Colors.white,
unselectedItemColor: Color(0xFF9e9e9e),
iconSize: 20,
backgroundColor: KBlackColor,
onTap: _onItemTapped,
),
);
}
}
class Home extends StatelessWidget{
@override
Widget build(BuildContext context) {
return Scaffold(
child: Column(children: [
TextButton(
onPressed: () {
MaterialPageRoute(builder: (context) => SubHome())
},
child: Text('Click'),
)
]),
);
}
}
class SubHome extends StatelessWidget{
@override
Widget build(BuildContext context) {
return Scaffold(
child: Column(children: [
Text('SubHome')
]),
);
}
}
推荐阅读
- ruby-on-rails - Rails 5中的范围参数作为数组
- sql-server - 获取未在 T-SQL 中保留的位置
- c# - 向使用 SocketAsyncEventArgs 构建的服务器添加加密
- reactjs - Jest 有时找不到模块
- spring-boot - 如何在 Spring Data Redis 2.0.X 中使用带有 Spring Cache Abstraction (RedisCacheManager) 的 Lettuce Async 连接
- sql - Presto SQL - 在条件之后为每行计数 1,之前为负 1
- python - 从列表中产生每个值
- javascript - 如果图像具有某些尺寸,则检索替代 src?
- git - 检索 Git 推送历史?
- java - 导出的文件与eclipse中编译的文件不一样