我有以下完整的簡單代碼
import 'package:flutter/material.dart';
class A2 extends StatefulWidget {
const A2({Key? key}) : super(key: key);
@override
State<A2> createState() => _A2State();
}
class _A2State extends State<A2> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
color: Colors.red,
),
);
}
}
好吧,現在我的紅色容器顏色已滿
問題:如何使用以下空間損失控制使容器中心的一些空間無色?

uj5u.com熱心網友回復:
我認為您可以decoration在 Container 上使用。如果您需要使用 Stack,請使用對齊小部件包裝子項。容器padding將處理間距。
class A2 extends StatefulWidget {
const A2({Key? key}) : super(key: key);
@override
State<A2> createState() => _A2State();
}
class _A2State extends State<A2> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: LayoutBuilder(
builder: (p0, constraints) {
return Stack(
children: [
Align(
child: Container(
width: constraints.maxWidth * .3,
height: constraints.maxHeight * .7,
decoration: BoxDecoration(
border: Border.all(color: Colors.red, width: 12),
),
alignment: Alignment.center,
padding: EdgeInsets.all(20),
child: Text(
'some part of parent ',
style: TextStyle(color: Colors.black, fontSize: 18),
),
),
),
],
);
},
),
);
}
}
uj5u.com熱心網友回復:
您可以通過提供邊框來實作這種方式Container
這是代碼:
Container(
height: MediaQuery.of(context).size.height,
width: MediaQuery.of(context).size.width,
child: const Center(
child: Text(
'some part of parent',
style: TextStyle(color: Colors.red),
),
),
decoration: BoxDecoration(
color: Colors.white,
border: Border.all(
color: Colors.red,
width: 100,
),
),
)

uj5u.com熱心網友回復:
class A2 extends StatefulWidget {
const A2({Key? key}) : super(key: key);
@override
State<A2> createState() => _A2State();
}
class _A2State extends State<A2> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(
children: [
Container(
color: Colors.red,
child: Center(
child: Container(
color: Colors.white,
height: MediaQuery.of(context).size.height - 200,
width: MediaQuery.of(context).size.width - 150,
)),
),
const Center(
child: Text(
'some part of parent ',
style: TextStyle(color: Colors.black, fontSize: 18),
),
),
],
));
}
}
在堆疊中,第一個小部件在后臺,最后一個小部件在前面
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/518257.html
標籤:扑镖颤振布局
上一篇:飛鏢/顫振列印地圖中的單個值
下一篇:如何在顫動中驗證下拉欄位
