【问题标题】:hide widget from ui without taking any space从 ui 中隐藏小部件而不占用任何空间
【发布时间】:2022-07-06 21:45:25
【问题描述】:

我有一个类似下面的代码,我想用if隐藏字幕文本,但是当隐藏字幕时,它会在标题下添加空白空间,但如果删除字幕文本,它会正常工作

Column(
  children : [
    SizedBox(height: AppTheme.space1),
    Image.asset(
      image,
      width: 128,
      height: 128,
    ),
    SizedBox(height: AppTheme.space2),
    Text(
      title,
      style: Theme.of(context).textTheme.headline2,
      textAlign: TextAlign.center,
    ),
    if (subTitle != null)
      SizedBox(height: AppTheme.space0),
    if (subTitle != null)
      Text(
        subTitle ?? '',
        style: Theme.of(context).textTheme.bodyText1,
      ),
    SizedBox(height: AppTheme.space3),
    Button(
      onPressed: () {
        Navigator.pop(context, returnTextYes);
        onTapPrimaryButton?.call();
      },
      text: textYes,
    ),
  ]
)

【问题讨论】:

    标签: flutter widget


    【解决方案1】:

    使用Visibility 小部件

    bool isVisible = subtitle.isNotEmpty
    
    Visibility(
    visible:isVisible
    child: Text(
              subTitle ?? '',
              style: Theme.of(context).textTheme.bodyText1,
            )
    )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-24
      相关资源
      最近更新 更多