简体   繁体   English

在 ListTile 中省略前导

[英]Omit leading in ListTile

I'm trying to reuse ListTile.我正在尝试重用 ListTile。 If leading parameter isn't specified I want to skip the leading being drawn.如果未指定前导参数,我想跳过正在绘制的前导。 I have tried common practice by replacing it with empty container but that doesn't work.我尝试过用空容器替换它的常见做法,但这不起作用。 The container must have some size otherwise it crash.容器必须有一定的大小,否则会崩溃。 But even if you give the container 1px size then it will create big space on the left of tile但即使你给容器 1px 大小,它也会在瓦片的左侧创建很大的空间

在此处输入图像描述

class SimpleListTile extends StatelessWidget {
  final String title;
  final Widget leading;

  const SimpleListTile({@required this.title, this.leading});
  @override
  Widget build(BuildContext context) {
    return ListTile(
        leading: leading == null ? Container() : leading, title: Text(title));
  }
} 

Is there any way to skip leading being drawn without creating 2 tiles in if statement because I want to do the same thing with trailing widget有什么方法可以跳过绘制的前导而不在 if 语句中创建 2 个图块,因为我想对尾随小部件做同样的事情

Either omitting of the leading property or explicitly providing null instead of an empty container seems to do it:忽略leading属性或明确提供null而不是空容器似乎可以做到这一点:

return ListTile(
  title: Text(title);
);

or或者

return ListTile(
  title: Text(title);
  leading: leading // regardless of is it null or not
);

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

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