我想根據下圖創建兩個專案(文本上方的圓圈,水平居中):

我創建了以下代碼:
Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
IconButton(
icon: const Icon(
Icons.circle,
color: Colors.white,
size: 40,
),
onPressed: () {},
),
const Text(
'text',
style: TextStyle(
color: Colors.white,
fontSize: 16,
),
),
],
)
但是,結果如下:

我發現問題出在sizeof 上IconButton。當我洗掉這條線時,圖示會變小(如預期的那樣),下面的文本將居中。似乎在增加圖示大小時,它不是從圖示的中心點(向各個方向均勻地)增加,而是向右增加。有人可以幫我嗎?Text是否可以在不中斷居中的情況下更改圖示的大小?或者有沒有辦法根據需要將其居中?謝謝你。
uj5u.com熱心網友回復:
我發現添加padding: EdgeInsets.zero. 你可以做什么來代替它以IconButton指定SizedBox大小包裝。
問題是 IconButton 沒有正確對齊。更多資訊可以在這里找到
Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
SizedBox(
height: 50,
width: 50,
child: IconButton(
padding: EdgeInsets.zero,
alignment: Alignment.center,
icon: const Icon(
Icons.circle,
color: Colors.blue,
size: 50,
),
onPressed: () {},
),
),
const Text(
'text',
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.blue,
fontSize: 16,
),
),
],
)
uj5u.com熱心網友回復:
實際上,Axid 列居中,但IconButton導致問題。
嘗試這個:
Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
IconButton(
padding: EdgeInsets.zero, // add this
icon: const Icon(
Icons.circle,
color: Colors.white,
size: 40,
),
onPressed: () {},
),
const Text(
'text',
style: TextStyle(
color: Colors.white,
fontSize: 16,
),
),
],
),
默認情況下IconButton帶有初始填充,洗掉它將顯示它們水平居中。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/533243.html
標籤:安卓IOS扑镖
