简体   繁体   English

Flutter 有多个独立的 DragTarget 小部件

[英]Flutter have multiple DragTarget widgets which are independent

I have been working with flutter for a couple months so i'm still learning.我已经与 flutter 合作了几个月,所以我还在学习。 I found an example of something similar which im adjusting to what i need.我发现了一个类似的例子,我正在根据我的需要进行调整。

I need to have multiple DragTarget widgets on the screen when i drop a shape into it i don't want all of them to update just the one where i drop the shape.当我将一个形状放入其中时,我需要在屏幕上有多个 DragTarget 小部件我不希望所有这些小部件只更新我放置形状的那个。

Home.dart家.dart

class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(APP_BAR_TITLE),
      ),
      floatingActionButton: FloatingActionButton.extended(
        onPressed: () {
          Provider.of<Data>(context).initializeDraggableList();
          Provider.of<Data>(context).changeSuccessDrop(false);
        },
        elevation: 20.0,
        label: Text('Reset'),
      ),
      body: SingleChildScrollView(
        child: Container(
          margin: EdgeInsets.only(top: 12.0),
          alignment: Alignment.center,
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              CardStackWidget(),
              Padding(
                padding: const EdgeInsets.all(16.0),
                child: DragTargetWidget(),
              ),
              Padding(
                padding: const EdgeInsets.all(16.0),
                child: DragTargetWidget(),
              ),
              Padding(
                padding: const EdgeInsets.all(16.0),
                child: DragTargetWidget(),
              )
            ],
          ),
        ),
      ),
    );
  }
}

DragTargetWidget.dart DragTargetWidget.dart

class DragTargetWidget extends StatelessWidget {


  @override
  Widget build(BuildContext context) {
    return DragTarget(onWillAccept: (data) {
      return true;
    }, onAccept: (CardItem data) {
      if (Provider.of<Data>(context).itemsList.length >= 1) {
        Provider.of<Data>(context).removeLastItem();
        Provider.of<Data>(context).changeSuccessDrop(true);
        Provider.of<Data>(context).changeAcceptedData(data);
      }
    }, builder: (context, List<CardItem> cd, rd) {
      if (Provider.of<Data>(context).isSuccessDrop) {
        return Padding(
          padding: const EdgeInsets.all(25.0),
          child: Stack(
            children: buildTargetList(Provider.of<Data>(context).getAcceptedData),
          ),
        );
      } else {
        return Padding(
          padding: const EdgeInsets.all(25.0),
          child: DottedBorder(
            color: Colors.black,
            gap: 3,
            strokeWidth: 1,
            child: Container(
                height: 100.0,
                width: 100.0,
                color: Colors.grey[400],
                child: Card(
                  shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(5)),
                  color: Colors.grey[400],
                  child: Center(
                    child: Text(
                      DROP_ITEMS_HERE,
                      style: TextStyle(color: Colors.black, fontSize: 22.0),
                    ),
                  ),
                )),
          ),
        );
      }
    });
  }

  List<Widget> buildTargetList(CardItem cardItem) {
    List<Widget> targetList = [];
    targetList.add(
      DottedBorder(
        gap: 3,
        strokeWidth: 1,
        color: Colors.black,
        child: Container(
          height: 100.0,
          width: 100.0,
          child: Card(
            shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(10.0)),
            color: cardItem.cardColor,
            child: Center(
                child: Text(
              '${cardItem.content}',
              style: TextStyle(fontSize: 25.0, color: WHITE_COLOR),
            )),
          ),
        ),
      ),
    );
    return targetList;
  }
}

将一个形状拖放到一个 DropTarget

每个 DropTarget 形状更新

Dropping one of the shapes in any of the DropTarget shapes updates all of them.将其中一个形状拖放到任何 DropTarget 形状中会更新所有这些形状。 I'm not sure if this is because i'm using the same List for everything, or if its something else.我不确定这是否是因为我对所有内容都使用相同的列表,或者是否是其他原因。

Probably too late, but it seems each of the targets gets info about their own status from a provider.可能为时已晚,但似乎每个目标都从提供商那里获得了有关其自身状态的信息。 So when you drop the object the info gets changed in the provider, since all of the targets depend on that provider they ALL change their status.因此,当您删除 object 时,提供者中的信息会发生变化,因为所有目标都依赖于该提供者,它们都会更改其状态。

A solution could be to not rely on a provider but transform the widget in stateful and use local variables inside the widget, so each can be independent一种解决方案可能是不依赖提供者,而是将小部件转换为有状态的,并在小部件内部使用局部变量,这样每个小部件都可以独立

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

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