如何禁用容器子级展开?
所以我在脚手架(材料)中得到了一个容器。但是,如果我添加一个 ElevatedButton 作为此容器的子级,则 ElevatedButton 按钮将扩展到父容器的完整大小。我怎样才能避免它?
不知道是flutter安装的问题还是我的问题。
这是代码。
class Login extends StatefulWidget {
const Login({Key? key}) : super(key: key);
@override
_LoginState createState() => _LoginState();
}
class _LoginState extends State<Login> {
String status = "Status: OK";
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.teal,
appBar: AppBar(
backgroundColor: Colors.teal,
elevation: 0,
centerTitle: true,
automaticallyImplyLeading: false,
title: Text(status),
),
body: Center(
child: Column(
children: [
SubmitContainer(),
],
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
),
),
);
}
}
class InputContainer extends StatelessWidget {
const InputContainer({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container(
child: TextField(),
width: 200,
height: 200,
color: Colors.yellow,
);
}
}
class SubmitContainer extends StatelessWidget {
const SubmitContainer({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container(
child: SizedBox(
child: ElevatedButton(
child: Text("GO"),
onPressed: () {},
),
width: 300,
height: 50,
),
width: MediaQuery.of(context).size.width,
);
}
}
So i got a Container in a Scaffold (Material). But if i add a ElevatedButton as a child of this Container the ElevatedButton the Button Expands to the full size of the parent Container. How can i avoid it?
I don't know whether it's the fault of flutter installation or if it's on me.
Here's the code.
class Login extends StatefulWidget {
const Login({Key? key}) : super(key: key);
@override
_LoginState createState() => _LoginState();
}
class _LoginState extends State<Login> {
String status = "Status: OK";
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.teal,
appBar: AppBar(
backgroundColor: Colors.teal,
elevation: 0,
centerTitle: true,
automaticallyImplyLeading: false,
title: Text(status),
),
body: Center(
child: Column(
children: [
SubmitContainer(),
],
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
),
),
);
}
}
class InputContainer extends StatelessWidget {
const InputContainer({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container(
child: TextField(),
width: 200,
height: 200,
color: Colors.yellow,
);
}
}
class SubmitContainer extends StatelessWidget {
const SubmitContainer({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container(
child: SizedBox(
child: ElevatedButton(
child: Text("GO"),
onPressed: () {},
),
width: 300,
height: 50,
),
width: MediaQuery.of(context).size.width,
);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当我们使用
FittedBox
时,它会占据内容宽度。使用 FittedBox 作为按钮。When we use
FittedBox
it occupies the content width. Use FittedBox for Button.另一种可能的解决方案是将按钮包裹UnconstrainedBox 小部件:“一个对其子部件不施加任何限制的小部件,允许其以其“自然”尺寸呈现。”
Just one other possible solution is to wrap your button inside UnconstrainedBox widget: "A widget that imposes no constraints on its child, allowing it to render at its 'natural' size."
尝试使用
Center
小部件包装您的Container
小部件的子部件这样,即使容器本身很宽,内容也将具有自动宽度。
请从文档中查看此页面以获取更多信息
https://docs.flutter.dev/development/ui/layout/约束#example-3
Try wrapping your
Container
widget's child with aCenter
widgetThis way even if the Container itself is wide, the content will have automatic width
Check out this page from the documentation for more information
https://docs.flutter.dev/development/ui/layout/constraints#example-3
你做错了。在 SubmitContainer 的容器小部件内,您定义了
width: MediaQuery.of(context).size.width,
,这就是提升按钮拉伸以匹配全屏宽度的原因。只需使用执行以下操作:You did a mistake. Inside your container widget in SubmitContainer, you defined
width: MediaQuery.of(context).size.width,
that is why the elevated button stretched to match the full screen width. Just use do the following: