如何禁用容器子级展开?

发布于 2025-01-10 19:33:34 字数 1805 浏览 0 评论 0原文

所以我在脚手架(材料)中得到了一个容器。但是,如果我添加一个 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,
    );
  }
}

最终结果如下所示: Google Pixel 3a XL API 23

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,
    );
  }
}

And the endresult looks like this:
Google Pixel 3a XL API 23

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

始终不够爱げ你 2025-01-17 19:33:34

当我们使用 FittedBox 时,它会占据内容宽度。使用 FittedBox 作为按钮。

Container(
        child: SizedBox(
          child: FittedBox(
            child: ElevatedButton(
              child: Text("GO"),
              onPressed: () {},
            ),
          ),
          width: 300,
          height: 50,
        ),
        width: MediaQuery.of(context).size.width,
      );

When we use FittedBox it occupies the content width. Use FittedBox for Button.

Container(
        child: SizedBox(
          child: FittedBox(
            child: ElevatedButton(
              child: Text("GO"),
              onPressed: () {},
            ),
          ),
          width: 300,
          height: 50,
        ),
        width: MediaQuery.of(context).size.width,
      );
在你怀里撒娇 2025-01-17 19:33:34

另一种可能的解决方案是将按钮包裹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."

强者自强 2025-01-17 19:33:34

尝试使用 Center 小部件包装您的 Container 小部件的子部件
这样,即使容器本身很宽,内容也将具有自动宽度。

Container(
  child: Center(
    child: //...
  ),
)

请从文档中查看此页面以获取更多信息
https://docs.flutter.dev/development/ui/layout/约束#example-3

Try wrapping your Container widget's child with a Center widget
This way even if the Container itself is wide, the content will have automatic width

Container(
  child: Center(
    child: //...
  ),
)

Check out this page from the documentation for more information
https://docs.flutter.dev/development/ui/layout/constraints#example-3

天暗了我发光 2025-01-17 19:33:34

你做错了。在 SubmitContainer 的容器小部件内,您定义了 width: MediaQuery.of(context).size.width,,这就是提升按钮拉伸以匹配全屏宽度的原因。只需使用执行以下操作:

SizedBox(
        child: ElevatedButton(
          child: Text("GO"),
          onPressed: () {},
        ),
        width: 150,
        height: 50,
      );

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:

SizedBox(
        child: ElevatedButton(
          child: Text("GO"),
          onPressed: () {},
        ),
        width: 150,
        height: 50,
      );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文