简体   繁体   English

将 pushNamed 与 Dismissible 小部件一起使用

[英]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.

相关问题 如何将 pushNamed 与 Dismissible Widget 一起使用并查看背景透明第一页 - How to use pushNamed with Dismissible Widget and see background transparent first page 如何在Flutter中的CustomScrollView中使用可禁用的小部件? - How to use a dismissible widget inside a CustomScrollView in Flutter? 在颤动中的 CustomScrollView 中使用 Dismissible Widget 的正确方法是什么? - What is the correct way to use Dismissible Widget inside CustomScrollView in flutter? 如何在 flutter 中使用提供者 state 管理的可关闭小部件 - How to use dismissible widget with provider state management in flutter 如何修复这个可关闭的小部件边框 - How to fix this dismissible widget border 在 Dismissible Widget 上禁用关闭方向 - Disable dismiss direction on Dismissible Widget Flutter:已关闭的可关闭小部件仍然是树的一部分 - Flutter: a dismissed dismissible widget is still part of the tree 使用唯一键时可关闭的小部件不会更改 - Dismissible widget not changing when using Unique Key Flutter:Dismissible 小部件内的 SnackBar 无法正常工作 - Flutter: SnackBar inside Dismissible widget is not working properly 已关闭的 Dismissible 小部件仍然是 flutter 中树的一部分 - A dismissed Dismissible widget is still part of the tree in flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM