【Flutter 專題】122 圖解自定義半遮擋頭像 SeriesCircleProfile & CircleAvatar

{"type":"doc","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"    小菜在學習過程中,想實現一行半遮擋的用戶頭像的功能,橫向展示過程中,具體包括 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"右側頭像逐個半遮擋左側頭像","attrs":{}},{"type":"text","text":" 和 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"左側頭像逐個半遮擋右側頭像","attrs":{}},{"type":"text","text":" 兩種展示;","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"可展示本地圖或網絡圖;","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"可自定義末尾圖標;","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"可自定義邊框樣式;","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"    整個自定義過程主要是通過基礎的 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Stack","attrs":{}},{"type":"text","text":" 層級和 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Positioned","attrs":{}},{"type":"text","text":" 設置偏移量來完成,小菜僅記錄一下在測試過程中遇到的小問題;","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/13/134336a85fdf5812ab18692dddee1293.webp","alt":null,"title":"","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"SeriesCircleProfile","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"1. 左右半遮擋","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"    小菜預想的核心功能,是實現不同方向的疊加遮擋效果;其中合適準備採用 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Stack","attrs":{}},{"type":"text","text":" 小組件作爲頭像的層級展示,在通過 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Positioned","attrs":{}},{"type":"text","text":" 設置偏移量來設置半遮擋效果;其中 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Stack","attrs":{}},{"type":"text","text":" 中的子 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Widget","attrs":{}},{"type":"text","text":" 是以棧的方式存儲的,即數組後面的元素會覆蓋前面的元素;因此左右半遮擋效果主要通過 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Positioned","attrs":{}},{"type":"text","text":" 設置偏移量來完成;小菜通過定義 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"isCoverUp","attrs":{}},{"type":"text","text":" 來判斷半遮擋順序;","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"1.1 右側半遮擋左側","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"    右側半遮擋左側相對較容易,僅需將數組中元素向右側偏移固定偏移量即可;","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"List _listWid = [Container(height: size)];\nfor (int i = 0; i < urlList.length; i++) {\n _listWid.add(_itemWid(isAsset ?? false, urlList[i], i, null));\n}\nreturn Stack(children: _listWid);\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/88/8877b5b0194ec01f8b4cdb55665cb0af.webp","alt":null,"title":"","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"1.2 左側半遮擋右側","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"    左側半遮擋右側,小菜通過數組倒序方式,然後再將數組向右側設置固定偏移量;","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"List _listWid = [Container(height: size)];\nfor (int i = urlList.length - 1; i >= 0; i--) {\n _listWid.add(_itemWid(isAsset ?? false, urlList[i], i, null));\n}\nreturn Stack(children: _listWid);\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/a7/a74ed5bb609019d84876fd58bcd3d121.webp","alt":null,"title":"","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"2. 本地圖 & 網絡圖","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"    小菜在自定義傳遞頭像 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"URL","attrs":{}},{"type":"text","text":" 時考慮到,可能是網絡圖也可能是本地圖,甚至是兩者混合展示的;主要分爲兩類:","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"2.1 純本地圖 & 純網絡圖","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"    小菜設置 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"isAsset","attrs":{}},{"type":"text","text":" 爲 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"urlList","attrs":{}},{"type":"text","text":" 中公共的圖片屬性,本地圖或網絡圖;","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"if (isCoverUp ?? true) {\n if (urlList != null) {\n for (int i = urlList.length - 1; i >= 0; i--) {\n _listWid.add(_itemWid(isAsset ?? false, urlList[i], i, null));\n }\n }\n} else {\n if (urlList != null) {\n for (int i = 0; i < urlList.length; i++) {\n _listWid.add(_itemWid(isAsset ?? false, urlList[i], i, null));\n }\n }\n}\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/14/1424cc0f960ad00e6ed8211e542e94c1.webp","alt":null,"title":"","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"2.2 本地圖片 + 網絡圖混合","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"    小菜設置一個 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"List>","attrs":{}},{"type":"text","text":" 類型的 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"mixUrlList","attrs":{}},{"type":"text","text":",","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Map","attrs":{}},{"type":"text","text":" 中存儲是否爲本地圖和圖片地址,遍歷加載時通過 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"bool","attrs":{}},{"type":"text","text":" 類型判斷即可;","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"if (isCoverUp ?? true) {\n if (mixUrlList != null) {\n for (int i = mixUrlList.length - 1; i >= 0; i--) {\n _listWid.add(_itemWid(mixUrlList[i].keys.first, mixUrlList[i].values.first, i, null));\n }\n }\n} else {\n if (mixUrlList != null) {\n for (int i = 0; i < mixUrlList.length; i++) {\n _listWid.add(_itemWid(mixUrlList[i].keys.first ?? false, mixUrlList[i].values.first, i, null));\n }\n }\n}\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/2f/2fce03c2dd644279e4fef33ea69c5ffb.webp","alt":null,"title":"","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"3. 末尾圖標","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"    在用戶頭像較多點情況下,很多場景需要一個末尾省略圖標,小菜提供了一個 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"endIcon","attrs":{}},{"type":"text","text":" 的 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Widget","attrs":{}},{"type":"text","text":" 以供自定義圖標的樣式,可以是圖片或文字或其他等展示效果;值得注意的是,之前小菜設置了 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"右側半遮擋左側","attrs":{}},{"type":"text","text":" 和 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"左側半遮擋右側","attrs":{}},{"type":"text","text":" 兩種效果;因此該圖標不僅需要對應的偏移量,還需要針對這兩種情況先後不同順序添加在 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Stack","attrs":{}},{"type":"text","text":" 棧內;","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"List _listWid = [Container(height: size)];\nif (isCoverUp ?? true) {\n if (urlList != null) {\n if (endIcon != null) {\n _listWid.add(_itemWid(isAsset ?? false, null, urlList.length, endIcon));\n }\n for (int i = urlList.length - 1; i >= 0; i--) {\n _listWid.add(_itemWid(isAsset ?? false, urlList[i], i, null));\n }\n } else if (mixUrlList != null) {\n if (endIcon != null) {\n _listWid.add(_itemWid(isAsset ?? false, null, mixUrlList.length, endIcon));\n }\n for (int i = mixUrlList.length - 1; i >= 0; i--) {\n _listWid.add(_itemWid(mixUrlList[i].keys.first, mixUrlList[i].values.first, i, null));\n }\n }\n} else {\n if (urlList != null) {\n for (int i = 0; i < urlList.length; i++) {\n _listWid.add(_itemWid(isAsset ?? false, urlList[i], i, null));\n }\n if (endIcon != null) {\n _listWid.add(_itemWid(isAsset ?? false, null, urlList.length, endIcon));\n }\n } else if (mixUrlList != null) {\n for (int i = 0; i < mixUrlList.length; i++) {\n _listWid.add(_itemWid(mixUrlList[i].keys.first ?? false, mixUrlList[i].values.first, i, null));\n }\n if (endIcon != null) {\n _listWid.add(_itemWid(isAsset ?? false, null, mixUrlList.length, endIcon));\n }\n }\n}\n\nreturn Stack(children: _listWid);\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/85/8510aba415e35212b15305a0f98a98a3.webp","alt":null,"title":"","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"4. 自定義 Border","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"    對於個性化需求,是否需要邊框,以及邊框顏色和粗細,這些屬於相對簡單邊緣的功能點;小菜予以補充,添加了 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"isBorder、borderColor 和 borderWidth","attrs":{}},{"type":"text","text":" 屬性;","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"return Positioned(\n left: (spaceWidth ?? _kSpaceWidth) * index,\n child: Container(\n width: size, height: size,\n decoration: BoxDecoration(\n border: (isBorder ?? false) == false ? null : Border.all(color: borderColor ?? _kBorderColor, width: borderWidth ?? _kBorderWidth),\n color: Colors.grey, shape: BoxShape.circle),\n child: PhysicalModel(\n color: Colors.transparent, shape: BoxShape.circle,\n clipBehavior: Clip.antiAlias, borderRadius: BorderRadius.all(Radius.circular(20.0)),\n child: Container(width: size, height: size,\n child: endIcon ?? (asset ? Image.asset(url) : Image.network(url))))));\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/fe/fe88a7e76e0cf8b92d3d202ae8aa2adc.webp","alt":null,"title":"","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"CircleAvatar","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"    小菜在設置圓形頭像時瞭解到 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"CircleAvatar","attrs":{}},{"type":"text","text":" 組件,","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Flutter","attrs":{}},{"type":"text","text":" 提供了很多繪製圓形的方法,小菜趁此機會簡單學習一下 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"CircleAvatar","attrs":{}},{"type":"text","text":";","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"CircleAvatar","attrs":{}},{"type":"text","text":" 比較特殊,通常用於用戶圖片和內容一同展示,且爲了保持效果一致,給定用戶的姓名縮寫應始終與相同的背景色配對;","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"源碼分析","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"const CircleAvatar({\n Key key,\n this.child, // 子 Widget\n this.backgroundColor, // 背景色\n this.backgroundImage, // 背景圖\n this.foregroundColor, // 子 Widget 顏色\n this.radius, // 半徑\n this.minRadius, // 最小半徑\n this.maxRadius, // 最大半徑\n})\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"    簡單分析源碼可得,主要是通過 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"BoxConstraints","attrs":{}},{"type":"text","text":" 來限制最大最小半徑,而 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"backgroundImage","attrs":{}},{"type":"text","text":" 來設置背景圖;","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"案例嘗試","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"1. child","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"    ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"child","attrs":{}},{"type":"text","text":" 爲 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"CircleAvatar","attrs":{}},{"type":"text","text":" 中居中展示的子 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Widget","attrs":{}},{"type":"text","text":",一般是 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"TextView","attrs":{}},{"type":"text","text":",用於展示姓名等;若設置圖片則不會進行圓形裁切;","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"return CircleAvatar(radius: 40.0, child: Text(index == 0 ? 'ACE' : 'Hi'));\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/cd/cd01a2e81e0a5fc449b21833bac5f2a4.webp","alt":null,"title":"","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"2. backgroundImage","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"    ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"backgroundImage","attrs":{}},{"type":"text","text":" 爲 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"CircleAvatar","attrs":{}},{"type":"text","text":" 圖片背景,默認居中裁切,注意 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"backgroundImage","attrs":{}},{"type":"text","text":" 對應的是 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"ImageProvider","attrs":{}},{"type":"text","text":" 而非 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Widget","attrs":{}},{"type":"text","text":",因此加載圖片時只能採用 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"AssetImage","attrs":{}},{"type":"text","text":" 或 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"NetworkImage","attrs":{}},{"type":"text","text":" 方式;","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"return CircleAvatar(\n radius: 40.0,\n backgroundImage: index != 0\n ? NetworkImage('https://locadeserta.com/game/assets/images/background/landing/1.jpg')\n : AssetImage('images/icon_hzw01.jpg'));\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/c0/c0b543b80ac566db7fe85b223885917d.webp","alt":null,"title":"","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"3. backgroundColor & foregroundColor","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"    ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"backgroundColor","attrs":{}},{"type":"text","text":" 和 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"foregroundColor","attrs":{}},{"type":"text","text":" 分別對應背景顏色和子 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"child","attrs":{}},{"type":"text","text":" 顏色,除非兩個顏色均設置,否則兩個背景色會之間會自動匹配;默認 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"backgroundColor","attrs":{}},{"type":"text","text":" 對應 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Theme","attrs":{}},{"type":"text","text":" 的主題顏色;","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"return CircleAvatar(\n radius: 40.0,\n child: Text(index == 0 ? 'ACE' : 'Hi'),\n backgroundColor: (index == 0) ? null : Colors.deepOrange,\n foregroundColor: (index == 0) ? null : Colors.blue);\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/6f/6f7c7bcae2732f2a5411f0b038db568c.webp","alt":null,"title":"","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"4. radius & minRadius & maxRadius","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"    瞭解源碼可得,","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"CircleAvatar","attrs":{}},{"type":"text","text":" 是通過 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"BoxConstraints","attrs":{}},{"type":"text","text":" 來限制半徑範圍的;若設置 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"radius","attrs":{}},{"type":"text","text":" 則其餘兩個不生效;默認 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"minRadius","attrs":{}},{"type":"text","text":" 爲 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"20","attrs":{}},{"type":"text","text":" 像素密度;","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"return CircleAvatar(\n maxRadius: 50.0,\n child: Text('ACE'),\n backgroundColor: Colors.deepOrange,\n foregroundColor: Colors.white,\n backgroundImage: AssetImage('images/icon_hzw01.jpg'));\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/9b/9bd9f7780985d7903523f4deb4517951.webp","alt":null,"title":"","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"horizontalrule","attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"    ","attrs":{}},{"type":"link","attrs":{"href":"https://github.com/ACE-YANGCE/FlutterApp/blob/master/lib/page/series_circle_page.dart","title":"","type":null},"content":[{"type":"text","text":"SeriesCircleProfile & CircleAvatar 案例源碼","attrs":{}}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"horizontalrule","attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"    小菜對於系列摺疊頭像的自定義較爲簡單,沒有使用複雜的 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Canvas","attrs":{}},{"type":"text","text":" 繪製,而是通過 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Stack","attrs":{}},{"type":"text","text":" 和 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Positioned","attrs":{}},{"type":"text","text":" 進行展示,邏輯更爲簡單;如有錯誤,請多多指導!","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"來源: 阿策小和尚","attrs":{}}]}],"attrs":{}}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章