共享 Flutter 抽屉出现导航错误

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

这让我发疯!我有一个从 StatelessWidget 扩展的共享抽屉。我的屏幕全部从 Scaffold 加载到这个抽屉中(所有屏幕都以 Scaffolds 开头,应用程序以 MaterialApp 开头)。每当我尝试在抽屉对象中导航时,都会出现异常:

FlutterError (Navigator operation requested with a context that does not include a Navigator.
The context used to push or pop routes from the Navigator must be that of a widget that is a descendant of a Navigator widget.)

我尝试了很多方法来解决这个问题,但没有任何效果。这是抽屉:

import 'package:arcadious/home.dart';
import 'package:flutter/material.dart';

class ArcadiousDrawer extends StatelessWidget {
  const ArcadiousDrawer({super.key});

  @override
  Widget build(BuildContext context) {
    return Drawer(
      child: ListView(
        padding: EdgeInsets.zero,
        children: <Widget>[
          SizedBox(
            height: 100,
            child: DrawerHeader(
              decoration:
                  const BoxDecoration(color: Color.fromARGB(255, 10, 10, 10)),
              child: Center(
                child: Container(
                  padding: const EdgeInsets.all(0),
                  child: Image.asset('assets/arcadious_logo.png',
                      fit: BoxFit.contain),
                ),
              ),
            ),
          ),
          
          Builder(builder: (context) =>
          ListTile(
            title: const Text(
              '- HOME',
              style: TextStyle(
                fontFamily: 'Outfit',
                fontWeight: FontWeight.w700,
                color: Colors.white,
                fontSize: 22,
              ),
            ),
            onTap: () {
              final ScaffoldState scaffoldState =
                  context.findRootAncestorStateOfType<ScaffoldState>()!;
              scaffoldState.closeDrawer();

        Navigator.pushAndRemoveUntil(
          context,
          MaterialPageRoute(builder: (context) => const HomeScreen()),
          (Route<dynamic> route) => false,
        );},
          ),),
        ],
      ),
    );
  }
}

我在所有屏幕上使用的另一个共享小部件中打开抽屉。这是共享标题小部件:

import 'package:flutter/material.dart';

class Header extends StatelessWidget {
  const Header({super.key});

  @override
  Widget build(BuildContext context) {
    return Container(margin: const EdgeInsets.only(bottom: 10),
    padding: const EdgeInsets.only(bottom: 15),
      decoration: const BoxDecoration(
    border: Border(bottom: BorderSide(width: 1, color: Colors.grey, style: BorderStyle.solid))
  ),
    child: Row(
      children: <Widget>[
        Expanded(child: Align(alignment: Alignment.centerLeft, child:
        SizedBox(
          width: 23,
          height: 23,
          child: GestureDetector(onTap: () {
            final ScaffoldState scaffoldState = context.findRootAncestorStateOfType<ScaffoldState>()!;
          scaffoldState.openDrawer();
          },
          child:Image.asset('assets/hamburger.png',
              fit: BoxFit.contain),),
        ),),),
        Expanded(child:
        SizedBox(
          width: 25,
          height: 25,
          child: Image.asset('assets/arcadious_logo.png',
              fit: BoxFit.contain),
        ),),
        Expanded(child: Align(alignment: Alignment.centerRight, child:
        SizedBox(
          width: 25,
          height: 25,
          child: Image.asset('assets/avatar.png',
              fit: BoxFit.contain),
        ),),),
      ],
    ),);
  }
}

我已经尝试了很多事情 - 据我所知,这个抽屉始终位于一个始终具有支架的屏幕上,并且始终是位于 MaterialApp 中的根屏幕的父屏幕。我尝试过只是弹出,并且尝试将链接封装到生成器中。

flutter
1个回答
0
投票

我无法添加评论,但我有一个建议:

看起来您正在使用构建器的上下文来实现

Navigator.pushAndRemoveUntil
。您可以尝试通过不同的重命名来使用构建函数中的函数吗?

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