Flutter: Adding a dynamic TextFormField to upload data to a list

336 views Asked by At

I have an ecommerce app where I want to give the user the ability to add custom options when they post their product. I have the options form built and I have the logic to add a new TextFormField for an additional "option" but I can't get the Add/Remove button to show so that they can add the new field.

This is the code where the TextFieldForm goes:

ListTile(
            title: Container(
              width: 250.0,
              child: TextField(
                style: TextStyle(color: Colors.white),
                controller: brandTextEditingController,
                decoration: InputDecoration(
                  enabledBorder: const OutlineInputBorder(
                    borderSide:
                        const BorderSide(color: Colors.white, width: 1.0),
                  ),
                  hintText: "Type Brand here...",
                  hintStyle: TextStyle(color: Colors.white),
//                  border: InputBorder.none,
                ),
              ),
            ),
          ),
          Divider(),
          ListTile(
            title: Container(
              width: 250.0,
              child: Column(
                  children: getOptions(),
              ),
            ),
          ),

Here's what I have to add the field dynamically:

List<Widget> getOptions(){
    List<Widget> optionsTextFields = [];
    for(int i=0; i<optionsList.length; i++) {
      optionsTextFields.add(
       OptionTextFields(i));
       SizedBox(width: 16,);
       _addRemoveButton(i == optionsList.length-1, i);
    }
    return optionsTextFields;
  }

  Widget _addRemoveButton(bool add, int index){
    return IconButton(
              icon: Icon((add) ? Icons.add : Icons.remove, color: Colors.white,),
              onPressed: (){
                if(add){
                  // add new text-fields
                  optionsList.insert(0, null);
                }
                else optionsList.removeAt(index);
                setState((){});
              },
    );
  }


  bool get wantKeepAlive => true;

  @override
  Widget build(BuildContext context) {
    return file == null ? displayUploadScreen() : displayUploadFormScreen();
  }
}

class OptionTextFields extends StatefulWidget {
  final int index;
  OptionTextFields(this.index);
  @override
  _OptionTextFieldsState createState() => _OptionTextFieldsState();
}

class _OptionTextFieldsState extends State<OptionTextFields> {
  TextEditingController optionsTextEditingController;

  @override
  void initState() {
    super.initState();
    optionsTextEditingController = TextEditingController();
  }

  @override
  void dispose() {
    optionsTextEditingController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {

    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      optionsTextEditingController.text = _UploadPageState.optionsList[widget.index] ?? '';
    });

    return TextField(
              style: TextStyle(color: Colors.white),
              controller: optionsTextEditingController,
              decoration: InputDecoration(
                enabledBorder: const OutlineInputBorder(
                  borderSide:
                  const BorderSide(color: Colors.white, width: 1.0),
                ),
                hintText: "Option...",
                hintStyle: TextStyle(color: Colors.white),
              ),
    );
  }

How to I get the Add/Remove button to show up?

1

There are 1 answers

0
Tyler On

I was able to figure out how to get the button working. Here's the code below:

Here's the main form:

ListTile(
            key: _formKey,
            title: Container(
              width: 250.0,
              child: Column(
                  children: getOptions(),
              ),
            ),
          ),

Here's the code for getOptions:

List<Widget> getOptions(){
    List<Widget> optionsTextFields = [];
    for(int i=0; i<optionsList.length; i++) {
      optionsTextFields.add(
        Row(
          children: <Widget>[
            Expanded(
              flex: 8,
                child: OptionTextFields(i),
            ),
            Expanded(
              flex: 1,
              child: _addRemoveButton(i == optionsList.length-1, i),
             ),
          ],
        ),
        );

And here's the form for the TextFormField:

return TextField(
        style: TextStyle(color: Colors.white),
        controller: optionsTextEditingController,
        decoration: InputDecoration(
          enabledBorder: const OutlineInputBorder(
            borderSide:
            const BorderSide(color: Colors.white, width: 1.0),
          ),
          hintText: "Option...",
          hintStyle: TextStyle(color: Colors.white),
        ),
    );
  }
}

Here's the code for the button:

Widget _addRemoveButton(bool add, int index){
    return IconButton(
        icon: Icon((add) ? Icons.add : Icons.remove, color: Colors.white,),
        onPressed: (){
          if(add){
            // add new text-fields
            optionsList.insert(0, null);
          }
          else optionsList.removeAt(index);
          setState((){});
        },
    );
  }

As you can see, I had to place a row within the column and then use the Expanded widget within the row to get the placement of the Add/Remove button.