Navigator.pop(context)引发异常

时间:2018-07-24 14:10:58

标签: android dart flutter drawer navigator

我试图实现同时包含抽屉和TabBar演示的UI,为此,我尝试了以下操作:-

        import 'package:flutter/material.dart';

void main() => runApp(new NewApp());

class NewApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      home: DefaultTabController(
        length: 3,
        child: Scaffold(
          drawer: Drawer(
            child: ListView(

              padding: EdgeInsets.zero,
              children: <Widget>[
                DrawerHeader(
                  child: Text("Drawer Header"),
                  decoration: BoxDecoration(
                    color: Colors.blue,
                  ),
                ),
                ListTile(
                  title: Text("Item1"),
                  onTap: (){
                    Navigator.pop(context);
                  },
                ),
                ListTile(
                  title: Text("Item2"),
                  onTap: (){
                    Navigator.pop(context);
                  },
                ),

              ],
            ),
          ),
          appBar: AppBar(
            bottom: TabBar(
              tabs: [
                Tab(icon: Icon(Icons.room),text: "Room",),
                Tab(icon: Icon(Icons.restaurant),text: "Restaurant",),
                Tab(icon: Icon(Icons.school),text: "School"),
              ],
            ),
            title: Text("Tab Bar View + Drawer"),
          ),
          body: TabBarView(
            children: [
              Icon(Icons.room),
              Icon(Icons.restaurant),
              Icon(Icons.school),
            ],
          ),
        ),
      ),
    );
     }
     }

现在,这给了我一个错误:              引发了另一个异常:请求导航器操作并带有              不包含导航器的上下文。

如果有人有解决方案,请帮助清除它。 谢谢。

1 个答案:

答案 0 :(得分:0)

我不知道错误/修复的原因(我所做的和起作用的)。有人,请解释一下它是如何工作的。我只是将DefaultTabController包装到另一个无状态小部件Home中。工作正常。请参考下面的代码。

import 'package:flutter/material.dart';

void main() => runApp(new NewApp());

class NewApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      home: Home(),
    );
  }
}

class Home extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
      length: 3,
      child: Scaffold(
        drawer: Drawer(
          child: ListView(

            padding: EdgeInsets.zero,
            children: <Widget>[
              DrawerHeader(
                child: Text("Drawer Header"),
                decoration: BoxDecoration(
                  color: Colors.blue,
                ),
              ),
              ListTile(
                title: Text("Item1"),
                onTap: (){
                  Navigator.pop(context);
                },
              ),
              ListTile(
                title: Text("Item2"),
                onTap: (){
                  Navigator.pop(context);
                },
              ),

            ],
          ),
        ),
        appBar: AppBar(
          bottom: TabBar(
            tabs: [
              Tab(icon: Icon(Icons.room),text: "Room",),
              Tab(icon: Icon(Icons.restaurant),text: "Restaurant",),
              Tab(icon: Icon(Icons.school),text: "School"),
            ],
          ),
          title: Text("Tab Bar View + Drawer"),
        ),
        body: TabBarView(
          children: [
            Icon(Icons.room),
            Icon(Icons.restaurant),
            Icon(Icons.school),
          ],
        ),
      ),
    );
    // TODO: implement build
  }

}