Reputation:
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');
},
),
),
],
),
Upvotes: 0
Views: 328
Reputation: 2678
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.
Upvotes: 1