Abhijeet Dash
Abhijeet Dash

Reputation: 146

Updating a widget from a List<Widget> in flutter?

I need to know of there is any possible way to update the properties of a widget like size and colour after it is added to a List.. consider the following code..

List<Widget> tree = [];

And I'm adding the following widget when it is Dragged and dropped on the container.. To show multiple widgets i'm using a stack..

DragTarget and Stack are as follows...

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,
       ),
      ),
     ),
    )
   );
  }

How Application Looks

From the Image.. I want to achieve that whenever I click a circle I should be able to update its shape and size by gestures..

NOTE

I achieved similar feature by creating a new Widget of same type and desired properties not by gestures but by filling the details in InputFields and then replacing it in following ways..

List<Widget> tree; 

//Then replace it with..

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

I don't need this to work..

I need to access the properties of the item on which I clicked and then update its shape and size with gestures.

Resource

If you need to see my complete code then use https://github.com/AbhijeetDash/designer Feel free to contribute..

Upvotes: 0

Views: 2330

Answers (1)

Henok
Henok

Reputation: 3393

You need to create a custom stateful widget for your items and change the state whenever they are clicked.



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,
          ),
        ),
      ),
    );
  }
}





Additionally, make sure you won’t forget about keys when you’re dealing with populated stateful items.

Upvotes: 1

Related Questions