代码之家  ›  专栏  ›  技术社区  ›  Rick

如何创建带有圆角和渐变背景的凸起按钮?

  •  0
  • Rick  · 技术社区  · 6 年前

    我一直试图创建一个凸起的按钮与圆角,梯度背景,但没有成功。我只能执行其中一个。两个小时过去了,我自己还没有找到解决方案,如何实现圆角和渐变背景。

    下面是我的代码,我最新尝试实现一个凸起的按钮与圆角,梯度背景。

    GradientButton的自定义类

    class RaisedGradientButton extends StatelessWidget {
      final Widget child;
      final Gradient gradient;
      final double width;
      final double height;
      final Function onPressed;
    
      const RaisedGradientButton({
        Key key,
        @required this.child,
        this.gradient,
        this.width = double.infinity,
        this.height = 50.0,
        this.onPressed,
      }) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return Container(
          width: width,
          height: 50.0,
          decoration: BoxDecoration(
            gradient: new LinearGradient(
              colors: [
                Colors.blue,
                Colors.red,
              ],
              begin: FractionalOffset.centerLeft,
              end: FractionalOffset.centerRight,
            ),
          ),
          child: Material(
            shape: new RoundedRectangleBorder(
                borderRadius: new BorderRadius.circular(128.0)),
    //        color: Colors.transparent,
            child: InkWell(
                onTap: onPressed,
                child: Center(
                  child: child,
                )),
          ),
        );
      }
    }
    

    RaisedGradientButton(
        onPressed: navigateToNextPage,
            child: Text("Select Community"),
    )
    

    外观:

    enter image description here

    0 回复  |  直到 6 年前
        1
  •  3
  •   David Peters Debanjan    5 年前

    有一个简单的解决办法。向按钮和按钮内部的容器添加相同的边界半径。这里有一个简单的例子。

    RaisedButton(
       onPressed: () {},
       textColor: Colors.white,
       color: Colors.transparent,
       padding: EdgeInsets.all(0),
       shape: RoundedRectangleBorder(
          borderRadius: new BorderRadius.circular(18.0),
       ),
       child: Container(
          decoration: BoxDecoration(
             borderRadius: BorderRadius.circular(18),
             gradient: LinearGradient(
                colors: <Color>[
                   Colors.black38,
                   Colors.black26,
                   Colors.white38,
                ],
             ),
          ),
          padding: const EdgeInsets.fromLTRB(24, 12, 24, 12),
          child: const Text('Sign In', style: TextStyle(fontSize: 20)),
       ),
    ),
    

    screenshot

        2
  •  2
  •   foxanna    6 年前

    Container 在按钮下面有一个渐变 Stack ClipRRect

    class RaisedGradientButton extends StatelessWidget {
      final Widget child;
      final Gradient gradient;
      final double width;
      final double height;
      final Function onPressed;
      final borderRadius = BorderRadius.circular(128.0);
    
      RaisedGradientButton({
        Key key,
        @required this.child,
        Gradient gradient,
        this.width = double.infinity,
        this.height = 50.0,
        this.onPressed,
      })  : this.gradient = gradient ??
                LinearGradient(
                  colors: [
                    Colors.blue,
                    Colors.red,
                  ],
                  begin: FractionalOffset.centerLeft,
                  end: FractionalOffset.centerRight,
                ),
            super(key: key);
    
      @override
      Widget build(BuildContext context) => Stack(
            children: [
              Positioned.fill(
                child: ClipRRect(
                  borderRadius: borderRadius,
                  child: Container(
                    width: width,
                    height: height,
                    decoration: BoxDecoration(
                      gradient: gradient,
                    ),
                  ),
                ),
              ),
              Container(
                width: width,
                height: height,
                child: RaisedButton(
                  shape: RoundedRectangleBorder(
                    borderRadius: borderRadius,
                  ),
                  padding: EdgeInsets.zero,
                  child: Center(child: child),
                  onPressed: onPressed,
                  color: Colors.transparent,
                ),
              ),
            ],
          );
    }
    
        3
  •  0
  •   Rick    6 年前

    如果有人遇到同样的问题。这是我如何解决它的代码。

    class GradientButton extends StatelessWidget {
      final Widget child;
    
      // final Gradient gradient;
      final double width;
      final double height;
      final bool isEnabled;
      final Function onPressed;
    
      const GradientButton({
        Key key,
        @required this.child,
        // this.gradient,
        this.isEnabled,
        this.width,
        this.height,
        this.onPressed,
      }) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        Color _statusColor;
        if (isEnabled != null) {
          // Show gradient color by making material widget transparent
          if (isEnabled == true) {
            _statusColor = Colors.transparent;
          } else {
            // Show grey color if isEnabled is false
            _statusColor = Colors.grey;
          }
        } else {
          // Show grey color if isEnabled is null
          _statusColor = Colors.transparent;
        }
    
        return Container(
          width: width,
          decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(8),
            gradient: LinearGradient(
              colors: [
                Color(0xFF3186E3),
                Color(0xFF1D36C4),
              ],
              begin: FractionalOffset.centerLeft,
              end: FractionalOffset.centerRight,
            ),
          ),
          child: Material(
              shape: RoundedRectangleBorder(
                  borderRadius: new BorderRadius.circular(4)),
              color: _statusColor,
              child: InkWell(
                  borderRadius: BorderRadius.circular(32),
                  onTap: onPressed,
                  child: Padding(
                    padding: EdgeInsets.fromLTRB(24, 16, 24, 16),
                    child: Center(
                      child: child,
                    ),
                  ))),
        );
      }
    }