简体   繁体   English

可拖动的可拖拉容器

[英]Draggable and swipable container in flutter

I'm new to flutter and I'm looking for a way to implement a simple feature. 我是新手,我正在寻找一种实现简单功能的方法。 A draggable container. 可拖动的容器。

I have two groups of UI elements wrapped in a Container widget. 我有两个包装在Container小部件中的UI元素组。 I want to be able to go from one group to another by dragging or swiping in different directions. 我希望能够通过在不同方向上拖动或滑动而从一个组转到另一个组。

How would I go about doing this? 我将如何去做呢?

Here are sample images of my UI design to help you understand what I want to achieve: 以下是我的UI设计的示例图像,可帮助您了解我想要实现的目标:

Image #1 图片#1

标签1

Image #2 图片#2

标签2

As you can see, Image #1 and Image #2 are only different in the bottom part of my design. 如您所见,图像#1和图像#2仅在设计的底部有所不同。 I have already created all the necessary UI elements and wrapped them in the Container widget. 我已经创建了所有必需的UI元素,并将它们包装在Container小部件中。 Now the only thing I need is the ability to go from one group to another. 现在,我唯一需要的就是能够从一个小组转到另一个小组。 It would also be nice if there was a callback method that could update the buttons above upon transitioning from one group to another. 如果有一个回调方法可以在从一个组过渡到另一个组时更新上面的按钮,那也很好。

Thanks in advance! 提前致谢!

There are many possibilities to achieve this, depends on your exact wishes, here are 3 ideas: 有多种方法可以实现此目标,具体取决于您的意愿,以下是3个想法:

  1. Using a TabBarView to swipe the entire screen, Tab1 will be the first screen you showed, and Tab2 will be second screen - only the contents. 使用TabBarView滑动整个屏幕,Tab1将是显示的第一个屏幕,Tab2将是第二个屏幕-仅显示内容。 (you probably did not want that, but just putting it out there). (您可能不希望这样,而只是将其放在那里)。

  2. Dividing the container into 2 pieces (vertically), and placing the TabBarView on the bottom part, having 2 tabs: 1 with the Today part and one with the Weekly part. 将容器分为两个部分(垂直),然后将TabBarView放在底部,有2个标签:1个标签为“今日”部分,一个标签为“每周”部分。 (there are a few examples out there, for instance: divide screen into two equal parts in flutter ). (例如,这里有一些示例: 在flutter中将屏幕分成两个相等的部分 )。

You can also customize the build method to change anything (for example the top indicator) based on the current tab index (as asked and answered here: How to get current tab index in Flutter ) 您还可以自定义构建方法,以基于当前选项卡索引(如此处要求和回答: 如何在Flutter中获取当前选项卡索引 )更改任何内容(例如,顶部指示器)。

For a more custom solution you can use: 对于更自定义的解决方案,您可以使用:

  1. GestureDetector wrapped around your container, and handle OnHorizontalDragX (where X is Start, End etc.) to do any custom stuff - maybe changing the state and trigger a rebuild with the new image 将GestureDetector包裹在您的容器上,并处理OnHorizo​​ntalDragX(其中X是Start,End等)以执行任何自定义操作-可能更改状态并触发使用新图像的重建

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM