MaterialApp 构建器错误:找不到覆盖小部件

aks*_*nge 14 dart flutter flutter-layout

我在构建 navigationDrawer 时遇到错误,其中 tootlip 小部件需要 MaterialApp 作为祖先。

这是错误的内容:

I/flutter ( 5780): _TooltipState#bc79e(ticker inactive)):
I/flutter ( 5780): No Overlay widget found.
I/flutter ( 5780): Tooltip widgets require an Overlay widget ancestor for correct operation.
I/flutter ( 5780): The most common way to add an Overlay to an application is to include a MaterialApp or Navigator
I/flutter ( 5780): widget in the runApp() call.
I/flutter ( 5780): The specific widget that failed to find an overlay was:
I/flutter ( 5780):   Tooltip
I/flutter ( 5780): 
I/flutter ( 5780): The relevant error-causing widget was:
I/flutter ( 5780):   AppBar
Run Code Online (Sandbox Code Playgroud)

我的main.dart代码

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      ... //basic info title & theme

      builder: (context, child) => LayoutTemplate(child: child),
      initialRoute:"/home",

      ... //Routing stuff like generate route & navigator key 
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

布局模板小部件

class LayoutTemplate extends StatelessWidget {
  final Widget child;

  const LayoutTemplate({Key key, this.child}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(title: Text("home"))
        drawer: NavDrawer()
        body: Column(
          children: <Widget>[
            //NavigationBar(),
            Expanded(
              child: child,
            )
          ],
       ),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

抱歉添加了太多代码。我不确定是什么导致了这个问题。也许是builderfromMaterialApp造成的。

感谢您的帮助。

Coa*_*edi 28

在您的构建器中,只需返回一个 Overlay 小部件,并将 LayoutTemplate 作为 OverlayEntry。

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      ... //basic info title & theme

      builder: (context, child) {
        return Overlay(
          initialEntries: [
            OverlayEntry(
              builder: (context) => LayoutTemplate(child: child),
            ),
          ],
        );
      },
      initialRoute:"/home",

      ... //Routing stuff like generate route & navigator key 
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

  • 基本上,MaterialApp 在幕后创建此 Overlay,但每当我们使用此“builder”参数时,MaterialApp 默认创建的所有内容都不再创建,而必须在“builder”内手动创建,包括 Overlay。 (5认同)

Mar*_*nie 5

如果您使用responsive_framework: ^0.1.4 包并且遇到该问题,则修复方法如下

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter App',
      home: MyHomePage(),
      builder: (context, child) {
        return Overlay(
          initialEntries: [
            OverlayEntry(
              builder: (context) {
                return ResponsiveWrapper.builder(
                  MyHomePage(),
                  defaultScale: true,
                  breakpoints: [
                    ResponsiveBreakpoint.autoScale(1000),
                  ],
                );
              },
            ),
          ],
        );
      },
    );
  }
}
Run Code Online (Sandbox Code Playgroud)


小智 -2

我通过使用 home 而不是构建器参数解决了这个问题

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      ... //basic info title & theme

      home: LayoutTemplate(child: child),
      initialRoute:"/home",

      ... //Routing stuff like generate route & navigator key 
    );
  }
}
Run Code Online (Sandbox Code Playgroud)