flutter 虚线边框

 使用方式跟  BoxDecoration 一致,是根据BoxDecoration修改来的,新增四个参数:

this.strokeWidth = 1.0,// 虚线高度
this.gap = 5.0,//虚线的点的长度和间隔
this.dashedColor,//虚线颜色
this.dawDashed = true,//是否画虚线,默认为画虚线不画边框

需要注意的是,虚线和边框不能共存,如果不画虚线,dawDashed设为 false。

使用:

decoration: const DashedDecoration(
            dashedColor: Colors.yellow,
            borderRadius: const BorderRadius.all(Radius.circular(4.0)),
            ),

 

代码:

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'dart:math' as math;

import 'package:saler_incoming/utils/LogUtils.dart';

class DashedDecoration extends Decoration {
  /// Creates a box decoration.
  ///
  /// * If [color] is null, this decoration does not paint a background color.
  /// * If [image] is null, this decoration does not paint a background image.
  /// * If [border] is null, this decoration does not paint a border.
  /// * If [borderRadius] is null, this decoration uses more efficient background
  ///   painting commands. The [borderRadius] argument must be null if [shape] is
  ///   [BoxShape.circle].
  /// * If [boxShadow] is null, this decoration does not paint a shadow.
  /// * If [gradient] is null, this decoration does not paint gradients.
  /// * If [backgroundBlendMode] is null, this decoration paints with [BlendMode.srcOver]
  ///
  /// The [shape] argument must not be null.
  const DashedDecoration({
    this.color,
    this.image,
    this.border,
    this.borderRadius,
    this.boxShadow,
    this.gradient,
    this.backgroundBlendMode,
    this.shape = BoxShape.rectangle,
    this.strokeHeight = 1.0,
    this.gap = 5.0,
    this.dashedColor,
    this.dawDashed = true,
  })  : assert(shape != null),
        assert(
            backgroundBlendMode == null || color != null || gradient != null,
            "backgroundBlendMode applies to DashedDecoration's background color or "
            'gradient, but no color or gradient was provided.');

  /// Creates a copy of this object but with the given fields replaced with the
  /// new values.
  DashedDecoration copyWith({
    Color color,
    DecorationImage image,
    BoxBorder border,
    BorderRadiusGeometry borderRadius,
    List<BoxShadow> boxShadow,
    Gradient gradient,
    BlendMode backgroundBlendMode,
    BoxShape shape,
  }) {
    return DashedDecoration(
      color: color ?? this.color,
      image: image ?? this.image,
      border: border ?? this.border,
      borderRadius: borderRadius ?? this.borderRadius,
      boxShadow: boxShadow ?? this.boxShadow,
      gradient: gradient ?? this.gradient,
      backgroundBlendMode: backgroundBlendMode ?? this.backgroundBlendMode,
      shape: shape ?? this.shape,
    );
  }

  @override
  bool debugAssertIsValid() {
    assert(shape != BoxShape.circle ||
        borderRadius == null); // Can't have a border radius if you're a circle.
    return super.debugAssertIsValid();
  }

  /// The color to fill in the background of the box.
  ///
  /// The color is filled into the [shape] of the box (e.g., either a rectangle,
  /// potentially with a [borderRadius], or a circle).
  ///
  /// This is ignored if [gradient] is non-null.
  ///
  /// The [color] is drawn under the [image].
  final Color color;

  /// An image to paint above the background [color] or [gradient].
  ///
  /// If [shape] is [BoxShape.circle] then the image is clipped to the circle's
  /// boundary; if [borderRadius] is non-null then the image is clipped to the
  /// given radii.
  final DecorationImage image;

  /// A border to draw above the background [color], [gradient], or [image].
  ///
  /// Follows the [shape] and [borderRadius].
  ///
  /// Use [Border] objects to describe borders that do not depend on the reading
  /// direction.
  ///
  /// Use [BoxBorder] objects to describe borders that should flip their left
  /// and right edges based
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
Flutter 中绘制虚线可以使用 `CustomPaint` 和 `Path` 绘制。 下面是一个绘制虚线的示例代码: ```dart import 'dart:ui'; import 'package:flutter/material.dart'; class DashedLine extends StatelessWidget { final double dashedWidth; final double dashedHeight; final Color color; const DashedLine({ Key key, @required this.dashedWidth, @required this.dashedHeight, this.color = Colors.grey, }) : super(key: key); @override Widget build(BuildContext context) { return CustomPaint( painter: _DashedLinePainter( dashedWidth: dashedWidth, dashedHeight: dashedHeight, color: color, ), ); } } class _DashedLinePainter extends CustomPainter { final double dashedWidth; final double dashedHeight; final Color color; _DashedLinePainter({ @required this.dashedWidth, @required this.dashedHeight, @required this.color, }); @override void paint(Canvas canvas, Size size) { final paint = Paint() ..color = color ..strokeWidth = dashedHeight ..style = PaintingStyle.stroke ..strokeCap = StrokeCap.round; final path = Path(); double startX = 0.0; double endX = dashedWidth; while (endX < size.width) { path.moveTo(startX, dashedHeight / 2); path.lineTo(endX, dashedHeight / 2); startX += dashedWidth * 2; endX += dashedWidth * 2; } canvas.drawPath(path, paint); } @override bool shouldRepaint(_DashedLinePainter oldDelegate) { return oldDelegate.dashedWidth != dashedWidth || oldDelegate.dashedHeight != dashedHeight || oldDelegate.color != color; } } ``` 使用方式: ```dart DashedLine( dashedWidth: 5, dashedHeight: 1, color: Colors.grey, ) ``` 其中,`dashedWidth` 表示虚线的宽度,`dashedHeight` 表示虚线的高度,`color` 表示虚线的颜色。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值