0

How can I create widget when I pressed IconButton I tried something but it's not working.If you know and help me I will be happy.

Container(
      color: Colors.redAccent,
      height: commonHeight / 12,
      width: commonWidth * 10 / 100,
      child: IconButton(
        highlightColor: Colors.yellow,
        icon: Icon(Icons.add_sharp),
        iconSize: 25,
        onPressed: () {
        print('Something');
                  },
                ),
              ),
            ],
          ),
2
  • Where you want to put the widget? can you show more code? Commented Mar 25, 2021 at 10:51
  • What you could do is create a widget and keep it hidden and by clicking the button show it. Commented Mar 25, 2021 at 11:01

1 Answer 1

1

First create a bool variable(say you name it isWidgetVisisble) which will control the visibility of the widget. Then in your widget tree you can add a conditional statement like this :

isWidgetVisible ? YourWidget() : Container();

Meaning of this line is simple, if your isWidget Visible is true, the widget will be displayed else it will display an empty container of height and width 0.

Then in your onPressed function you can call a setState will will toggle the boolean variable it will be something like this :

onPressed: () {
       setState((){
       isWidgetVisible = true;
  });
},

Also make sure to position the new widget properly, if you have added it somewhere in the middle of a column then it will push the other widgets below it down.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.