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



Hy Guys我试图关闭材料应用程序内的抽屉,但它不起作用。我的代码:

@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);会进入黑屏,看不到谷歌地图。知道吗?

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

你的应用程序栏也在错误的位置。

在代码下方签出。

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

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

您可以使用而不是调用navigator.pop

Navigator.of(context).maybePop();

最新更新