容器高度被设置为固定的40,但是一旦我在AppBar()中使用了该Widget,它就会占用所有可能的高度。下面是我的定制小部件的代码,它有固定的容器高度,
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(),
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",
            ),
          )
        ],
      ),
    );
  }
}下面是自定义容器获取所有可能高度的结果。请让我知道如何设置固定高度,我的自定义小部件。

发布于 2020-12-09 18:39:25
将您的容器放置在一个对齐,Aling将迫使容器只占据它需要的空间。
Align(
   child: Container(
   height: 20,
   width: 30,
   color: Colors.white,
  ),
)发布于 2020-12-10 18:04:29
父小部件占用了绘制小部件的全部可用空间,这里Container是父小部件,它占用所有可用的空间,因此要给Container以高度,需要将其放置在指定小部件的x,y位置的小部件中,以使其绘制。
Container(
    height: 40, // Its not going to apply height as it's parent widget
)因此,要计算出上面的代码,您必须将容器对齐到任何其他小部件,如Center、Align等。
例如:
Scaffold(
  body: Container(
    height: 600,
    color: Colors.red,
    child: Container(
      height: 200,
      color: Colors.yellow,
    ),
  ),
);上面的例子子容器不会在200高度上画黄色,它将占用整个600高度的空间。
输出:

为了解决这个问题,我们给子Container分配了一些小部件,这样它就可以得到x,y位置来开始绘制子小部件。这里使用Center小部件。
例:
Scaffold(
      body: Container(
        height: 600,
        color: Colors.red,
        child: Center(
          child: Container(
            height: 200,
            color: Colors.yellow,
          ),
        ),
      ),
    );输出:

一些限制:
由于父部件的大小和位置也取决于它自己的父部件,所以如果不考虑整个树,就不可能精确地定义任何小部件的大小和位置。
参考链接: https://flutter.dev/docs/development/ui/layout/constraints
https://stackoverflow.com/questions/65222802
复制相似问题