简体   繁体   中英

I want to create widget when I press button

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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