简体   繁体   English

卡片小部件可以显示ListTile吗?

[英]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 . 因此,我将Cardchild:定义为带有尾随IconListTile ,应该删除该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被显示在SliverGridcrossAxisCount = 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
                      )
                  ),
                ]
            )
        );
      }
    }

actual result: 实际结果: 实际结果

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. 我将在下面解决它们。

  1. First of all the alignment of child text in card. 首先,对齐卡片中的子文字。 It is in center of the card but there is no center property used in your code. 它在卡的中心,但是代码中没有使用中心属性。 Card widget does not auto align the text. 卡片小部件不会自动对齐文本。
  2. Second adding the items. 第二项添加。 You post the code of addItems function but also added another function in build with name createCard . 您发布了addItems函数的代码,但还在构建中添加了另一个名为createCard的函数。 So we don't know if that createCard have the same widget tree as the addItems function or not. 因此,我们不知道createCard是否具有与addItems函数相同的小部件树。
  3. Its not working not so you are not seeing it but even if you are able to see the Icon widget you still won't be able to delete the item. 它无法正常工作,因此您看不到它,但是即使您能够看到Icon小部件,您仍将无法删除该项目。 The reason being is its a non-clickable widget. 原因是它是不可点击的小部件。 You should be using the IconButton widget if you want to add the click functionality on it. 如果要在其上添加单击功能,则应使用IconButton小部件。

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.

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