【问题标题】:How to change the expanded height in flutter?如何改变颤振的展开高度?
【发布时间】:2021-11-21 04:56:44
【问题描述】:

我是从 youtube 学习 Flutter UI 构建的新手。当我使用扩展小部件时,我发现输出不是我想要的,只是想问有没有办法限制扩展小部件的高度。 This is my current UI looks like.

SafeArea(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Align(
              alignment: Alignment.topRight,
              child: Container(
                alignment: Alignment.center,
                height: 52,
                width: 52,
                decoration: BoxDecoration(
                  color: Color(0xFFF2BEA1),
                  shape: BoxShape.circle,
                ),
                child: SvgPicture.asset("icons/icons/menu.svg"),
              ),
            ),
            Text(
              "\t\tMake Cents out of \n\t\tSolar",
              style: Theme.of(context).textTheme.subtitle1!.copyWith(
                  fontWeight: FontWeight.w900,
                  fontSize: 38,
                  color: Colors.blueGrey[900]),
            ),
            Padding(
              padding: const EdgeInsets.all(20.0),
              child: Container(
                padding: EdgeInsets.symmetric(horizontal: 30, vertical: 5),
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.circular(29.5),
                ),
                child: TextField(
                  decoration: InputDecoration(
                    hintText: "Search",
                    icon: SvgPicture.asset("icons/icons/search.svg"),
                    border: InputBorder.none,
                  ),
                ),
              ),
            ),
            Expanded(
              child: GridView.count(
                crossAxisCount: 2,
                childAspectRatio: .85,
                children: <Widget>[
                  CategoryCard(
                    title: "Promotion of the day",
                    svgSrc: "icons/icons/Hamburger.svg",
                    press: () {},
                  ),
                  CategoryCard(
                    title: "Promotion of the day",
                    svgSrc: "icons/icons/Hamburger.svg",
                    press: () {},
                  ),
                ],
              ),
            ),
            Container(
              padding: EdgeInsets.fromLTRB(20.0, 0, 0, 0),
              child: Text(
                "Solar Panel",
                style: Theme.of(context).textTheme.subtitle1!.copyWith(
                    fontWeight: FontWeight.w900,
                    fontSize: 20,
                    color: Colors.black),
              ),
            ),
          ],

我想知道有什么方法可以控制展开的小部件的高度,以便我的带有文本的容器可以连接到自定义类别卡的正下方。或者我应该使用另一个小部件而不是使用扩展小部件?

【问题讨论】:

    标签: android flutter user-interface


    【解决方案1】:

    要设置小部件的最小和最大尺寸(宽度和高度),您可以使用ConstrainedBox 小部件,了解更多信息here

    【讨论】:

    • 谢谢,ConstrainedBox 解决了我的问题。
    • @RogerWong 很高兴为您提供帮助,那么您能否将其标记为已接受的答案?
    【解决方案2】:

    您无法控制Expanded 小部件的高度。它占用了屏幕上所有可用的空间。我建议您使用SizedBox 并明确提供所需的高度并将宽度设置为double.infinity

    【讨论】:

    • 好的,刚刚意识到 Expanded 会占用全部空间。谢谢。
    【解决方案3】:

    根据定义,Expanded 小部件将填满所有剩余的屏幕空间。如果您想使用固定尺寸,可以使用 ContainerSizedBox 及其宽度和高度参数。

    为了更好地理解一般约束,我建议您查看this flutter documentation page all about constraints

    【讨论】:

    • 非常感谢。 ConstrainedBox 解决了我的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-07-20
    • 2019-02-12
    • 2019-06-09
    • 2021-05-12
    • 2020-11-25
    • 2019-05-19
    • 2020-01-26
    相关资源
    最近更新 更多