Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 如何使用GestureDetector和多个容器将边框放置到特定容器_Flutter_Dart - Fatal编程技术网

Flutter 如何使用GestureDetector和多个容器将边框放置到特定容器

Flutter 如何使用GestureDetector和多个容器将边框放置到特定容器,flutter,dart,Flutter,Dart,我有一个由按钮创建的可变数量的容器。这些容器由GestureDetector包装,使其可拖动。但是我有一个问题,因为我想给当前选定的容器添加一个边框。 以下是我试图做的: 这里是创建的容器的代码: @override Widget build(BuildContext context) { return Positioned( top: yPosition, left: xPosition, child: GestureDetector(

我有一个由按钮创建的可变数量的
容器。这些
容器
GestureDetector
包装,使其可拖动。但是我有一个问题,因为我想给当前选定的容器添加一个边框。 以下是我试图做的:

这里是创建的
容器的代码:

  @override
  Widget build(BuildContext context) {
    return Positioned(
      top: yPosition,
      left: xPosition,
      child: GestureDetector(
        onPanUpdate: (tapInfo) {
          setState(() {
            xPosition += tapInfo.delta.dx;
            yPosition += tapInfo.delta.dy;
            widget.selectedSectionContainer(widget.key, widget.isSelected);

          });
        },
        onTap: () => setState(() {
          widget.selectedSectionContainer(widget.key, widget.isSelected);

        }),
        child: Container(
          decoration: BoxDecoration(
            border: Border.all(
                color: widget.isSelected ? Colors.black : Colors.transparent,
                width: 5),
            color: widget.color,
          ),
        ),
      ),
    );
  }
这里是另一个类中的代码,我试图将
isSelected
设置为
容器

selectedSectionContainer: (Key key, bool isSelected) => setState(() {
        
          if (_currentSelectedContainerKey != key) {
            isSelected = true;
            _isSelected = isSelected;
            _currentSelectedContainerKey = key;
            updateSectionContainer(key);
          } else {
            isSelected = false;

            updateSectionContainer(key);
          }
通过这样做,容器都有边框,而我只需要将边框放置到所选的那个


有人能帮我吗?谢谢

在您的
仪表板屏幕状态中
,您仅维护1个
bool\u被选中
作为容器
是否被选中的状态。但是关于所选的
容器的实际数据存在于
\u currentSelectedContainerKey

因此,您不能仅将
\u isSelected
传递到所有容器中,因为当选择其中一个且
\u isSelected
设置为
true
时,所有容器都认为它们已被选中

要避免这种情况,请开始将您的
\u CurrentSelectedContainer键
\u isSelected
一起传递到您的
节容器

因此,将其添加到您的
节容器中

final bool isSelected;
final Key currentSelectedContainerKey; // Add this line in the class

required this.isSelected,
required this.currentSelectedContainerKey, // Add this line in constructor
然后,将其传递到
SectionContainer
,同时在
loadSectionContainers
addSectionContainer
中创建它们。(基本上,无论您在何处创建新的
SectionContainer

isSelected: _isSelected,
currentSelectedContainerKey: _currentSelectedContainerKey // Start passing this
现在,最后,将您的
边框
代码更改为

border: Border.all(
            color: widget.currentSelectedContainerKey == widget.key && widget.isSelected ? Colors.black : Colors.transparent,
            width: 5),
这不是最好的方法,但它可以解决您的问题。

希望你能理解我的解释。

如果你的容器重叠,flifter会将事件从树中冒泡出来,这是你的问题吗?还要发布更多的代码。因为很难理解你在控制器小部件中所做的事情。这里有两个类的完整代码@Nisanth Reddymy的问题只是在o当前选择的
节容器
,使用我的代码,我可以获得所选
节容器
,但是我不知道如何在其上加边框。代码缺少一些部分。你是说
小部件。isSelected
总是
真的
?缺少哪些部分?是的总是de>true
非常感谢!!!太好了!你为什么说这不是最好的方法?因为我们将
Key
作为一个状态来维护。
Key
在我们想要保存小部件状态或其他复杂属性时更有用。在这里,我们可以只使用一些
String
id,它的功能也一样。没有什么重要的问题使用键时出现问题。很高兴能提供帮助:)抱歉,如果我想从我的
仪表板屏幕状态
访问
中的
位置
位置
,我该怎么做呢?这取决于具体情况。您想什么时候访问它们?有什么行动吗?这种行为是从哪里触发的?