gpt4 book ai didi

flutter - 显示 ModalBottomSheet 的脚手架抽屉

转载 作者:IT王子 更新时间:2023-10-29 07:15:16 27 4
gpt4 key购买 nike

myApp()的主屏幕上我有一个无状态的小部件,它包含一个 MaterialApp和一个 Scaffold .脚手架的属性为 drawer然后我创建了一个抽屉,抽屉中的其中一件元素需要打开 showModalBottomSheet同时关闭 drawer .我怎样才能做到这一点?我试过通过 context本身,并作为 globalKey.currentContext (在 GlobalKey<ScaffoldState> globalKey = GlobalKey(); 之后)但抽屉有时会关闭,其他时候会给我一个 NoMethodFoundException (或类似的东西)

简而言之,如何拥有Scaffold drawer具有其中一项的,点击时关闭 drawershowModalBottomSheet

当前代码:

class Timeline extends StatelessWidget {
@override
Widget build(BuildContext context) {
GlobalKey<ScaffoldState> homeScaffoldKey = GlobalKey();

return MaterialApp(
title: "Test",
theme: ThemeData(
appBarTheme: AppBarTheme(iconTheme: IconThemeData(color: Colors.black)),
),
home: Scaffold(
key: homeScaffoldKey,
drawer: showDrawer(homeScaffoldKey.currentContext),
backgroundColor: Colors.grey[100],
body: Stack(
children: <Widget>[
HomePageView(),
AppBar(
elevation: 0,
backgroundColor: Colors.transparent,
),
],
),
),
);
}
}

Drawer showDrawer(BuildContext context) {
void showCalendarsModalBottom() {
showModalBottomSheet(
context: context,
builder: (BuildContext builder) {
return ListView.builder(
itemCount: repo.calendars.length,
itemBuilder: (builder, index) {
return StatefulBuilder(
builder: (builder, StateSetter setState) => ListTile(
leading: Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Checkbox(
value: repo.getIsEnabledCal(repo.getCal(index)),
onChanged: (value) {
setState(() {
repo.toggleCalendar(repo.getCal(index));
});
},
),
Container(
height: 14,
width: 14,
margin: EdgeInsets.only(left: 2, right: 6),
decoration: BoxDecoration(
color: Colors.redAccent,
shape: BoxShape.circle,
),
),
Text(
repo.getCal(index).name,
style: TextStyle(
fontSize: 16,
),
),
],
),
onTap: () {
setState(() {
repo.toggleCalendar(repo.getCal(index));
});
},
),
);
},
);
},
);
}

return Drawer(
child: ListView(
children: <Widget>[
DrawerHeader(
child: Align(
child: Text('Timeline', textScaleFactor: 2),
alignment: Alignment.bottomLeft,
),
),
ListTile(
title: Text('Dark Mode'),
onTap: () => Navigator.pop(context),
),
ListTile(
title: Text('Calenders'),
onTap: () {
Navigator.pop(context);

showCalendarsModalBottom();
},
)
],
),
);
}

最佳答案

根据您的代码片段更新了工作代码:

您需要有 statefulwidget,它有助于将上下文从抽屉传递到 bottomsheet,并将 context 作为 showCalendarModalBottomSheet()< 中的参数传递 方法。

void main() {
runApp(new MaterialApp(home: Timeline(), debugShowCheckedModeBanner: false));
}

class Timeline extends StatelessWidget {
@override
Widget build(BuildContext context) {

return MaterialApp(
title: "Test",
theme: ThemeData(
appBarTheme: AppBarTheme(iconTheme: IconThemeData(color: Colors.black)),
),
home: MyHomePage()
);
}
}

class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {

@override
Widget build(BuildContext context) {
return Scaffold(
drawer: AppDrawer(),
backgroundColor: Colors.grey[100],
body: Stack(
children: <Widget>[
//HomePageView(),
AppBar(
elevation: 0,
backgroundColor: Colors.transparent,
)
],
)
);
}

Widget AppDrawer() {
return Drawer(
child: ListView(
children: <Widget>[
DrawerHeader(
child: Align(
child: Text('Timeline', textScaleFactor: 2),
alignment: Alignment.bottomLeft,
),
),
ListTile(
title: Text('Dark Mode'),
onTap: () => Navigator.pop(context),
),
ListTile(
title: Text('Calenders'),
onTap: () {
Navigator.of(context).pop();
showCalendarsModalBottom(context);
},
)
],
),
);
}

Future<Null> showCalendarsModalBottom(context) {
return showModalBottomSheet(context: context, builder: (context) => Container(
color: Colors.red,
// your code here
));
}
}

输出是:当点击应用程序抽屉菜单日历时,它会无缝关闭和打开底页。如果您再次点击应用程序抽屉并重复这些步骤,您会看到抽屉和底页之间的平滑过渡。希望这能回答您的问题。

enter image description here

enter image description here

关于flutter - 显示 ModalBottomSheet 的脚手架抽屉,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57567202/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com