我正在尝试实现外部Route文件,以将我的应用程序的所有路由信息都保存在一个文件中。我现在正在尝试在BottomNavigationBar中使用所述路由文件。
我一直在观看无数视频和阅读文章,并尝试了多种不同的实现方式
Routes.dart
import 'package:flutter/material.dart';
import 'package:second_try/Screens/homeScreen.dart';
import 'package:second_try/Screens/agendaScreen.dart';
class RouteGenerator{
static Route<dynamic> generateRoute(RouteSettings settings){
final args = settings.arguments;
switch (settings.name){
case '/':
return MaterialPageRoute(builder: (_)=> homeScreen());
case '/agenda':
if (args is String) {
return MaterialPageRoute(builder: (_)=> agendaScreen());
}
}
}}
bottomNavigation小部件
import 'package:flutter/material.dart';
import 'package:second_try/Screens/homeScreen.dart';
import 'package:second_try/Screens/agendaScreen.dart';
import 'package:second_try/routes.dart';
class bottomNavigation extends StatefulWidget {
@override
_bottomNavigation createState() => _bottomNavigation();
}
class _bottomNavigation extends State<bottomNavigation> {
int currentNavItem = 0;
homeScreen home;
agendaScreen agenda;
List<Widget> pages;
Widget currentPage;
@override
void initState(){
home = homeScreen();
agenda = agendaScreen();
pages = [home, agenda];
currentPage = home;
super.initState();
}
@override
Widget build(BuildContext context) {
return BottomNavigationBar(
type: BottomNavigationBarType.fixed,
selectedItemColor: Colors.deepPurpleAccent,
unselectedItemColor: Colors.grey,
selectedFontSize: 16,
currentIndex: currentNavItem,
onTap: (int index) {
Navigator.of(context).pushNamed('/agenda');
setState(() {
currentNavItem = index;
currentPage = pages[index];
},
);
},
我对抖动非常陌生,由于这种实现而迷失了自己。谢谢您的帮助和提前输入!
答案 0 :(得分:0)
你可以试试吗?
Navigator.pushReplacementNamed(context, '/agenda');