为什么列中的按钮不能全宽,而外部按钮可以全宽?

huangapple go评论50阅读模式
英文:

Why can't the buttons in the column be full-width, but the outside buttons can be full-width?

问题

这是两个相同的按钮。
外部按钮的宽度与页面的宽度相同。
内部按钮的宽度是可能的最小宽度。
我想让内部按钮的宽度与卡片相同,我该如何更改?
如何更新?

import 'package:flutter/material.dart';

class RoomListPage extends StatefulWidget {
  const RoomListPage({Key? key}) : super(key: key);

  @override
  _RoomListPageState createState() => _RoomListPageState();
}

class _RoomListPageState extends State<RoomListPage> {
  List roomList = [
    {
      "title": "卧室",
      "type": "卧室",
      "description": "这是卧室",
    },
    {
      "title": "厨房",
      "type": "厨房",
      "description": "这是厨房",
    },
    {
      "title": "浴室",
      "type": "浴室",
      "description": "这是浴室",
    },
  ];
  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.all(20.0),
      child: ListView(children: [
        const SizedBox(height: 20),
        ElevatedButton(onPressed: () {}, child: const Text('添加区域')), // 外部按钮 - 全宽度
        const SizedBox(height: 20),
        ...roomList.map(
          (room) => Column(
            children: [
              ElevatedButton(onPressed: () {}, child: const Text('添加区域')), // 内部按钮 - 最小宽度
              Card(
                  child: Column(
                children: [
                  Text(room['title'] as String),
                  Text(room['description'] as String),
                ],
              )),
              const SizedBox(height: 20)
            ],
          ),
        ),
      ]),
    );
  }
}
英文:

These are two identical buttons
The width of the outer button is the same as the width of the page
The width of the inner button is the smallest possible width
I want the inner button to be the same width as the card, how do I change this?
How do I update?

import &#39;package:flutter/material.dart&#39;;

class RoomListPage extends StatefulWidget {
  const RoomListPage({Key? key}) : super(key: key);

  @override
  _RoomListPageState createState() =&gt; _RoomListPageState();
}

class _RoomListPageState extends State&lt;RoomListPage&gt; {
  List roomList = [
    {
      &quot;title&quot;: &quot;bedroom&quot;,
      &quot;type&quot;: &quot;bedroom&quot;,
      &quot;description&quot;: &quot;this is bedroom&quot;,
    },
    {
      &quot;title&quot;: &quot;kitchen&quot;,
      &quot;type&quot;: &quot;kitchen&quot;,
      &quot;description&quot;: &quot;this is kitchen&quot;,
    },
    {
      &quot;title&quot;: &quot;bathroom&quot;,
      &quot;type&quot;: &quot;bathroom&quot;,
      &quot;description&quot;: &quot;this is bathroom&quot;,
    },
  ];
  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.all(20.0),
      child: ListView(children: [
        const SizedBox(height: 20),
        ElevatedButton(onPressed: () {}, child: const Text(&#39;Add region&#39;)), // outer button - full width
        const SizedBox(height: 20),
        ...roomList.map(
          (room) =&gt; Column(
            children: [
              ElevatedButton(onPressed: () {}, child: const Text(&#39;Add region&#39;)), // inner button - min width
              Card(
                  child: Column(
                children: [
                  Text(room[&#39;title&#39;] as String),
                  Text(room[&#39;description&#39;] as String),
                ],
              )),
              const SizedBox(height: 20)
            ],
          ),
        ),
      ]),
    );
  }
}

答案1

得分: 0

尝试将ElevatedButton包装在一个Row中。您可以在https://docs.flutter.dev/ui/layout/constraints了解更多关于Flutter约束的信息。

英文:

Try wrapping the ElevatedButton in a Row. You can learn more about Flutter constraints at https://docs.flutter.dev/ui/layout/constraints

huangapple
  • 本文由 发表于 2023年7月18日 10:47:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76709232.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定