嗨,我正在尝试关闭材质应用内的抽屉,但是它不起作用。我的代码:
@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地图。有想法吗?
答案 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上下文。