[英]Use pushNamed with Dismissible Widget
I am trying to create a Dismissible widget but I want to have the router history, I mean when I go to another route using the onDismissed event, when user presses back button on that new view be return to the first one.我正在尝试创建一个 Dismissible 小部件,但我想拥有路由器历史记录,我的意思是当我使用 onDismissed 事件转到另一条路线时,当用户按下该新视图上的后退按钮时,将返回到第一个。
This is my widget.这是我的小部件。
Dismissible(
key: new ValueKey("dismiss_key"),
direction: DismissDirection.horizontal,
child: Container(child: this.getTopPlacesSubscription()),
onDismissed: (direction) {
if (direction == DismissDirection.endToStart) {
Navigator.of(context).pushNamed(Router.getRoute(Routes.map));
}
if (direction == DismissDirection.startToEnd) {
Navigator.of(context)
.pushNamed(Router.getRoute(Routes.camera));
}
}
I will appreciate any help.我将不胜感激任何帮助。 I got an issue trying to do it in this way.
我在尝试以这种方式进行操作时遇到了问题。
This code block will let you create a random string based on length.此代码块将让您根据长度创建一个随机字符串。 Add this to your code.
将此添加到您的代码中。
import 'dart:math';
String _randomString(int length) {
var rand = new Random();
var codeUnits = new List.generate(
length,
(index){
return rand.nextInt(33)+89;
}
);
return new String.fromCharCodes(codeUnits);
}
In your state, define a new variable and give it a random value.在您的状态下,定义一个新变量并为其指定一个随机值。
String vk;
@override
void initState() {
this.vk = _randomString(10)
}
Then go to your Dismissable widget and replace vk
with your string.然后转到您的 Dismissable 小部件并用您的字符串替换
vk
。 And here comes the magic part lol.神奇的部分来了,哈哈。 You have to change your
vk
value in onDismissed
.您必须在
onDismissed
更改vk
值。 This will pass a new value to Dismissable key, so Flutter will recognize it as a new Widget, which will prevent the error.这会将一个新值传递给 Dismissable 键,因此 Flutter 会将其识别为一个新的 Widget,从而防止出现错误。
Dismissible(
key: new ValueKey(vk),
direction: DismissDirection.horizontal,
child: Container(child: this.getTopPlacesSubscription()),
onDismissed: (direction) {
setState(() {
this.vk = _randomString(10);
});
if (direction == DismissDirection.endToStart) {
Navigator.of(context).pushNamed(Router.getRoute(Routes.map));
}
if (direction == DismissDirection.startToEnd) {
Navigator.of(context)
.pushNamed(Router.getRoute(Routes.camera));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.