繁体   English   中英

如何以编程方式访问 ListTile.Subtitle?

[英]How to access ListTile.Subtitle programmatically?

我在学习 Flutter 的过程中摸索着,并且到目前为止我想做的事情已经相当成功,但是这个问题一直困扰着我。

我有一个项目的 ListView,我希望能够让用户长按一个,然后从一对命令中进行选择。 我真的找不到我喜欢的弹出菜单解决方案,所以我想,为什么不在 SubTitle 中放置两个 RaisedButton,因为它需要一个 Widget。

我在那里有按钮,就像我想要的那样,但是我想在页面第一次加载时隐藏副标题,然后在用户长按时变得可见。

当前代码如下:

 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(() { }); },

如何从 onLongPress 处理程序中访问 ListTile 的 Subtitle 属性?

您可以只使用StatefulWidget并在长按ListTile后使用setState重建小部件。

检查此示例:

    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,
        )));
      }
    }

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM