【问题标题】:Elevated Button height not increasing升高的按钮高度不增加
【发布时间】:2021-09-09 23:45:54
【问题描述】:

由于不推荐使用凸起按钮,因此我将其替换为提升按钮。但我不能增加高架按钮的高度。

class ZuzuButton extends StatelessWidget {
final Function onTapped;
final String name;
final double height;
final TextStyle textStyle;
final double radius;
final List<BoxShadow> shadow;
final Color color;
final bool enable;
ZuzuButton({this.onTapped,@required this.name,
  this.height,this.textStyle,this.radius,this.shadow,this.color,this.enable=true});
@override
Widget build(BuildContext context) {
  return Container(
    height: height==0?48.0:height,
    decoration: new BoxDecoration(
      borderRadius: BorderRadius.circular(radius!=null?radius:30.0),
      border: enable? Border.all(
        width: color!=null?0.0:1.0,
        color: color!=null?color:Color(0x407F16F0),
      ):null,
      boxShadow: enable?(shadow==null?[
        BoxShadow(
          color: Color(0x407F16F0),
          offset: Offset(0.0, 8.0),
          spreadRadius: 0,
          blurRadius: 20,
        ),
      ]:shadow):null,
    ),
    child: ElevatedButton(
      child: Container(
        child: Center(
          child: Text(name,style: textStyle!=null?textStyle:null,),
        ),
        height: height==0?48.0:height,
      ),
      onPressed: enable?onTapped:null,
      style: ButtonStyle(
        elevation:  MaterialStateProperty.resolveWith<double>(
              (Set<MaterialState> states) {
            return 0.0;
          },
        ),
        backgroundColor: MaterialStateProperty.resolveWith<Color>(
              (Set<MaterialState> states) {
            if (states.contains(MaterialState.pressed))
              return Color(0xffF7E86C);
            return enable?(color!=null?color:null):Color(0xffDBD9D2); // Use the component's default.
          },
        ),
        textStyle: MaterialStateProperty.resolveWith<TextStyle>(
              (Set<MaterialState> states) {
            if (states.contains(MaterialState.pressed))
              return ZuzuTopography.FF2_Button_Bold.copyWith(color: Colors.black);
            return ZuzuTopography.FF2_Button_Bold.copyWith(color: Colors.white); // Use the component's default.
          },
        ),
        shape: MaterialStateProperty.resolveWith<OutlinedBorder>(
              (Set<MaterialState> states) {
            // if (states.contains(MaterialState.pressed))
            //   return radius!=null? RoundedRectangleBorder(
            //           borderRadius: BorderRadius.circular(radius),
            //       ):null;
            return RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(radius!=null?radius:30.0),
            ); // Use the component's default.
          },
        ),
      ),

    ),
  );
}
}

我的输出。

如何让这个按钮占据它的容器高度?我在互联网上搜索解决方案,但找不到任何解决方案。我的代码中有什么建议吗?除了 Elevated Button 之外,Raised Button 还有其他选择吗?

【问题讨论】:

  • 你试过按钮主题小部件吗?

标签: flutter dart button height


【解决方案1】:

我刚开始使用高架按钮。对我来说,我只是用这个来改变高度:

            ElevatedButton(
                onPressed: () {},
                style: ElevatedButton.styleFrom(
                  minimumSize: Size(width, height) // put the width and height you want
                ),
                child: Text("NEXT"),
              )

【讨论】:

  • 有什么方法可以只增加最小高度而不增加宽度?
【解决方案2】:

您可以使用ConstrainedBox 来做同样的事情。请参考下面的代码。

ConstrainedBox(
            constraints: BoxConstraints.tightFor(width: 300, height: 200),
            child: ElevatedButton(
              child: Text('300 x 200'),
              onPressed: () {},
            ),
          ),

【讨论】:

    【解决方案3】:

    您可以简单地使用 fixedSize(width, height)。这是一个示例

                ElevatedButton(
                    onPressed: () {},
                    child: Text(
                      'submit',
                      
                    ),
                    style: ElevatedButton.styleFrom(
                      fixedSize: Size(90, 15),
                      primary: Colors.deepOrange,
                     
                    ),
                  )
    

    【讨论】:

      猜你喜欢
      • 2018-09-13
      • 2020-04-04
      • 1970-01-01
      • 2023-01-09
      • 2023-03-10
      • 1970-01-01
      • 2010-12-24
      • 1970-01-01
      相关资源
      最近更新 更多