关闭抽屉后,Navigator.pop(context)之后会自动返回MaterialApp和Scaffold黑屏;

时间:2020-02-21 11:08:38

标签: google-maps flutter dart

嗨,我正在尝试关闭材质应用内的抽屉,但是它不起作用。我的代码:

@override
  Widget build(BuildContext context) {
   return MaterialApp(
    home: currentLocation == null ? Container(
      alignment: Alignment.center,
      child: Center(
        child: CircularProgressIndicator(),
      ),
    ):

    Scaffold(
        drawer: Drawer(
            child: ListView(
               children: <Widget>[
                ListTile(
                  leading: CircleAvatar(),
                  title: Text("test app"),
                  subtitle: Text("nn"),
                ),
                ListTile(leading: Icon(Icons.multiline_chart), title: Text("check gps.."),
                    onTap: () { 
                   _checkgps();
                     Navigator.pop(context);
                    }
                    ),
           appBar: AppBar(
          title: Text('test app'),

        ),
        body:  GoogleMap(

          initialCameraPosition:
          CameraPosition(target: LatLng(currentLocation.latitude,
              currentLocation.longitude), zoom: 17),
          onMapCreated: _onMapCreated,
          mapType: _currentMapType,
          compassEnabled: true,
          myLocationEnabled: true,
          polylines: Set<Polyline>.of(_mapPolylines.values),
          markers: Set<Marker>.of(markers.values),
         )

       );

      }

但是当我按列表项1(checkgps)Navigator.pop(context);进入黑屏时,看不到Google地图。有想法吗?

2 个答案:

答案 0 :(得分:0)

我假设您是直接从运行应用程序调用此小部件的,因此会导致错误。

您的应用栏也位于错误的位置。

在代码下方签出。

void main() {
  runApp(MyApp());
}


class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return  MaterialApp(
        title: 'Flutter Demo',
        home: Scaffold(body: DeleteWidget()),
    );
  }
}

class DeleteWidget extends StatefulWidget {
  const DeleteWidget({Key key}) : super(key: key);

  @override
  _DeleteWidgetState createState() => _DeleteWidgetState();
}

class _DeleteWidgetState extends State<DeleteWidget> {
  @override
  Widget build(BuildContext context) {
    return currentLocation == null
        ? Container(
            alignment: Alignment.center,
            child: Center(
              child: CircularProgressIndicator(),
            ),
          )
        : Scaffold(
            drawer: Drawer(
              child: ListView(children: <Widget>[
                ListTile(
                  leading: CircleAvatar(),
                  title: Text("test app"),
                  subtitle: Text("nn"),
                ),
                ListTile(
                    leading: Icon(Icons.multiline_chart),
                    title: Text("check gps.."),
                    onTap: () {
                      _checkgps();
                      Navigator.pop(context);
                    }),
              ]),
            ),
            appBar: AppBar(
              title: Text('test app'),
            ),
            body: GoogleMap(
              initialCameraPosition: CameraPosition(
                  target: LatLng(
                      currentLocation.latitude, currentLocation.longitude),
                  zoom: 17),
              onMapCreated: _onMapCreated,
              mapType: _currentMapType,
              compassEnabled: true,
              myLocationEnabled: true,
              polylines: Set<Polyline>.of(_mapPolylines.values),
              markers: Set<Marker>.of(markers.values),
            ),
          );
  }
}

答案 1 :(得分:0)

问题是您没有弹出Drawer的上下文,而是弹出MaterialApp的上下文。

将应用程序拆分为小部件也是一个好主意,因此您的抽屉内容必须放在另一个小部件中。我进行了这些更改,请尝试以下代码:

void main() {
    runApp(MyApp());
}


class MyApp extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
        return  MaterialApp(
            title: 'Flutter Demo',
            home: Scaffold(body: DeleteWidget()),
        );
    }
}

class DeleteWidget extends StatefulWidget {
    const DeleteWidget({Key key}) : super(key: key);

    @override
    _DeleteWidgetState createState() => _DeleteWidgetState();
}

class _DeleteWidgetState extends State<DeleteWidget> {
    @override
    Widget build(BuildContext context) {
        return currentLocation == null
                ? Container(
            alignment: Alignment.center,
            child: Center(
                child: CircularProgressIndicator(),
            ),
        )
                : Scaffold(
            drawer: _DrawerContent(),
            appBar: AppBar(
                title: Text('test app'),
            ),
            body: GoogleMap(
                initialCameraPosition: CameraPosition(
                        target: LatLng(
                                currentLocation.latitude, currentLocation.longitude),
                        zoom: 17),
                onMapCreated: _onMapCreated,
                mapType: _currentMapType,
                compassEnabled: true,
                myLocationEnabled: true,
                polylines: Set<Polyline>.of(_mapPolylines.values),
                markers: Set<Marker>.of(markers.values),
            ),
        );
    }
}

class _DrawerContent extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Drawer(
            child: ListView(children: <Widget>[
                ListTile(
                    leading: CircleAvatar(),
                    title: Text("test app"),
                    subtitle: Text("nn"),
                ),
                ListTile(
                        leading: Icon(Icons.multiline_chart),
                        title: Text("check gps.."),
                        onTap: () {
                            _checkgps();
                            Navigator.pop(context);
                        }),
            ]),
        );
  }
}

在调用Drawer时将Navigator.pop(context);内容放在另一个小部件中,它将弹出抽屉上下文,而不是Drawer所在的Page上下文。