【问题标题】:Can a Card widget display a ListTile?卡片小部件可以显示 ListTile 吗?
【发布时间】:2019-09-13 05:34:21
【问题描述】:

在这个应用程序中,当用户点击 FAB 时,它会返回一个 Card 包裹在一个 GestureDetector 中。当用户点击显示的卡片时,GestureDetector 会将他们导航到另一个页面。我想实现一个删除功能,以便我可以关闭Card

所以我将Cardchild: 定义为ListTile,并带有一个尾随Icon,它应该删除那个特定的Card。但是当我添加Card 时,它只显示title: 而不会显示尾随Icon

Cards 显示在 SliverGridcrossAxisCount = 2 中。

问题:Card 小部件是否支持显示带有尾随小部件的 ListTile 还是我的实现有误?

附:我已经尝试设置crossAxisCount = 1,但它仍然没有显示尾随Icon

卡片生成函数:

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

卡片删除功能:

      _deleteNoDo(int index) async {
        debugPrint("Deleted Item!");
        setState(() {
          cardList.removeAt(index);
        });
      }

完整示例(不包括上述功能):

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

实际结果:

预期结果(假设只显示标题和尾随图标,如下所示):

【问题讨论】:

    标签: flutter


    【解决方案1】:

    你确定你调用了正确的方法。因为很少有事情没有到位,或者您没有共享正确的代码。我将在下面解决它们。

    1. 首先对齐卡片中的子文本。它位于卡片的中心,但您的代码中没有使用中心属性。卡片小部件不会自动对齐文本。
    2. 第二次添加项目。您发布了 addItems 函数的代码,但还在构建中添加了另一个名为 createCard 的函数。所以我们不知道 createCard 是否与 addItems 函数具有相同的小部件树。
    3. 它不起作用,所以您看不到它,但即使您能够看到 Icon 小部件,您仍然无法删除该项目。原因是它是一个不可点击的小部件。如果您想在其上添加点击功能,您应该使用 IconButton 小部件。

    这些是我在您的代码中的发现。如果您可以查看它们或分享正确的代码,也许我可以提供更多帮助。

    【讨论】:

    • 是的,原来我在 List.generate 中实现了错误的卡片小部件。我还没有添加 IconButton 因为我想知道为什么图标没有显示。谢谢你的帮助:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-19
    • 2023-03-25
    • 2021-02-22
    • 1970-01-01
    • 1970-01-01
    • 2021-12-11
    • 1970-01-01
    相关资源
    最近更新 更多