flutter - Flutter 无法添加抽屉
问题描述
我发现在最新版本的颤振中添加抽屉很困难。我的代码看起来像这样
class HomeScreen extends StatelessWidget {
const HomeScreen({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: buildAppbar(),
body: const Body(),
drawer: DrawerSlide(),
);
}
AppBar buildAppbar() {
return AppBar(
elevation: 3,
leading: GestureDetector(
child: Padding(
padding: const EdgeInsets.all(12.0),
child: SvgPicture.asset('assets/icons/menu.svg', width: 10, height: 10, fit: BoxFit.fitWidth,),
),
onTap: () => Scaffold.of(context).openDrawer(),
),
);
}
}
Scaffold.of(context).openDrawe() 中显示上下文未定义的错误。在我更新颤振之前它一直有效,但在新的颤振中我认为它不起作用。
解决方案
The buildAppbar
method requires context
as a positional argument :
AppBar buildAppbar(BuildContext context) {
return AppBar(
elevation: 3,
leading: GestureDetector(
child: Padding(
padding: const EdgeInsets.all(12.0),
child: SvgPicture.asset('assets/icons/menu.svg', width: 10, height: 10, fit: BoxFit.fitWidth,),
),
onTap: () => Scaffold.of(context).openDrawer(),
),
);
}
use it like :
return Scaffold(
appBar: buildAppbar(context),
body: const Body(),
drawer: DrawerSlide(),
);
推荐阅读
- python - 如何以以下打印格式下载 CSV 文件中的以下抓取数据?
- azure-functions - 如何从 azure app 设置或 azure function app 文件中使用 Serilog 配置?
- c - 访问 argv[1] 会阻止程序
- reactjs - react-router-dom 多布局配置不显示路由指示的组件
- html - 在Jquery中获取特定表行数据
- sql-server - 如何将表连接到联合查询的结果
- git - 合并后master分支丢失文件,文件在git合并输出中报告
- c# - 如何在 Unity 中为特定的克隆游戏对象播放粒子?
- performance-testing - 无法在虚拟用户生成器中打开新的/现有的 LoadRunner 脚本
- objective-c - 为什么类对象可以调用“methodSignatureForSelector:”的实例方法