flutter 使用命名路线时,找不到路线错误的生成器

ttcibm8c  于 2022-12-05  发布在  Flutter
关注(0)|答案(2)|浏览(132)

我尝试将CupertinoTabBar与命名路线一起使用。我尝试添加一个CupertinoTabScaffold,并在标签栏中显示标签视图,但当应用将应用推送到不同的路线时,应用抛出错误:
FlutterError(无法在_CupertinoTabViewState中找到路径RouteSettings(“/randomONE”,空)的生成器

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (context) => SomeProvider(),
      child: MaterialApp(
        title: 'Cool Title',
        home: CupertinoTabScaffold(
            tabBar: CupertinoTabBar(
              items: [
                BottomNavigationBarItem(title: Text('First Page'), icon: Icon(Icons.access_alarms)),
                BottomNavigationBarItem(title: Text('Second Page'), icon: Icon(Icons.account_balance)),
                BottomNavigationBarItem(title: Text('Third Page'), icon: Icon(Icons.accessible)),
              ],
            ),
            tabBuilder: (context, index) {
              CupertinoTabView selectedView;
              switch (index) {
                case 0:
                  selectedView = CupertinoTabView(builder: (context) {
                    return CupertinoPageScaffold(child: FirstPage());
                  });
                  break;
                default:
              }

              return selectedView;
            }),
        initialRoute: NamedRoutes.splashScreen,
        routes: {
          '/first': (BuildContext context) => FirstPage(),
          '/second': (BuildContext context) => SecondPage(),
          '/randomONE': (BuildContext context) => ThirdPage(),
        },
      ),
    );
  }
}
fafcakar

fafcakar1#

您只需完成切换:

switch (index) {
  case 0:
    selectedView = CupertinoTabView(builder: (context) {
      return CupertinoPageScaffold(child: FirstPage());
    });
    break;
  case 1:
    selectedView = CupertinoTabView(builder: (context) {
      return CupertinoPageScaffold(child: SecondPage());
    });
    break;
  case 2:
    selectedView = CupertinoTabView(builder: (context) {
      return CupertinoPageScaffold(child: ThirdPage());
    });
    break;
  default:
}
laik7k3q

laik7k3q2#

CupertinoTabBar的CupertinoTabView中使用routesonGenerateRoutes属性,然后在此处传递您的路线。您的问题将得到解决。

相关问题