Flutter 颤振应用程序抽屉导航到新页面
为了寻找一种更好的方法来实现从应用程序抽屉到下一页的导航,我在其他文件中制作了一个有状态的小部件,并将其导入main.dart,而不是Flutter 颤振应用程序抽屉导航到新页面,flutter,flutter-layout,android-studio-3.4,Flutter,Flutter Layout,Android Studio 3.4,为了寻找一种更好的方法来实现从应用程序抽屉到下一页的导航,我在其他文件中制作了一个有状态的小部件,并将其导入main.dart,而不是 Navigate.pop(context); 我用什么 我试过了 Navigator.of(context).push( MaterialPageRoute<Null>(builder: (BuildContext context) { return new HomePage(); Navigator.of(context
Navigate.pop(context);
我用什么
我试过了
Navigator.of(context).push(
MaterialPageRoute<Null>(builder: (BuildContext context) {
return new HomePage();
Navigator.of(context).push(
MaterialPage路由(生成器:(构建上下文){
返回新主页();
它将页面加载到上一页上,并使内容滞后
下面是代码
return Scaffold(
appBar: AppBar(title: Text(title)),
body: Center(child: Text('some text')),
drawer: Drawer(
// Add a ListView to the drawer. This ensures the user can scroll
// through the options in the drawer if there isn't enough vertical
// space to fit everything.
child: ListView(
// Important: Remove any padding from the ListView.
padding: EdgeInsets.zero,
children: <Widget>[
DrawerHeader(
child: Text('Drawer Header'),
decoration: BoxDecoration(
color: Colors.blue,
image: DecorationImage(image: AssetImage("assets/gold.jpg"),fit: BoxFit.cover)
),
),
ListTile(
title: Text('Item 1'),
onTap: () {
// Update the state of the app
// ...
// Then close the drawer
Navigator.pop(context);
},
),
ListTile(
title: Text('Item 2'),
onTap: () {
// Update the state of the app
// ...
// Then close the drawer // how do i close the drawer after click?
Navigator.pop(context);
},
),
],
),
),
);
返回脚手架(
appBar:appBar(标题:文本(标题)),
正文:居中(子:文本(“某些文本”),
抽屉(
//向抽屉添加列表视图。这确保用户可以滚动
//如果没有足够的垂直线,请通过抽屉中的选项
//空间适合所有东西。
子:ListView(
//重要提示:从ListView中删除任何填充。
填充:EdgeInsets.zero,
儿童:[
抽屉阅读器(
子项:文本(“抽屉标题”),
装饰:盒子装饰(
颜色:颜色,蓝色,
图片:DecorationImage(图片:AssetImage(“assets/gold.jpg”),fit:BoxFit.cover)
),
),
列表砖(
标题:文本(“项目1”),
onTap:(){
//更新应用程序的状态
// ...
//然后关上抽屉
Navigator.pop(上下文);
},
),
列表砖(
标题:文本(“项目2”),
onTap:(){
//更新应用程序的状态
// ...
//然后关闭抽屉//单击后如何关闭抽屉?
Navigator.pop(上下文);
},
),
],
),
),
);
我希望当我点击app drawer链接时,它会将我带到一个新页面,并关闭app drawer本身,如果您正在寻找一种方法来编辑当前页面,例如选项卡,然后在视图之间切换,而不实际启动新页面路由 我通常做的是:
enum Section
{
GUEST,
HOME,
PAGE_1,
PAGE_2
}
您的主要构建功能:
@override
Widget build(BuildContext context)
{
Widget body;
/// You can easily control the section for example inside the initState where you check
/// if the user logged in, or other related logic
switch (section)
{
/// This is for example a login page since the user not logged in
case Section.GUEST:
break;
/// Display the home section, simply by
case Section.HOME:
body = HomeSection();
break;
case Section.PAGE_1:
body = Page1Section();
break;
case Section.PAGE_2:
body = Page2Section();
break;
}
return Scaffold(
body: Container(
child: body,
),
/// Display the drawer for logged in users only
drawer: section != Section.GUEST ? Drawer(
// Your drawer
) : null,
);
}
这甚至可以保存这些部分的状态,并且可以在它们之间快速移动
重新加载抽屉,你做得很好。你只需在上下文上使用导航器弹出。只需确保你有正确的上下文。(而不是传播的上下文)
当然,更改部分很简单,如下所示:
setState(() => section = Section.HOME);
如果您正在寻找一种方法来编辑当前页面,例如选项卡,然后在视图之间切换,而不实际启动新的页面路由 我通常做的是:
enum Section
{
GUEST,
HOME,
PAGE_1,
PAGE_2
}
您的主要构建功能:
@override
Widget build(BuildContext context)
{
Widget body;
/// You can easily control the section for example inside the initState where you check
/// if the user logged in, or other related logic
switch (section)
{
/// This is for example a login page since the user not logged in
case Section.GUEST:
break;
/// Display the home section, simply by
case Section.HOME:
body = HomeSection();
break;
case Section.PAGE_1:
body = Page1Section();
break;
case Section.PAGE_2:
body = Page2Section();
break;
}
return Scaffold(
body: Container(
child: body,
),
/// Display the drawer for logged in users only
drawer: section != Section.GUEST ? Drawer(
// Your drawer
) : null,
);
}
这甚至可以保存这些部分的状态,并且可以在它们之间快速移动
重新加载抽屉,你做得很好。你只需在上下文上使用导航器弹出。只需确保你有正确的上下文。(而不是传播的上下文)
当然,更改部分很简单,如下所示:
setState(() => section = Section.HOME);