容器高度被设置为固定的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,
  ),
)https://stackoverflow.com/questions/65222802
复制相似问题