我有以下代碼:
containers.add(Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
color: Colors.blue,
),
margin: const EdgeInsets.all(10),
width: MediaQuery.of(context).size.width/2-20,
height: MediaQuery.of(context).size.width/2-20,
child: Column(
children: [
Text(
value2,
style: TextStyle(
inherit: false,
color: Colors.white,
fontSize: MediaQuery.of(context).size.width/2,
fontWeight: FontWeight.bold,
)
),
]
)
));
});
}
});
容器本身進入一個串列,該串列被傳遞到 GridView 最終成為螢屏下方的 2 寬網格。我想讓文本高度是由 width 和 height 定義的容器高度的一半MediaQuery.of(context).size.width/2-20。這可能嗎?謝謝
uj5u.com熱心網友回復:
您可以Expanded與FittedBox以下一起使用:
class HalfHeightText extends StatelessWidget {
final String label;
const HalfHeightText({required this.label});
Widget build(BuildContext context) {
return Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
color: Colors.blue,
),
margin: const EdgeInsets.all(10),
width: MediaQuery.of(context).size.width / 2 - 20,
height: MediaQuery.of(context).size.width / 2 - 20,
child: Column(
children: [
// ===========================================
Expanded(
child: FittedBox(
fit: BoxFit.contain,
child: Text(
label,
style: const TextStyle(
inherit: false,
color: Colors.white,
fontWeight: FontWeight.bold,
),
),
),
),
const Expanded(child: SizedBox.shrink()),
// ===========================================
],
),
);
}
}
結果:
HalfHeightText(label: "Text")

uj5u.com熱心網友回復:
使用寬度:MediaQuery.of(context).size.width 0.50,它的平均值像寬度為 50% 的用戶,否則 MediaQuery.of(context).size.width 0.25,平均值為 25%
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/369388.html
