CovertDad
CovertDad

Reputation: 57

How to access ListTile.Subtitle programmatically?

I am fumbling my way through learning Flutter, and have been pretty successful with what I want to do so far, but this issue has be vexed.

I have a ListView of items, and I want to be able to have the user long-click on one, and then choose from a pair of commands. I couldn't really find a popup menu solution I liked, so I thought, why not put two RaisedButtons in the SubTitle, since it takes a Widget.

I've got the buttons in there, just as I want, but I want to have the subtitle hidden when the page first loads, then become visible when the user long-presses.

Current code below:

    new ListTile(
        title: new Text(
          _players[index].name,
          style: regularTextStyle,
        ),
        subtitle: new Visibility(
          child: new Row(children: <Widget>[
            new RaisedButton.icon(
                icon: new Icon(Icons.delete),
                label: new Text(Constants.DeletePlayer),
                onPressed: null),
            new Padding(padding: EdgeInsets.fromLTRB(10, 0, 0, 0)),
            new RaisedButton.icon(
                icon: new Icon(Icons.edit),
                label: new Text(Constants.EditPlayer),
                onPressed: null)
          ]),
          visible: false,
        ),
        trailing: new Icon(
          _players[index].selected
              ? Icons.check_box
              : Icons.check_box_outline_blank,
          color: _players[index].selected ? Colors.blue : null,
        ),
        onTap: () {
          setState(() {
            _players[index].toggle();
          });
        },
        onLongPress: () {
          setState(() {
            
          });
        },

How do I access the ListTile's Subtitle property from within the onLongPress handler?

Upvotes: 2

Views: 1924

Answers (1)

diegoveloper
diegoveloper

Reputation: 103551

You can just use a StatefulWidget and after you long press the ListTile rebuild the widget using setState.

Check this sample :

    class MyApp extends StatefulWidget {
      @override
      MyAppState createState() => MyAppState();
    }

    class MyAppState extends State<MyApp> {
      bool isVisible = false;

      @override
      Widget build(BuildContext context) {
        return Scaffold(
            body: Center(
                child: ListTile(
          title: Text("Title"),
          onLongPress: () {
            setState(() {
              isVisible = !isVisible;
            });
          },
          subtitle: isVisible ? Text("Subtitle sample") : null,
        )));
      }
    }

enter image description here

Upvotes: 4

Related Questions