![](/img/trans.png)
[英]Too many positional arguments error when trying to use openDrawer() using Scaffold.of(context) finding 2 instead of 0
[英]Flutter Scaffold.of(context).openDrawer() doesn't work
按下 BottomMenu 中的自定义按钮后,我想打开一个抽屉我遇到了 Scaffold.of(context).openDrawer() 的问题,它不起作用。 My 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();
},
),
],
),
),
],
),
);
}
}
就我而言,这行得通。
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 下。
我的问题解决了,而不是
Scaffold.of(context).openEndDrawer()
我给 Scaffold 钥匙,然后通过 state 调用,如下所示
_scaffoldkey.currentState.openEndDrawer()
它解决了我的问题我希望它也适用于你
问题是您在Scaffold
上指定endDrawer
,但您正在调用Scaffold.of(context).openDrawer()
。
openDrawer()
文档状态:
如果脚手架有一个非空的 Scaffold.drawer,这个 function 将导致抽屉开始其入口 animation。
由于您的drawer
是 null,因此没有任何反应。
相反, openEndDrawer()
告诉我们:
如果脚手架有一个非空的 Scaffold.endDrawer,这个 function 将导致端侧抽屉开始其入口 animation。
由于您的endDrawer
不是 null 您应该使用openEndDrawer()
方法。 或者,如果您不关心抽屉从哪一侧滑入,您可以在构建Scaffold
时使用drawer
而不是endDrawer
。
Scaffold.of(context).openEndDrawer()
这里有类似的问题。 单击按钮,没有任何反应。 问题是我正在使用实例化 Scaffold 的小部件的上下文。 不是 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(),
),
],
),
],
)),
),
问题
如果在调用Scaffold.of(context).openDrawer()
(或openEndDrawer()
)时未使用正确的BuildContext
,则可能会出现此问题。
最简单的解决方案
只需使用Builder
小部件包装任何调用openDrawer()
(或openEndDrawer()
)。 这将给它一个工作context
。
最小的工作示例
// your build method
@override
Widget build(BuildContext context) {
return Scaffold(
floatingActionButton: Builder(builder: (context) { // this uses the new context to open the drawer properly provided by the Builder
return FloatingActionButton(onPressed: (() => Scaffold.of(context).openDrawer()));
}),
drawer: const Drawer(
child: Text("MY DRAWER"),
),
);
}
将 Drawer 分配给脚手架中的抽屉属性。 使用 Builder 包装您的特定小部件/按钮(您希望在其单击方法上打开抽屉的位置)。 在点击属性上使用以下方法:在此处输入图像描述Scaffold.of(context).openDrawer();
如果您有带有操作按钮的 appbar 小部件来启动抽屉并且抽屉永远不会被推动,请记住您需要在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(
///...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.