flutter Navigator.pop(上下文)后,关闭抽屉自动返回MaterialApp和支架黑屏;

juzqafwq  于 2022-12-14  发布在  Flutter
关注(0)|答案(3)|浏览(155)

Hy家伙我试图尝试关闭抽屉内材料应用程序,但它是不工作.我的代码:

@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);去黑屏,看不到谷歌Map。任何想法?

guicsvcw

guicsvcw1#

我假设你是直接从你运行的应用程序调用这个小部件,因为它是导致错误。
你的应用程序也在错误的地方。
checkout 以下代码。

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),
            ),
          );
  }
}
cbjzeqam

cbjzeqam2#

问题是您不是在弹出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所在的页面上下文。

q0qdq0h2

q0qdq0h23#

除了调用navigator.pop,您还可以使用

Navigator.of(context).maybePop();

相关问题