title: how to remove widget based on it's index in flutter
body: <p>I've question about how to close the appropriate widget based on the close button index. here in this image, you can see the output so here I am adding some widget using add button which located inside app bar now i've added close button inside the container when the user pressed the close button it will remove that container.</p>
  
  <p>Here is the image of output :</p>
  
  <p><a href="https://i.stack.imgur.com/xLPkv.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xLPkv.png" alt="enter image description here"></a></p>
  
  <p>Here is the code i've tried</p>
  
  <pre><code>class BspUnlicensedSignupPage extends StatefulWidget {
    static const String routeName = "/bspUnlicensedSignup";
    final BspSignupCommonModel bspSignupCommonModel;
  
    BspUnlicensedSignupPage({
      Key key,
      @required this.bspSignupCommonModel,
    }) : super(key: key);
  
    @override
    _BspUnlicensedSignupPageState createState() =&gt;
        _BspUnlicensedSignupPageState();
  }
  
  class _BspUnlicensedSignupPageState extends State&lt;BspUnlicensedSignupPage&gt; {
    final GlobalKey&lt;FormState&gt; _formKey = GlobalKey&lt;FormState&gt;();
  
    List&lt;Object&gt; images = List&lt;Object&gt;();
    Future&lt;File&gt; _imageFile;
    bool autovalidate = false;
    bool informationislegitimate = false;
    DateTime expirydate1 = DateTime.now();
    DateTime expirydate2 = DateTime.now();
  
    final format = DateFormat("yyyy-MM-dd");
    final format2 = DateFormat("yyyy-MM-dd");
  
    String type2 = 'Passport';
    List&lt;String&gt; _type = &lt;String&gt;[
      '',
      'Passport',
      'Driving License',
      'Voter ID card',
      'Ration Card',
      'Aadhar',
      'Other Id',
    ];
    String type = 'Passport';
    var _myWidgets = List&lt;Widget&gt;();
    int _index = 3;
    final Map&lt;int, String&gt; identification1Values = Map();
    final Map&lt;int, String&gt; documentValues = Map();
    final Map&lt;int, DateTime&gt; expiryDateValues = Map();
    final Map&lt;int, String&gt; issuingAuthority = Map();
    final Map&lt;int, String&gt; identificationPicturesValues = Map();
  
    final List&lt;TextEditingController&gt; _documentControllers = List();
    final List&lt;TextEditingController&gt; _issuingauthoritytype = List();
    final List&lt;TextEditingController&gt; _expiryDate = List();
    final List&lt;TextEditingController&gt; _issuingauthority = List();
    final List&lt;List&lt;Object&gt;&gt; _identificationpictures = List();
  
    @override
    void initState() {
      super.initState();
      setState(() {
        images.add("Add Image");
        images.add("Add Image");
        images.add("Add Image");
        images.add("Add Image");
        images.add("Add Image");
      });
    }
  
    void _add() {
      int keyValue = _index;
      _myWidgets = List.from(_myWidgets)
        ..add(Column(
          key: Key("$keyValue"),
          children: &lt;Widget&gt;[
            SizedBox(height: 10),
            Container(
              // padding: EdgeInsets.fromLTRB(18,5,18,18),
              padding: EdgeInsets.all(15),
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.circular(10),
                boxShadow: [
                  BoxShadow(
                    color: Colors.black12,
                    blurRadius: 15,
                  ),
                ],
              ),
              child: Column(
                children: &lt;Widget&gt;[
                  Stack(
                    children: &lt;Widget&gt;[
                      Align(
                        alignment: Alignment.topRight,
                        child: GestureDetector(
                          child: Icon(Icons.close),
                          onTap: () {
                            print("CLose pressed");
                            _myWidgets.removeAt(_index);
                          },
                        ),
                      ),
                      SizedBox(
                        height: 10,
                      ),
                      Column(
                        children: &lt;Widget&gt;[
                          SizedBox(
                            height: 20,
                          ),
                          _buildidentificationtype1(keyValue),
                          _builddocumentnumber1(keyValue),
                          _builddate(keyValue),
                          _buildissuingauthority1(keyValue),
                          _buildidentificationpictures(keyValue),
                        ],
                      ),
                    ],
                  )
                ],
              ),
            )
          ],
        ));
  
      setState(() =&gt; ++_index);
    }
  
    bool isClicked = false;
  
    Widget _buildidentificationtype1(int keyValue) {
      TextEditingController controller = TextEditingController();
      _issuingauthoritytype.add(controller);
  
      return FormBuilder(
        autovalidate: autovalidate,
        child: FormBuilderCustomField(
            attribute: "Business type",
            validators: [FormBuilderValidators.required()],
            formField: FormField(
              builder: (FormFieldState&lt;dynamic&gt; field) {
                return InputDecorator(
                  decoration: InputDecoration(
                    prefixIcon: Icon(Icons.location_on),
                    labelText: "Business type",
                    errorText: field.errorText,
                  ),
                  isEmpty: type == '',
                  child: new DropdownButtonHideUnderline(
                    child: new DropdownButton(
                      value: type,
                      isDense: true,
                      onChanged: (String newValue) {
                        setState(() {
                          type = controller.text = newValue;
                          field.didChange(newValue);
                        });
                      },
                      items: _type.map(
                        (String value) {
                          return new DropdownMenuItem(
                            value: value,
                            child: new Text(value),
                          );
                        },
                      ).toList(),
                    ),
                  ),
                );
              },
            )),
      );
    }
  
    Widget _builddocumentnumber1(int keyValue) {
      TextEditingController controller = TextEditingController();
      _documentControllers.add(controller);
      return new TudoTextWidget(
        controller: controller,
        prefixIcon: Icon(FontAwesomeIcons.idCard),
        labelText: "Document Number",
        validator: Validators().validateLicenseno,
        onSaved: (val) {
          setState(() {
            documentValues[keyValue] = val;
          });
          // _licenseno = val;
        },
      );
    }
  
    Widget _builddate(int keyValue) {
      TextEditingController controller = TextEditingController();
      _expiryDate.add(controller);
      return DateTimeField(
        format: format,
        autocorrect: true,
        autovalidate: autovalidate,
        controller: controller,
        readOnly: true,
        decoration: InputDecoration(
            labelText: "Expiry Date",
            hintText: "Expiry Date",
            prefixIcon: Icon(
              FontAwesomeIcons.calendar,
              size: 24,
            )),
        onShowPicker: (context, currentValue) {
          return showDatePicker(
              context: context,
              firstDate: DateTime(1900),
              initialDate: currentValue ?? DateTime.now(),
              lastDate: DateTime.now());
        },
      );
    }
  
    Widget _buildissuingauthority1(int keyValue) {
      TextEditingController controller = TextEditingController();
      _issuingauthority.add(controller);
      return new TudoTextWidget(
        prefixIcon: Icon(FontAwesomeIcons.idCard),
        labelText: "Issuing Authority",
        validator: (val) =&gt; Validators.validateName(val, "Issuing Authority"),
        onSaved: (val) {
          setState(() {
            issuingAuthority[keyValue] = val;
          });
        },
        controller: controller,
      );
    }
  
    Widget _buildidentificationpictures(int keyValue) {
      return GridView.count(
        physics: NeverScrollableScrollPhysics(),
        shrinkWrap: true,
        crossAxisCount: 5,
        childAspectRatio: 1,
        children: List.generate(images.length, (index) {
          if (images[index] is ImageUploadModel) {
            ImageUploadModel uploadModel = images[index];
  
            return Card(
              clipBehavior: Clip.antiAlias,
              child: Stack(
                children: &lt;Widget&gt;[
                  Image.file(
                    uploadModel.imageFile,
                    width: 300,
                    height: 300,
                  ),
                  Positioned(
                    right: 5,
                    top: 5,
                    child: InkWell(
                      child: Icon(
                        Icons.remove_circle,
                        size: 20,
                        color: Colors.red,
                      ),
                      onTap: () {
                        setState(() {
                          images.replaceRange(index, index + 1, ['Add Image']);
                        });
                      },
                    ),
                  ),
                ],
              ),
            );
          } else {
            return Card(
              child: IconButton(
                icon: Icon(Icons.add),
                onPressed: () {
                  _onAddImageClick(index);
                },
              ),
            );
          }
        }),
      );
    }
  
    @override
    Widget build(BuildContext context) {
      final appBar = AppBar(
        title: Text("BSP Unlicensed Details"),
        leading: IconButton(
          icon: Icon(Icons.arrow_back_ios),
          onPressed: () {
            NavigationHelper.navigatetoBack(context);
          },
        ),
        actions: &lt;Widget&gt;[IconButton(icon: Icon(Icons.add), onPressed: _add)],
        centerTitle: true,
      );
      final bottomNavigationBar = Container(
        color: Colors.transparent,
        height: 56,
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: &lt;Widget&gt;[
            new FlatButton.icon(
              icon: Icon(Icons.close),
              label: Text('Clear'),
              color: Colors.redAccent,
              textColor: Colors.black,
              padding: EdgeInsets.symmetric(vertical: 10, horizontal: 30),
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(7),
              ),
              onPressed: () {},
            ),
            new FlatButton.icon(
                icon: Icon(FontAwesomeIcons.arrowCircleRight),
                label: Text('Next'),
                color: colorStyles["primary"],
                padding: EdgeInsets.symmetric(vertical: 10, horizontal: 30),
                textColor: Colors.white,
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(7),
                ),
                onPressed: () async {
                  setState(() {
                    autovalidate = !autovalidate;
                  });
                  if (_formKey.currentState.validate()) {
                    BspSignupCommonModel model = widget.bspSignupCommonModel;
                    for (var i = 0; i &lt; _myWidgets.length; i++) {
                      String document = _documentControllers[i].text;
                      String issuingAuthorityType = _issuingauthoritytype[i].text;
                      String expiryDate = _expiryDate[i].text;
                      String issuingAuthority = _issuingauthority[i].text;
                      //  String picture = _identificationpictures[i].text;
                      print('Document: $document');
                      print('IssuingAuthorityType: $issuingAuthorityType');
                      print('ExpiryDate: $expiryDate');
                      print('IssuingAuthority: $issuingAuthority');
                      print('Picture: ${_identificationpictures.length}');
                      print(_myWidgets.length);
                      List&lt;Licensed&gt; listOfLicenses = new List&lt;Licensed&gt;();
  
                      Licensed licensed = new Licensed(
                          bspLicenseNumber: document,
                          bspAuthority: issuingAuthority,
                          bspExpiryDate: expiryDate,
                          bspIssuing: issuingAuthorityType);
  
                      licensed.bspLicenseNumber = _documentControllers[i].text;
                      licensed.bspExpiryDate = _expiryDate[i].text;
                      licensed.bspIssuing = _issuingauthoritytype[i].text;
                      licensed.bspAuthority = _issuingauthority[i].text;
                      listOfLicenses.add(licensed);
                      model.unlicensed = listOfLicenses;
                    }
  
                    Navigator.push(
                        context,
                        MaterialPageRoute(
                            builder: (context) =&gt; BspLicensedSignupTermsPage(
                                bspSignupCommonModel: model)));
                  }
                }),
          ],
        ),
      );
      return new Scaffold(
        appBar: appBar,
        bottomNavigationBar: bottomNavigationBar,
        body: Container(
          height: double.infinity,
          width: double.infinity,
          child: Form(
              autovalidate: autovalidate,
              key: _formKey,
              child: Stack(
                children: &lt;Widget&gt;[
                  Column(
                    children: &lt;Widget&gt;[
                      Expanded(
                        child: SizedBox(
                          child: ListView(
                            padding: const EdgeInsets.all(18.0),
                            children: _myWidgets,
                          ),
                        ),
                      ),
                    ],
                  )
                ],
              )),
        ),
      );
    }
  }
  </code></pre>