简体   繁体   English

如何在 Flutter 中更改 ListTile 中的前导形状?

[英]How to change shape of leading in ListTile in Flutter?

I want to show an image in a ListTile widget which should looks something like this.我想在 ListTile 小部件中显示一个图像,它应该看起来像这样。 在此处输入图片说明

Here is my code:这是我的代码:

                          Padding(
                            padding: EdgeInsets.symmetric(vertical: 0.0, horizontal: 20.0),
                            child: Card(
                              color: Colors.white,
                              child: ListTile(
                                leading: Container(
                                  child: Image.network((orientation == Orientation.portrait
                                        ? image.portrait
                                        : image.landscape),
                                        fit: BoxFit.cover)
                                     ),
                                title: Text(terms[index].title),
                                subtitle: Text(terms[index].videoNumber.toString() + " Videos"),
                                trailing: Icon(
                                  Icons.arrow_forward_ios,
                                  color: Colors.lightBlueAccent,
                              ),
                              ),
                            ),
                          );

This results in below output这导致以下输出

在此处输入图片说明

What should I do so the image looks spread out as above.我该怎么做才能使图像看起来像上面一样展开。

You can set the padding of ListTile to 0:您可以将 ListTile 的填充设置为 0:

                  ...
                      child: ListTile(
                        contentPadding: EdgeInsets.all(0),
                        leading: ...
                  );

But even so, you can see that the leading only take the upper half, not the entire height of the card:但即便如此,你也可以看到领先只占卡的上半部分,而不是卡的整个高度:

                 ... 
                    Card(
                      color: Colors.blue,
                      child: ListTile(
                        contentPadding: EdgeInsets.all(0),
                        leading: ...

Which result in:结果是:
在此处输入图片说明

You can use a Stack to include both ListTile and the leading Icons but lots of things have to be hardcorded.您可以使用 Stack 来包含 ListTile 和前导图标,但很多东西必须硬编码。 In this case, I recommend using ClipRRect with Row:在这种情况下,我建议将 ClipRRect 与 Row 一起使用:

Padding(
                    padding: EdgeInsets.symmetric(
                        vertical: 0.0, horizontal: 20.0),
                    child: ClipRRect(
                      borderRadius: BorderRadius.circular(10),
                      child: Container(
                        height: 70,
                        color: Colors.white,
                        child: Row(
                          children: <Widget>[
                            Container(
                              color: Colors.red,
                              width: 70,
                              height: 70,
                              child: Icon(Icons.cake, color: Colors.white),
                            ),
                            SizedBox(width: 10),
                            Expanded(
                              child: Column(
                                mainAxisAlignment: MainAxisAlignment.center,
                                crossAxisAlignment:
                                    CrossAxisAlignment.start,
                                children: <Widget>[
                                  Text('Test Title'),
                                  Text('Test Video',
                                      style: TextStyle(color: Colors.grey))
                                ],
                              ),
                            ),
                            Icon(Icons.arrow_forward_ios,
                                color: Colors.blue),
                          ],
                        ),
                      ),
                    ),
                  );

Result:结果:
在此处输入图片说明

Widget buildCard(_user) { return Padding( padding: const EdgeInsets.all(8.0), child: ClipRRect( Widget buildCard(_user) { return Padding( padding: const EdgeInsets.all(8.0), child: ClipRRect(

    child: Card(
      color: Colors.blue.shade100,
      child: Row(
        children: <Widget>[
          Container(
            margin: const EdgeInsets.all(0),
            decoration:  BoxDecoration(
              color: Colors.blue.shade200,
              shape: BoxShape.rectangle,
              borderRadius:const BorderRadius.only(
                topLeft: Radius.circular(4.0),
                bottomLeft: Radius.circular(4.0),
              ),
            ),
            width: 20,
            height: 73,
          ),
          const SizedBox(width: 10),
          Expanded(
            child: ListTile(
              title: Text('${_user.state?.email!.substring(0, 6)}'),
              subtitle: Text('${_user.state?.createdAt}'),
            ),
          ),
          //const Icon(Icons.arrow_forward_ios, color: Colors.blue),
        ],
      ),
    ),
  ),
);

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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