0

My button to add item doesn't show.

I ask to user to fill player Name. If user would like to add a player, then click on a button to add a new field

List<Widget> _playerList = new List(); //_playerList is my List name

ListView.builder(
    itemBuilder: (context,index){
      _playerList.add(new TextField());
      Widget widget = _playerList.elementAt(index);
    return widget;
    }),

    new FlatButton.icon(
                        color: Colors.black,
                        icon: Icon(
                          Icons.add,
                          color: Colors.white,
                          size: 18,
                        ),
                        label: Text('Add player',
                            style: TextStyle(fontSize: 12, color: Colors.white)),
                        onPressed: () {
                          _playerList.add(new Text("ggg"));
                        },


                    ),

Button to add not display

3 Answers 3

1

In your code, below part creating problem-

ListView.builder(
    itemBuilder: (context,index){
      _playerList.add(new TextField());
      Widget widget = _playerList.elementAt(index);
    return widget;
    }),

Remove '_playerList.add(new TextField());' line. It should like-

 class Items extends StatefulWidget {
      @override
      State<StatefulWidget> createState() {
        return _ItemsState();
      }
    }

    class _ItemsState extends State<Items> {
       List<Widget> _playerList = new List(); //_playerList is my List name

      @override
      Widget build(BuildContext context) {
        // TODO: implement build
        return Scaffold(
            body: Column(
              children: <Widget>[
                new FlatButton.icon(
                  color: Colors.black,
                  icon: Icon(
                    Icons.add,
                    color: Colors.white,
                    size: 18,
                  ),
                  label: Text('Add player',
                      style: TextStyle(fontSize: 12, color: Colors.white)),
                  onPressed: () {
                    setState(() {

                      _playerList.add(new Text("ggg"));
                      print(_playerList);
                    });
                  },
                ),
                Expanded(
                    child: ListView.builder(
                      itemCount:_playerList.length ,
                        itemBuilder: (context,index){
                          Widget widget = _playerList.elementAt(index);
                          return widget;
                        })
                )
              ],
            ));
      }
    }
Sign up to request clarification or add additional context in comments.

Comments

0

You have to call setState after you change the _playerList property. I was not sure if you want to add more text fields on button press or want to add texts on button press, so I made the code to add text fields on button press.

List<Widget> _playerList = []; 

  ListView.builder(
    itemBuilder: (context,index){
    return _playerList[index];
    })

    FlatButton.icon(
      color: Colors.black,
      icon: Icon(
        Icons.add,
        color: Colors.white,
        size: 18,
      ),
      label: Text('Add player',
          style: TextStyle(fontSize: 12, color: Colors.white)),
      onPressed: () {
        _playerList.add(TextField());
        setState(() {});
      },
    ),

Comments

0

You have to use setState, Here is the code.

Screenshot https://i.sstatic.net/SFvLE.jpg

 List<String> _playerList = List();
 TextEditingController controller = TextEditingController();

this is the set State

 _submit() {
   setState(() {
     _playerList.add(controller.text);
   });
 }

Here is the body

  Scaffold(
    body: Column(
      children: <Widget>[
        SizedBox(height: 15.0),
        TextField(
          controller: controller,
          decoration: InputDecoration(
            hintText: 'Add Player',
          ),
        ),
        SizedBox(height: 15.0),
        Center(
          child: FlatButton.icon(
              color: Colors.black,
              icon: Icon(
                Icons.add,
                color: Colors.white,
                size: 18,
              ),
              label: Text('Add player',
                  style: TextStyle(fontSize: 12, color: Colors.white)),
              onPressed: () => _submit()),
        ),
        SizedBox(height: 20.0),
        Expanded(
          child: ListView(
            padding: EdgeInsets.all(10.0),
            children: _playerList.reversed.map((data) {
              return Dismissible(
                key: Key(data),
                child: ListTile(
                  title: Text('$data'),
                ),
              );
            }).toList(),
          ),
        ),
      ],
    ),
  );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.