[英]Can a Card widget display a ListTile?
In this app, when the user clicks the FAB, it returns a Card
wrapped in a GestureDetector
. 在这个程序中,当用户点击FAB,它返回一个
Card
包在一个GestureDetector
。 when the user clicks on a displayed card, the GestureDetector
will navigate them to another page. 当用户单击显示的卡片时,
GestureDetector
会将其导航到另一个页面。 I want to implement a delete function so that i can dismiss a Card
. 我想实现删除功能,以便我可以解散
Card
。
So i defined the child:
of the Card
as a ListTile
with a trailing Icon
which is supposed to delete that particular Card
. 因此,我将
Card
的child:
定义为带有尾随Icon
的ListTile
,应该删除该Card
。 But when i add a Card
, it only displays the title:
and does not display the trailing Icon
. 但是,当我添加
Card
,它仅显示title:
,而不显示结尾的Icon
。
The Cards
are displayed in a SliverGrid
with a crossAxisCount = 2
. 该
Cards
被显示在SliverGrid
与crossAxisCount = 2
。
question: do Card
widgets support displaying a ListTile
with a trailing widget or is my implementation wrong? 问题:
Card
小部件支持显示带有尾随小部件的ListTile
还是我的实现错误?
PS i have tried setting crossAxisCount = 1
but it still does not show the trailing Icon
. PS我尝试设置
crossAxisCount = 1
但仍不显示尾随的Icon
。
card generating function: 卡生成功能:
void addItems() async {
setState(() {
cardList.insert(0, new GestureDetector(
onTap: () async {
await Navigator.push(context, MaterialPageRoute(
builder: (context) =>
TodoList(), // this just navigates to another screen ; not important in this question
)
);
},
child: Card(
child: ListTile(
title: Text("project 1"),
trailing: new Icon(Icons.remove_circle,
color: Colors.redAccent,),
// subtitle: whitefontstylemont(text: "project 1", size: 20,)) //this is just a custom TextStyle
),
)
));
});
}
card deleting function: 卡删除功能:
_deleteNoDo(int index) async {
debugPrint("Deleted Item!");
setState(() {
cardList.removeAt(index);
});
}
complete example (excluding above mentioned functions): 完整示例(不包括上述功能):
class _Starting_screenState extends State<Starting_screen> {
int _count = 0;
@override
Widget build(BuildContext context) {
List<Widget> cardList = new List.generate(
_count, (int i) => new createCard());
SystemChrome.setEnabledSystemUIOverlays([]);
// _deleteNoDo(int index) async {
// debugPrint("Deleted Item!");
// setState(() {
// cardList.removeAt(index);
// });
// }
//
// void addItems() async {
// setState(() {
// cardList.insert(0, new GestureDetector(
// onTap: () async {
// await Navigator.push(context, MaterialPageRoute(
// builder: (context) =>
// TodoList(), // this just navigates to another screen ; not important in this question
// )
// );
// },
// child: Card(
// child: ListTile(
// title: Text("project 1"),
// trailing: new Icon(Icons.remove_circle,
// color: Colors.redAccent,),
//// subtitle: whitefontstylemont(text: "project 1", size: 20,)) //this is just a custom TextStyle
// ),
// )
// ));
// });
// }
return Scaffold(
floatingActionButton: FloatingActionButton(
onPressed: () async {
setState(() {
_count += 1;
});
},
heroTag: "btn2",
child: Icon(Icons.add, color: Color(whitecolor),), // this is just a custom color
backgroundColor: Color(redcolor),), // this is just a custom color
body: CustomScrollView(
slivers: <Widget>[
SliverAppBar(
pinned: true,
flexibleSpace: FlexibleSpaceBar(
),
actions: <Widget>[
Expanded(
child: Padding(
padding: const EdgeInsets.only(top: 20, right: 10),
child: whitefontstyle(text: "Remaining tasks for today - ${cardList.length}", size: 20,), // this is just a custom textstyle
),
),
],
),
SliverGrid(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2
),
delegate: new SliverChildBuilderDelegate((context,
index) {
return cardList[index]; // this is where the cards are displayed in a list
},
childCount: cardList.length
)
),
]
)
);
}
}
expected result (assume only displaying title and trailing icon as shown below): 预期结果(假设仅显示标题和结尾图标,如下所示):
Are you sure that you are calling the right methods. 您确定要调用正确的方法吗? Cause there are few things which are not in place or you haven't shared the right code.
原因是有些东西还没有到位,或者您没有共享正确的代码。 I will address them below.
我将在下面解决它们。
These are my finding in your code. 这些是我在您的代码中发现的。 If you can review them or either share the correct code, may be I am able to help more.
如果您可以查看它们或共享正确的代码,也许我可以提供更多帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.