首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >集装箱固定高度在颤振中不起作用

集装箱固定高度在颤振中不起作用
EN

Stack Overflow用户
提问于 2020-12-09 18:31:27
回答 2查看 2.7K关注 0票数 1

容器高度被设置为固定的40,但是一旦我在AppBar()中使用了该Widget,它就会占用所有可能的高度。下面是我的定制小部件的代码,它有固定的容器高度,

代码语言:javascript
运行
复制
class LPBorderButtonWithIcon extends StatelessWidget {
  final GestureTapCallback onPressed;
  final String text;
  final String iconAsset;
  final Color textColor;

  LPBorderButtonWithIcon(
      {@required this.onPressed,
      @required this.text,
      @required this.textColor,
      @required this.iconAsset});

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
        onTap: onPressed,
        child: Container(
          height: 40,
          decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(25),
              border: Border.all(color: Color(0XFFd8dce1))),
          child: Row(
            children: [
              WidthSizedBox(15),
              Image.asset(
                iconAsset,
                height: 14,
                width: 14,
              ),
              WidthSizedBox(5),
              Text(text,
                  style: TextStyle(
                      color: textColor,
                      fontSize: 12,
                      fontFamily: "GilroyMedium")),
              WidthSizedBox(15),
            ],
          ),
        ));
  }
}

我在这个屏幕上使用LPBorderButtonWithIcon()

代码语言:javascript
运行
复制
class CreateRulesScreen extends StatefulWidget {
  @override
  _CreateRulesScreenState createState() => _CreateRulesScreenState();
}

class _CreateRulesScreenState extends State<CreateRulesScreen> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        brightness: Brightness.light,
        backgroundColor: Colors.white,
        elevation: 1,
        centerTitle: false,
        titleSpacing: 0.0,
        leading: BackButton(
          color: LPColor.primary,
          onPressed: () {
            Navigator.of(context).pop();
          },
        ),
        title: Text(
          "Create Rule",
          style: LPStyle.titleStyle,
        ),
        actions: [
          Container(
            margin: EdgeInsets.only(top: 12, bottom: 12, right: 16),
            child: LPBorderButtonWithIcon(
              onPressed: null,
              text: "Create",
              textColor: Color(0XFF508ff4),
              iconAsset: "images/ic_publish.png",
            ),
          )
        ],
      ),

    );
  }
}

下面是自定义容器获取所有可能高度的结果。请让我知道如何设置固定高度,我的自定义小部件。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-12-09 18:39:25

将您的容器放置在一个对齐,Aling将迫使容器只占据它需要的空间。

代码语言:javascript
运行
复制
Align(
   child: Container(
   height: 20,
   width: 30,
   color: Colors.white,
  ),
)
票数 7
EN

Stack Overflow用户

发布于 2020-12-10 18:04:29

父小部件占用了绘制小部件的全部可用空间,这里Container是父小部件,它占用所有可用的空间,因此要给Container以高度,需要将其放置在指定小部件的x,y位置的小部件中,以使其绘制。

代码语言:javascript
运行
复制
Container(
    height: 40, // Its not going to apply height as it's parent widget
)

因此,要计算出上面的代码,您必须将容器对齐到任何其他小部件,如Center、Align等。

例如:

代码语言:javascript
运行
复制
Scaffold(
  body: Container(
    height: 600,
    color: Colors.red,
    child: Container(
      height: 200,
      color: Colors.yellow,
    ),
  ),
);

上面的例子子容器不会在200高度上画黄色,它将占用整个600高度的空间。

输出:

为了解决这个问题,我们给子Container分配了一些小部件,这样它就可以得到x,y位置来开始绘制子小部件。这里使用Center小部件。

例:

代码语言:javascript
运行
复制
Scaffold(
      body: Container(
        height: 600,
        color: Colors.red,
        child: Center(
          child: Container(
            height: 200,
            color: Colors.yellow,
          ),
        ),
      ),
    );

输出:

一些限制:

  • 小部件只能在其父部件给它的约束范围内决定它自己的大小。这意味着一个小部件通常不能有它想要的任何大小。

  • A小部件不能知道,也不能决定自己在屏幕中的位置,因为小部件的位置由小部件的父部件决定。

由于父部件的大小和位置也取决于它自己的父部件,所以如果不考虑整个树,就不可能精确地定义任何小部件的大小和位置。

  • 如果一个子对象想要与其父级不同的大小,而父级没有足够的信息来对齐它,则可能忽略子级的大小。在定义对齐时要明确。

参考链接: https://flutter.dev/docs/development/ui/layout/constraints

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65222802

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档