我正在尝试在 WebApp 中创建管理面板。原生抽屉在打开时完全覆盖内容,我想让它在单击图标时始终打开和关闭。如何制作隐藏/显示功能?编码:
class AdminPage extends StatefulWidget {
@override
_AdminPage createState() => _AdminPage();
}
bool isColapsed = true;
class _AdminPage extends State<AdminPage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
leading: Builder(
builder: (context) => IconButton(
icon: Icon(Icons.menu),
onPressed: () => Scaffold.of(context).openEndDrawer())),
backgroundColor: Colors.deepOrange,
actions: <Widget>[IconButton(icon: Icon(Icons.menu), onPressed: () {})],
),
body: Stack(
children: <Widget>[
Container(
color: Colors.amberAccent,
child: Center(
child: Text('content'),
),
),
],
),
);
}
}
重新设计的小部件
class SideMenu extends StatefulWidget {
@override
_SideMenuState createState() => _SideMenuState();
}
bool isSideBarOpened = true;
final _animationDuration = const Duration(milliseconds: 500);
class _SideMenuState extends State<SideMenu> {
Widget widgetForBody = FirstPage();
@override
Widget build(BuildContext context) {
final screenWidth = MediaQuery.of(context).size.width;
return AnimatedPositioned(
duration: _animationDuration,
top: 0,
bottom: 0,
left: isSideBarOpened ? 0 : -300,
right: isSideBarOpened ? 0 : screenWidth - 35,
child: Row(
children: <Widget>[
Flexible(
child: Container(
width: 300.0,
color: Colors.deepOrange,
child: Column(
children: <Widget>[
ListTile(
title: Text('First Page'),
onTap: () {
setState(() {
widgetForBody = FirstPage();
});
},
),
],
),
),
),
Align(
alignment: Alignment(0, -0.9),
child: Container(
width: 35,
height: 100,
color: Colors.deepOrange,
child: IconButton(
icon: Icon(Icons.menu),
onPressed: () {
setState(() {
isSideBarOpened = !isSideBarOpened;
});
},
),
),
),
],
),
);
}
}
其实这已经不是 Drawer 了,而是它自己实现的菜单……你可以在DartPad中查看。
Drawer 的出现和隐藏动画的实现。
代码也可以在dartpad中播放