【问题标题】:The following assertion was thrown while handling a gesture: Navigator operation requested with a context that does not include a Navigator处理手势时引发以下断言:使用不包含导航器的上下文请求导航器操作
【发布时间】:2021-01-14 08:42:41
【问题描述】:

我声明了一个类,返回 MaterialApp 并在其中使用按钮也使用 Navigator.Push 方法导航到不同的页面,但它给出了异常

使用不包含 导航器

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
            SafeArea(
            child: Scaffold(
            backgroundColor: Colors.red[100],
            body:Column(
              children: [
                   SizedBox(height: 50,),
                  Align(
                    alignment:Alignment.center,
                     child:Image.asset("assets/icons/appicon.png",
                   height:150),
                   ),
                   
                   Align(
                     alignment: Alignment.bottomCenter,
                     child: RaisedButton(  
  
                             child: Text('Click Picture'),  
  
                             color: Colors.red[800],  
  
                               onPressed: () {
  
                                            Navigator.push(
  
                                            context,
  
                                            MaterialPageRoute(builder: (context) =>  CameraRoute()
                                            ),
                                            );
  
                               },  
  
                             
  
                     ),
                   )
              ],
             
          ),
              
            ),
                        
        )
);
  

    //throw UnimplementedError();
  }
}

【问题讨论】:

标签: flutter flutter-navigation


【解决方案1】:

通过创建一个新的小部件将页面/屏幕与MyApp 分开。

像这样

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Home(),
    );
  }
}

class Home extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: Center(
          child: Column(
            children: [
              SizedBox(height: 100),
              Image.asset(
                "assets/icons.appicon.png",
                height: 150,
              ),
              RaisedButton(
                child: Text("Click Picture"),
                color: Colors.red,
                onPressed: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(
                      builder: (context) => CameraRoute(),
                    ),
                  );
                },
              ),
            ],
          ),
        ),
      ),
    );
  }
}

【讨论】:

    【解决方案2】:

    您正在尝试从没有 Navigator 作为父级的上下文中获取 Navigator。代码中唯一的导航器在 MaterialApp 中,但它是上下文的子级。

    获取作为 MaterialApp 子级的上下文的最小方法是使用 Builder 包装您的 Scaffold。

    如另一个答案所述,更好的方法是将您的代码拆分为更多小部件(每个小部件的构建方法公开一个上下文)。

    【讨论】:

    • 感谢@spkersten,您的回答很有帮助
    猜你喜欢
    • 2021-09-12
    • 2021-02-23
    • 2020-09-01
    • 2019-05-16
    • 2019-04-01
    • 2021-09-18
    • 1970-01-01
    • 2018-08-31
    • 1970-01-01
    相关资源
    最近更新 更多