0

I have created a class that has a widget that I want to appear on the home page as many times as the button is pressed.

https://gyazo.com/f9cc3e2c1f48e9efeb7b1d1c9b0b988e

as you can observe in this picture, the 'home care kit' rectangles is the widget, and I want them to appear when you press the add device button.

Here is the code of the button:

child: RaisedButton(

elevation: 0,
shape: RoundedRectangleBorder(
  borderRadius: BorderRadius.circular(40.0),
side: BorderSide(color: Colors.transparent)),
onPressed: () {print('Clicked!');},
color: Colors.transparent,
textColor: Colors.white,
child: Text('Add Device', style: TextStyle(color: Colors.white, fontSize: 18)),

),

and the widget class is simply called HomeCareKit()

Boxman07
  • 13
  • 1
  • 4
Andrei Marin
  • 616
  • 1
  • 6
  • 16
  • 3
    Does this answer your question? [Append items dynamically to ListView](https://stackoverflow.com/questions/51343567/append-items-dynamically-to-listview) – Harry Oct 13 '20 at 20:52

1 Answers1

2

You first need a list of widgets:

List<Widget> _widgetList = [];

and you pass this list of widgets to your ListView or Column like:

ListView(
    children:_widgetList,
  )

to add a widget dynamically you can do this inside your onPressed:

setState(() {
  _widgetList.add(HomeCareKit());
});
Payam Asefi
  • 2,677
  • 2
  • 15
  • 26
  • amazing. Thank you so much it works!! I have been searching all day! One more thing. Do you know how i can refresh the screen when you click is so it instantly appears and so you dont have to go on another page and go back so it appears, like a refresh on click – Andrei Marin Oct 13 '20 at 21:10
  • 1
    @AndreiMarin make sure your page is `StatefulWidget`. the screen should automatically refresh with `setState` – Payam Asefi Oct 13 '20 at 21:38
  • Yeah, i forgot to add the setstate and i just added the widget add. Thank you so much again! – Andrei Marin Oct 14 '20 at 05:11
  • how to add an initial widget to that list.? – vishnu reji Jun 02 '22 at 07:14