Flutter 容器Container嵌套Container出現(xiàn)內(nèi)部Container設(shè)置寬高屬性不生效問題分析

問題描述

想要實(shí)現(xiàn)一個(gè)大 Container嵌套一個(gè)小 Container 的布局,如下圖所示


希望的效果
///代碼
Container(
          width: 200,
          height: 200,
          color: Colors.red,
            child: Container(
              width: 50,
              height: 50,
              color: Colors.blue,
            ),
        )

但是運(yùn)行結(jié)果確實(shí)下圖效果
代碼運(yùn)行結(jié)果

問題探究

先看下解決辦法

///增加個(gè)對(duì)齊方式
Container(
          width: 200,
          height: 200,
          color: Colors.red,
          alignment: Alignment.center,///增加個(gè)對(duì)齊方式
            child: Container(
              width: 50,
              height: 50,
              color: Colors.blue,
            ),
        )
///或者包一層row,column,center
Container(
          width: 200,
          height: 200,
          color: Colors.red,
          child: Center(
            child: Container(
              width: 50,
              height: 50,
              color: Colors.blue,
            ),
          ),
        )

那么問題來了 , 為什么同樣是容器 , Container差在哪里了呢 ? 那我們來看下Container的代碼實(shí)現(xiàn)吧 , 其關(guān)鍵在于一個(gè)屬性 : BoxConstraints .

Container({
    Key? key,
    this.alignment,
    this.padding,
    this.color,
    this.decoration,
    this.foregroundDecoration,
    double? width,
    double? height,
    BoxConstraints? constraints,
    this.margin,
    this.transform,
    this.transformAlignment,
    this.child,
    this.clipBehavior = Clip.none,
  }) : assert(margin == null || margin.isNonNegative),
       assert(padding == null || padding.isNonNegative),
       assert(decoration == null || decoration.debugAssertIsValid()),
       assert(constraints == null || constraints.debugAssertIsValid()),
       assert(clipBehavior != null),
       assert(decoration != null || clipBehavior == Clip.none),
       assert(color == null || decoration == null,
         'Cannot provide both a color and a decoration\n'
         'To provide both, use "decoration: BoxDecoration(color: color)".',
       ),
       constraints =
        (width != null || height != null)
          ? constraints?.tighten(width: width, height: height)
            ?? BoxConstraints.tightFor(width: width, height: height)
          : constraints,
       super(key: key);

代碼中可以看到BoxConstraints不是一個(gè)必傳的參數(shù),但是呢,會(huì)為我們?cè)O(shè)置一個(gè)默認(rèn)值,而其生效條件是這么判斷的
如果寬高為空怎選擇默認(rèn)值,如果寬高不為空但是不滿足默認(rèn)值條件則選擇默認(rèn)值, BoxConstraints 里的默認(rèn)寬高是屏幕寬高.

///BoxConstraints的初始設(shè)置
const BoxConstraints.tightFor({
    double? width,
    double? height,
  }) : minWidth = width ?? 0.0,
       maxWidth = width ?? double.infinity,
       minHeight = height ?? 0.0,
       maxHeight = height ?? double.infinity;

單獨(dú)談?wù)凜onstrainedBox

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容