Flutter Scaffold.of(context).openDrawer() 不起作用
Flutter Scaffold.of(context).openDrawer() doesn't work
我想在按下 BottomMenu 中的自定义按钮后打开一个抽屉我遇到了 Scaffold.of(context).openDrawer() 的问题,它不起作用。我的 BottomMenu 是一个 单独的 小部件 class。据我了解,它不起作用,因为它是一个单独的上下文。我怎样才能得到正确的上下文?或者也许有人知道另一种解决方案。
这里是我的代码复制者:
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
home: MyHomePage(title: 'Flutter Drawer'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
bottomNavigationBar: BottomMenu(),
endDrawer: SizedBox(
width: double.infinity,
child: Drawer(
elevation: 16,
child: Container(
color: Colors.black,
child: ListView(
padding: EdgeInsets.zero,
children: <Widget>[
ListTile(
title: Text('Some context here',
style: TextStyle(color: Colors.white))),
ListTile(
title: Text('Some context here',
style: TextStyle(color: Colors.white))),
],
),
),
),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'Call Drawer form menu reproducer',
)
],
),
),
);
}
}
class BottomMenu extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 15),
child: Wrap(
alignment: WrapAlignment.center,
children: <Widget>[
Divider(color: Colors.black, height: 1),
Padding(
padding: const EdgeInsets.symmetric(vertical: 2),
child: Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
InkWell(
borderRadius: new BorderRadius.circular(20.0),
customBorder: Border.all(color: Colors.black),
child: Container(
padding: EdgeInsets.only(
left: 3, right: 6, bottom: 15, top: 11),
child: Row(
children: <Widget>[
Icon(Icons.menu),
Text('Show menu', style: TextStyle(fontSize: 15, fontWeight: FontWeight.bold)),
],
),
),
onTap: () {
Scaffold.of(context).openDrawer();
},
),
],
),
),
],
),
);
}
}
Scaffold.of(context).openEndDrawer()
问题是您在 Scaffold
上指定了 endDrawer
,但您正在调用 Scaffold.of(context).openDrawer()
。
openDrawer()
文档状态:
If the scaffold has a non-null Scaffold.drawer, this function will cause the drawer to begin its entrance animation.
由于您的 drawer
为 null,因此没有任何反应。
相比之下,openEndDrawer()
告诉我们:
If the scaffold has a non-null Scaffold.endDrawer, this function will cause the end side drawer to begin its entrance animation.
由于您的 endDrawer
不为空,您应该使用 openEndDrawer()
方法。或者,如果您不关心抽屉从哪一侧滑入,则可以在构建 Scaffold
.
时使用 drawer
而不是 endDrawer
我的问题解决了
Scaffold.of(context).openEndDrawer()
我把钥匙交给脚手架,然后我按如下状态调用
_scaffoldkey.currentState.openEndDrawer()
它解决了我的问题我希望它也适用于你
这里有类似的问题。单击按钮并没有任何反应。问题是我正在使用实例化脚手架的小部件的上下文。不是 Scaffold 子项的上下文。
我是这样解决的:
// body: Column(
// children: <Widget>[
// Row(
// children: <Widget>[
// IconButton(
// icon: Icon(Icons.filter_list),
// onPressed: () => Scaffold.of(context).openEndDrawer(), (wrong context)
// ),
// ],
// ),
// ],
// )
收件人:
body: Builder(
builder: (context) => Column(
children: <Widget>[
Row(
children: <Widget>[
IconButton(
icon: Icon(Icons.filter_list),
onPressed: () => Scaffold.of(context).openEndDrawer(),
),
],
),
],
)),
),
就我而言,这很有效。
return Scaffold(
key: _scaffoldKey,
endDrawerEnableOpenDragGesture: false, // This!
appBar: AppBar(
iconTheme: IconThemeData(color: Colors.white),
leading: IconButton(
icon: Icon(Icons.menu, size: 36),
onPressed: () => _scaffoldKey.currentState.openDrawer(), // And this!
),
),
drawer: DrawerHome(),
....
和_scaffoldKey
必须初始化为,
final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
下class.
如果您的应用栏小部件带有用于启动抽屉的操作按钮并且抽屉永远不会被推动,请记住您需要在 appbar: ...
之后定义 endDrawer: YOURAppDrawerWIDGET(),
否则使用 Scaffold.of(context).openEndDrawer()
将不起作用。
Scaffold(
appBar: AppBar(title: Text(_title)),
endDrawer: AppDrawer(), // <-- this is required or else it will not know what is opening
body: SingleChildScrollView(
///...
我想在按下 BottomMenu 中的自定义按钮后打开一个抽屉我遇到了 Scaffold.of(context).openDrawer() 的问题,它不起作用。我的 BottomMenu 是一个 单独的 小部件 class。据我了解,它不起作用,因为它是一个单独的上下文。我怎样才能得到正确的上下文?或者也许有人知道另一种解决方案。
这里是我的代码复制者:
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
home: MyHomePage(title: 'Flutter Drawer'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
bottomNavigationBar: BottomMenu(),
endDrawer: SizedBox(
width: double.infinity,
child: Drawer(
elevation: 16,
child: Container(
color: Colors.black,
child: ListView(
padding: EdgeInsets.zero,
children: <Widget>[
ListTile(
title: Text('Some context here',
style: TextStyle(color: Colors.white))),
ListTile(
title: Text('Some context here',
style: TextStyle(color: Colors.white))),
],
),
),
),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'Call Drawer form menu reproducer',
)
],
),
),
);
}
}
class BottomMenu extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 15),
child: Wrap(
alignment: WrapAlignment.center,
children: <Widget>[
Divider(color: Colors.black, height: 1),
Padding(
padding: const EdgeInsets.symmetric(vertical: 2),
child: Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
InkWell(
borderRadius: new BorderRadius.circular(20.0),
customBorder: Border.all(color: Colors.black),
child: Container(
padding: EdgeInsets.only(
left: 3, right: 6, bottom: 15, top: 11),
child: Row(
children: <Widget>[
Icon(Icons.menu),
Text('Show menu', style: TextStyle(fontSize: 15, fontWeight: FontWeight.bold)),
],
),
),
onTap: () {
Scaffold.of(context).openDrawer();
},
),
],
),
),
],
),
);
}
}
Scaffold.of(context).openEndDrawer()
问题是您在 Scaffold
上指定了 endDrawer
,但您正在调用 Scaffold.of(context).openDrawer()
。
openDrawer()
文档状态:
If the scaffold has a non-null Scaffold.drawer, this function will cause the drawer to begin its entrance animation.
由于您的 drawer
为 null,因此没有任何反应。
相比之下,openEndDrawer()
告诉我们:
If the scaffold has a non-null Scaffold.endDrawer, this function will cause the end side drawer to begin its entrance animation.
由于您的 endDrawer
不为空,您应该使用 openEndDrawer()
方法。或者,如果您不关心抽屉从哪一侧滑入,则可以在构建 Scaffold
.
drawer
而不是 endDrawer
我的问题解决了
Scaffold.of(context).openEndDrawer()
我把钥匙交给脚手架,然后我按如下状态调用
_scaffoldkey.currentState.openEndDrawer()
它解决了我的问题我希望它也适用于你
这里有类似的问题。单击按钮并没有任何反应。问题是我正在使用实例化脚手架的小部件的上下文。不是 Scaffold 子项的上下文。
我是这样解决的:
// body: Column(
// children: <Widget>[
// Row(
// children: <Widget>[
// IconButton(
// icon: Icon(Icons.filter_list),
// onPressed: () => Scaffold.of(context).openEndDrawer(), (wrong context)
// ),
// ],
// ),
// ],
// )
收件人:
body: Builder(
builder: (context) => Column(
children: <Widget>[
Row(
children: <Widget>[
IconButton(
icon: Icon(Icons.filter_list),
onPressed: () => Scaffold.of(context).openEndDrawer(),
),
],
),
],
)),
),
就我而言,这很有效。
return Scaffold(
key: _scaffoldKey,
endDrawerEnableOpenDragGesture: false, // This!
appBar: AppBar(
iconTheme: IconThemeData(color: Colors.white),
leading: IconButton(
icon: Icon(Icons.menu, size: 36),
onPressed: () => _scaffoldKey.currentState.openDrawer(), // And this!
),
),
drawer: DrawerHome(),
....
和_scaffoldKey
必须初始化为,
final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
下class.
如果您的应用栏小部件带有用于启动抽屉的操作按钮并且抽屉永远不会被推动,请记住您需要在 appbar: ...
之后定义 endDrawer: YOURAppDrawerWIDGET(),
否则使用 Scaffold.of(context).openEndDrawer()
将不起作用。
Scaffold(
appBar: AppBar(title: Text(_title)),
endDrawer: AppDrawer(), // <-- this is required or else it will not know what is opening
body: SingleChildScrollView(
///...