为什么 Flutter 不基于 main.dart 文件进行导航?

问题描述 投票:0回答:1

我试图在 Flutter 中使用持久小部件实现导航,但调用

Navigation.of(context).pushNamed('/route')
会产生“使用不包含导航器的上下文请求导航器操作”错误。 每个类的代码如下:

main.dart

@override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Benchside',
      theme: ThemeData(
            fontFamily: 'GemunuLibre',
      ),
      builder: (context, child) => BaseWidget(content: child!),
      initialRoute: '/',
      routes: {
        '/': (context) => const HomePage(),
        '/profile': (context) => const ProfilePage(),
      },
      onGenerateRoute: (settings) {
        if (settings.name == '/') {
          return MaterialPageRoute(builder: (context) => const HomePage());
        }
        else if (settings.name == '/profile') {
          return MaterialPageRoute(builder: (context) => const ProfilePage());
        }
      },
    );
  }

BaseWidget.dart

@override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: const Color.fromARGB(0xFF, 0xFA, 0xFA, 0xFA),
        body: SafeArea(
          minimum: const EdgeInsets.fromLTRB(0.0, 15.0, 0.0, 0.0),
          child: CColumn(children: [

        //HEADER
        AppBar(
          title: const Text(
            'BENCHSIDE',
            style: TextStyle(fontSize: 30.0, fontWeight: FontWeight.bold),
          ),
          centerTitle: true,
          backgroundColor: const Color.fromARGB(0xFF, 0xFA, 0xFA, 0xFA),
          scrolledUnderElevation: 0.0,
          leading: IconButton(
            onPressed: () {  },
            icon: const Image(
              image: AssetImage('assets/images/setting.png'),
              width: 30.0,
              height: 30.0,
              color: Color.fromARGB(0xFF, 0xA4, 0xA4, 0xA4),
            ),
          ),
          actions: [
            IconButton(
                onPressed: () {},
                icon: const Image(
                  image: AssetImage('assets/images/announcements.png'),
                  width: 30.0,
                  height: 30.0,
                  color: Color.fromARGB(0xFF, 0xA4, 0xA4, 0xA4),
                )
            )
          ],
        ),


    // PAGE VIEWER
        Expanded(child: content,),

        // NAVIGATION BAR
    Container(
        decoration: const BoxDecoration(
            color: Colors.transparent,
            borderRadius: BorderRadius.only(
                topLeft: Radius.circular(10.0),
                topRight: Radius.circular(10.0),
            ),
        ),
        child:
                Row(
                mainAxisAlignment: MainAxisAlignment.spaceAround,
                children: [
                  GestureDetector(
                    onTap: () => {Navigator.pushNamed(context, '/')},
                    child:
                      const Image(image: AssetImage('assets/images/home.png'), 
                      width: 30.0, 
                      height: 30.0,
                  ),),
                    Image(image: AssetImage('assets/images/ranking.png'), 
                    width: 30.0, 
                    height: 30.0, 
                    color: Color.fromARGB(0xFF, 0xA4, 0xA4, 0xA4),),
                    Image(image: AssetImage('assets/images/community.png'), 
                    width: 30.0, 
                    height: 30.0, 
                    color: Color.fromARGB(0xFF, 0xA4, 0xA4, 0xA4),),
                                  GestureDetector(
                    onTap: () => {Navigator.of(context).pushNamed('/profile')},
                    child: const CircleAvatar()
                  ),
        ],),
        )],
    ),),
      );
  }

我尝试切换到使用主页而不是初始路线,但这似乎也不起作用。

如果您仍然对我想要做的事情感到困惑,这里是我正在遵循的指南

flutter dart routes mobile
1个回答
0
投票

事实证明,为了完成我想要完成的任务,我必须使用自定义导航服务并通过“get_it”包使用依赖注入。

具体信息可以查看这里

© www.soinside.com 2019 - 2024. All rights reserved.