我正在构建一个管理面板,现在当我运行代码时没有错误,但抽屉不工作
我找过问题,但找不到
这些是我用多个支架键创建的控制器
class MenuController extends ChangeNotifier {
final GlobalKey<ScaffoldState> _scaffoldkey = GlobalKey<ScaffoldState>();
final GlobalKey<ScaffoldState> _gridScaffoldkey = GlobalKey<ScaffoldState>();
final GlobalKey<ScaffoldState> _addProductsScaffoldkey =
GlobalKey<ScaffoldState>();
GlobalKey<ScaffoldState> get getScaffoldkey => _scaffoldkey;
GlobalKey<ScaffoldState> get getGridScaffoldkey => _gridScaffoldkey;
GlobalKey<ScaffoldState> get getAddProductsScaffoldkey =>
_addProductsScaffoldkey;
void controlDashboardMenu() {
if (_scaffoldkey.currentState!.isDrawerOpen) {
_scaffoldkey.currentState!.openDrawer();
}
}
void controlProductsMenu() {
if (_gridScaffoldkey.currentState!.isDrawerOpen) {
_gridScaffoldkey.currentState!.openDrawer();
}
}
void controlAddProductsMenu() {
if (_addProductsScaffoldkey.currentState!.isDrawerOpen) {
_addProductsScaffoldkey.currentState!.openDrawer();
}
}
}
这就是我在抽屉小部件中的函数内部调用它的地方我把它传给了这个小部件
class DashboardScreen extends StatelessWidget {
const DashboardScreen({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return SafeArea(
child: SingleChildScrollView(
child: Column(
children: [
Header(fuc: () {
context.read<MenuController>().controlDashboardMenu();
}),
Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Expanded(
child: Column(
children: [ProductsWidget()],
),
),
],
),
],
),
),
);
}
}
在这里我调用支架内的密钥,因为这是我的主屏幕
class MainScreen extends StatelessWidget {
const MainScreen({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
key: context.read<MenuController>().getScaffoldkey,
drawer: const SideMenu(),
body: SafeArea(
child: Row(
children: [
if (Responsive.isDesktop(context))
const Expanded(
child: SideMenu(),
),
const Expanded(
flex: 5,
child: DashboardScreen(),
),
],
),
),
);
}
}
添加应用程序栏打开导航抽屉,使用此代码Scaffold.of(context).openDrawer();
修复
return Scaffold(
key: context.read<MenuController>().getScaffoldkey,
drawer: const SideMenu(),
appBar: AppBar(
backgroundColor: colors_yellow,
elevation: 0,
leading: Builder(
builder: (BuildContext context) {
return IconButton(
icon: const Icon(Icons.menu),
onPressed: () {
Scaffold.of(context).openDrawer();
},
tooltip: MaterialLocalizations.of(context).openAppDrawerTooltip,
);
},
),
title: const Text(
'Title ',
style: TextStyle(color: Colors.white),
),
),
抽屉缺少AppBar
?
也许可以将以下内容添加到您的Scaffold
定义中?
appBar: AppBar(title: Text(title)),