【问题标题】:Flutter Image with Gradient Border color带有渐变边框颜色的颤振图像
【发布时间】:2021-02-13 12:25:58
【问题描述】:

我怎样才能在 Flutter 中实现这样的目标:

【问题讨论】:

    标签: image flutter border


    【解决方案1】:

    只需将 Text 替换为 Image.network

    使用下面的代码

    UnicornOutlineButton(
       strokeWidth: 2,
       radius: 24,
       gradient: LinearGradient(
           colors: [Colors.black, Colors.redAccent]),
           child: Text('OMG', style: TextStyle(fontSize: 16)),
           onPressed: () {},
          ),
    

    创建一个名为UnicornOutlineButton的新类

    class CustomOutlineButton extends StatelessWidget {
      final _GradientPainter _painter;
      final Widget _child;
      final VoidCallback _callback;
      final double _radius;
    
      CustomOutlineButton({
        @required double strokeWidth,
        @required double radius,
        @required Gradient gradient,
        @required Widget child,
        @required VoidCallback onPressed,
      })  : this._painter = _GradientPainter(strokeWidth: strokeWidth, radius: radius, gradient: gradient),
            this._child = child,
            this._callback = onPressed,
            this._radius = radius;
    
      @override
      Widget build(BuildContext context) {
        return CustomPaint(
          painter: _painter,
          child: GestureDetector(
            behavior: HitTestBehavior.translucent,
            onTap: _callback,
            child: InkWell(
              borderRadius: BorderRadius.circular(_radius),
              onTap: _callback,
              child: Container(
                constraints: BoxConstraints(minWidth: 88, minHeight: 48),
                child: Row(
                  mainAxisSize: MainAxisSize.min,
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    _child,
                  ],
                ),
              ),
            ),
          ),
        );
      }
    }
    
    class _GradientPainter extends CustomPainter {
      final Paint _paint = Paint();
      final double radius;
      final double strokeWidth;
      final Gradient gradient;
    
      _GradientPainter({@required double strokeWidth, @required double radius, @required Gradient gradient})
          : this.strokeWidth = strokeWidth,
            this.radius = radius,
            this.gradient = gradient;
    
      @override
      void paint(Canvas canvas, Size size) {
        // create outer rectangle equals size
        Rect outerRect = Offset.zero & size;
        var outerRRect = RRect.fromRectAndRadius(outerRect, Radius.circular(radius));
    
        // create inner rectangle smaller by strokeWidth
        Rect innerRect = Rect.fromLTWH(strokeWidth, strokeWidth, size.width - strokeWidth * 2, size.height - strokeWidth * 2);
        var innerRRect = RRect.fromRectAndRadius(innerRect, Radius.circular(radius - strokeWidth));
    
        // apply gradient shader
        _paint.shader = gradient.createShader(outerRect);
    
        // create difference between outer and inner paths and draw it
        Path path1 = Path()..addRRect(outerRRect);
        Path path2 = Path()..addRRect(innerRRect);
        var path = Path.combine(PathOperation.difference, path1, path2);
        canvas.drawPath(path, _paint);
      }
    
      @override
      bool shouldRepaint(CustomPainter oldDelegate) => oldDelegate != this;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-18
      相关资源
      最近更新 更多