如何以编程方式访问ListTile.Subtitle? [英] How to access ListTile.Subtitle programmatically?
问题描述
我正在通过学习Flutter摸索自己的方式,到目前为止,我已经很成功地完成了我想做的事情,但是这个问题已经困扰了.
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.
我有一个项目的ListView,我希望能够让用户长按一个,然后从一对命令中进行选择.我找不到真正喜欢的弹出菜单解决方案,所以我想,为什么不把两个RaisedButtons放在SubTitle中,因为它需要一个Widget.
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.
下面的当前代码:
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属性?
How do I access the ListTile's Subtitle property from within the onLongPress handler?
推荐答案
您可以只使用StatefulWidget
,然后长按ListTile
并使用setState
重建小部件.
You can just use a StatefulWidget
and after you long press the ListTile
rebuild the widget using 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,
)));
}
}
这篇关于如何以编程方式访问ListTile.Subtitle?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!