【发布时间】:2017-10-04 01:37:26
【问题描述】:
点击 Scaffold 抽屉中的某个项目后,我希望它自动隐藏。如何在 Flutter 中做到这一点?
【问题讨论】:
点击 Scaffold 抽屉中的某个项目后,我希望它自动隐藏。如何在 Flutter 中做到这一点?
【问题讨论】:
Navigator.pop() 会将Drawer 路由从堆栈中弹出并使其关闭。
【讨论】:
pop 的调用重构为共享私有方法。
Navigator.pop() 在我的情况下没有关闭抽屉......(Navigator.pop(context) 或 Navigator.pop(context, true) 也不这样做......我需要做什么为了通过选择其中一项来关闭抽屉?我的抽屉在 MaterialApp 内的 Scaffold 内...这有关系吗??
Navigator.of(context).pop() 应该做你想做的:)
https://docs.flutter.io/flutter/widgets/Navigator-class.html https://docs.flutter.io/flutter/material/Drawer-class.html
【讨论】:
关闭抽屉并导航到新路线的简写:
Navigator.popAndPushNamed(context, '/newroute');
【讨论】:
首先创建一个 ScaffoldState 键
GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
Scaffold(
key: _scaffoldKey,)
现在您可以使用toggleDrawer() 方法打开和关闭抽屉。
左侧抽屉
toggleDrawer() async {
if (_scaffoldKey.currentState.isDrawerOpen) {
_scaffoldKey.currentState.openEndDrawer();
} else {
_scaffoldKey.currentState.openDrawer();
}
}
右抽屉
toggleDrawer() async {
if (_scaffoldKey.currentState.isDrawerOpen) {
_scaffoldKey.currentState.openDrawer();
} else {
_scaffoldKey.currentState.openEndDrawer();
}
}
【讨论】:
Scaffold(endDrawer: Drawer())
如果你只是想关闭Drawer,你可以使用以下任何一种:
Navigator.pop(context);
Navigator.of(context).pop();
如果你想导航到一个新页面,你可以使用
Navigator.popAndPushNamed(context, "/new_page");
或
Navigator.pop(context);
Navigator.push(context, MaterialPageRoute(builder: (_) => Page2()));
【讨论】:
对于最新的 Flutter 版本(我写这篇文章时是 v0.3.2),实现有所改变,Navigator.pop() 现在需要给定的上下文。
Navigator.pop(context) 是关闭路由的典型用法。
Navigator.pop(context, true) 是关闭返回结果的路由的用法。
请参阅 Flutter 的食谱中的 Drawer Class 和 example。
【讨论】:
这就是答案
一流的
GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
小部件中的第二个
Scaffold(key: _scaffoldKey,)
代码第三
if (_scaffoldKey.currentState.isDrawerOpen) {
_scaffoldKey.currentState.openEndDrawer();
}
【讨论】:
我认为 OP 可能想说的是,即使 Navigator.of(context).pop() 和 Navigator.pop(context) 应该可以工作,但他们不在他的情况下——我有同样的问题。
在项目的早期,抽屉可以正常工作 - 正确打开和关闭。由于已经进行了几处更改(没有直接针对抽屉或其支架),抽屉不再使用 Navigator.of(context).pop() 和 Navigator.pop(context) 关闭。
我进行了更多挖掘,发现您可以使用 Scaffold.of(context).openEndDrawer 来关闭抽屉,而不是使用 Navigator.pop - 即使它看起来不应该这样做。直到现在我从未使用过这个功能,但它工作得很好。希望这对遇到同样问题的人有所帮助。
【讨论】:
Scaffold.of(context).openEndDrawer 将打开末端抽屉,因此在打开末端抽屉时,它可能会关闭当前抽屉,它会完成您的工作,但这不是正确的方法,
您可以根据需要使用以下代码。
当你想删除并推送新路由时:
Navigator.of(context).popAndPushNamed('/routeName');
当你只想弹出时:
Navigator.of(context).pop();
【讨论】:
其实很简单
问题说明:在默认主页中,当有人按下返回按钮时,(如果抽屉打开)它会关闭,否则会提示退出应用程序“是”和“否”。
解决方案
添加
GlobalKey _scaffoldKey = new GlobalKey();
Make Function Future _onWillPop() async 处理所需的问题陈述
如下源代码中给出的调用键和 onWillPop
通过完整的源代码查看源代码中与此问题陈述相关的添加
import 'package:flutter/material.dart';
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
Future<bool> _onWillPop() async {
if(_scaffoldKey.currentState != null && _scaffoldKey.currentState!.isDrawerOpen){
return true;
}
return (await showDialog(
context: context,
builder: (context) => new AlertDialog(
title: new Text('Leaving our App?'),
content: new Text('Are you sure you want to Exit?'),
actions: <Widget>[
TextButton(
onPressed: () => Navigator.of(context).pop(false),
child: new Text('No'),
),
TextButton(
onPressed: () => Navigator.of(context).pop(true),
child: new Text('Yes'),
),
],
),
)) ??
false;
}
@override
Widget build(BuildContext context) {
return Scaffold(
key: _scaffoldKey,
body: WillPopScope(
onWillPop: _onWillPop,
child: Container(
child: Center(
child: Text("Welcome My App"),
),
),
),
drawer: SideDrawer(),
);
}
}
【讨论】:
Navigate.of(context).pop();
或
导航.pop(contex);
必须是 onTap() 函数中的第一个
例如:
onTap: () {
Navigator.of(context).pop();//before pushing to any other route
Navigator.push(
context,
MaterialPageRoute(
builder: (BuildContext context) => Screen2(),
),
);
}
【讨论】: