listviewflutterricheditabletext

How do I make an editable ListView item?


I have dynamic data and I am using ListView.builder to show the user. I simply use a Text to show the data to user. Only right I add an edit icon so when user clicks the icon it enables user to edit specific Text. How do I make an editable ListView item?


Solution

  • You should call the following class into itemBuilder: , filed of ListView.builder(...)

    class ListItem extends StatefulWidget {
      @override
      _ListItemState createState() => _ListItemState();
    }
    
    class _ListItemState extends State<ListItem> {
      bool _isEnabled = false;
    
      @override
      Widget build(BuildContext context) {
        return ListTile(
          title: TextField(
            enabled: _isEnabled,
            decoration: InputDecoration(
              hintText: 'Enter a text',
            ),
          ),
    
          // The icon button which will notify list item to change
          trailing: GestureDetector(
            child: new Icon(
              Icons.edit,
              color: Colors.black,
            ),
            onTap: () {
              setState((){
                _isEnabled = !_isEnabled;
              });
            },
          ),
        );
      }
    }