使用底部导航栏时,找不到用于路由RouteSettings的生成器

时间:2020-09-16 22:27:55

标签: flutter

我正在尝试使用以下方法实现底部导航栏:https://pub.dev/packages/custom_navigation_bar

但是当我尝试导航到另一条路线并且应用程序没有重新路线时,我得到了一个错误:

在_WidgetsAppState中找不到路由RouteSettings(“ / second”,null)的生成器。

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (context) => SomeProvider(),
      child: MaterialApp(
        title: 'Cool app',
        home: Scaffold(
          body: Navigator(
            onGenerateRoute: (RouteSettings settings) {
              WidgetBuilder builder;

              switch (settings.name) {
                case '/first':
                  builder = (BuildContext context) => First();
                  break;

                case '/second':
                  builder = (BuildContext context) => Second();
                  break;

                case '/random':
                  builder = (BuildContext context) => Random();
                  break;

                case '/another-random':
                  builder = (BuildContext context) => AnotherRandom();
                  break;

                default:
                  throw Exception('Invalid route: ${settings.name}');
              }

              return MaterialPageRoute(builder: builder, settings: settings);
            },
          ),
          bottomNavigationBar: BottomNav(),
        ),
        initialRoute: '/first',
      ),
    );
  }
}

class BottomNav extends StatefulWidget {
  @override
  BottomNavState createState() => BottomNavState();
}

class BottomNavState extends State<BottomNav> {
  int _currentIndex = 0;
  @override
  Widget build(BuildContext context) {
    return CustomNavigationBar(
      iconSize: 30.0,
      selectedColor: Color(0xff040307),
      strokeColor: Color(0x30040307),
      unSelectedColor: Color(0xffacacac),
      backgroundColor: Colors.white,
      items: [
        CustomNavigationBarItem(unSelectedTitle: 'First', selectedTitle: 'First', icon: Icons.play_circle_filled),
        CustomNavigationBarItem(unSelectedTitle: 'Second', selectedTitle: 'Second', icon: Icons.play_circle_filled),
        CustomNavigationBarItem(unSelectedTitle: 'Third', selectedTitle: 'Third', icon: Icons.playlist_add_check)
      ],
      currentIndex: _currentIndex,
      onTap: (index) {
        switch (index) {

          case 0:
            Navigator.pushNamed(context, '/first');
            break;
          case 1:
            Navigator.pushNamed(context, '/second');
            break;
          case 2:
            Navigator.pushNamed(context, '/random');
            break;
          default:
        }
        setState(() {
          _currentIndex = index;
        });
      },
    );
  }
}

我在做什么错,我该如何解决?

0 个答案:

没有答案