gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 06:59:28 31 4
gpt4 key购买 nike

我在学习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,
)));
}
}

enter image description here

关于flutter - 如何以编程方式访问 ListTile.Subtitle?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53675372/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com