【问题标题】:Updating a widget from a List<Widget> in flutter?在颤动中从 List<Widget> 更新小部件?
【发布时间】:2020-03-30 20:33:51
【问题描述】:

我需要知道在将小部件添加到列表后是否有任何可能的方法来更新小部件的属性,例如大小和颜色。请考虑以下代码..

List<Widget> tree = [];

当它被拖放到容器上时,我正在添加以下小部件。为了显示多个小部件,我正在使用堆栈..

DragTarget 和 Stack 如下...

DragTarget<String>(
  builder: (a,b,c)=>Stack(
    children: tree,
  ),
  onAccept: (data){
    tree.add(Positioned(
     key: Key("$sx$sy"),
     top: _y,
     left: _x,
     child: FlatButton(
      onPressed: (){
      },
      child: CustomPaint(
       size: Size(sx/2, sy/2),
       painter: ShapePainter(shape: "circle", sx : sx/2, sy: sy/2),
       child: Container(
        width: sx,
        height: sy,
       ),
      ),
     ),
    )
   );
  }

从图像..我想实现这一点,每当我点击一个圆圈时,我应该能够通过手势更新它的形状和大小..

注意

我通过创建一个相同类型和所需属性的新小部件而不是通过手势而是通过在 InputFields 中填充详细信息然后以下列方式替换它来实现类似的功能..

List<Widget> tree; 

//Then replace it with..

tree.insert(0, Container());
OR
tree.insert(1, Container());

我不需要这个工作..

我需要访问我单击的项目的属性,然后通过手势更新其形状和大小。

资源

如果您需要查看我的完整代码,请使用https://github.com/AbhijeetDash/designer 随意贡献..

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    您需要为您的项目创建一个自定义的有状态小部件,并在它们被点击时更改状态。

    
    
    class CustomItem extends StatefulWidget {
      @override
      _CustomItemState createState() => _CustomItemState();
    }
    
    class _CustomItemState extends State<CustomItem> {
      var desiredChangingVariable;
    
      @override
      Widget build(BuildContext context) {
        return Positioned(
          key: Key("$sx$sy"),
          top: _y,
          left: _x,
          child: FlatButton(
            onPressed: (){
              setState(() {
                //desiredChangingVariable = newValue;
              });
            },
            child: CustomPaint(
              size: Size(sx/2, sy/2),
              painter: ShapePainter(shape: "circle", sx : sx/2, sy: sy/2),
              child: Container(
                width: sx,
                height: sy,
              ),
            ),
          ),
        );
      }
    }
    
    
    
    
    
    

    此外,确保在处理填充的有状态项时不会忘记键。

    【讨论】:

    • 谢谢 Henok,这就行了。 ?
    猜你喜欢
    • 2020-03-03
    • 1970-01-01
    • 2021-08-09
    • 2021-06-27
    • 2020-09-04
    • 2020-02-21
    • 1970-01-01
    • 2021-08-08
    • 1970-01-01
    相关资源
    最近更新 更多