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,它的功能也一样。没有什么重要的问题使用键时出现问题。很高兴能提供帮助:)抱歉,如果我想从我的仪表板屏幕状态
访问中的位置
和位置
,我该怎么做呢?这取决于具体情况。您想什么时候访问它们?有什么行动吗?这种行为是从哪里触发的?