首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在flutter中创建框周围的虚线边框?

如何在flutter中创建框周围的虚线边框?
EN

Stack Overflow用户
提问于 2019-03-27 17:32:10
回答 6查看 38K关注 0票数 35

我正在使用flutter在我的应用程序中构建一个方框布局列表。我希望框周围有虚线边框。我已经使用card小部件创建了这些框。但是,如何在框周围设置虚线边框?

EN

回答 6

Stack Overflow用户

发布于 2019-03-30 12:00:37

编辑

我已经将其作为一个包添加到了pub中。

现在,你要做的就是

代码语言:javascript
复制
DottedBorder(
  color: Colors.black,
  gap: 3,
  strokeWidth: 1,
  child: FlutterLogo(size: 148),
)

工作解决方案已过时

就像tomerpacificthis answer中所说的那样,Flutter目前还没有虚线边框的默认实现。

我昨天工作了一段时间,最终想出了一个使用CustomPainter的解决方案。希望这对某些人有帮助。

DashedRect添加到容器中,如下所示

代码语言:javascript
复制
class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Container(
          height: 400,
          width: 300,
          color: Colors.black12,
          child: DashedRect(color: Colors.red, strokeWidth: 2.0, gap: 3.0,),
        ),
      ),
    );
  }
}

DashedRect.dart

代码语言:javascript
复制
import 'package:flutter/material.dart';
import 'dart:math' as math;

class DashedRect extends StatelessWidget {
  final Color color;
  final double strokeWidth;
  final double gap;

  DashedRect(
      {this.color = Colors.black, this.strokeWidth = 1.0, this.gap = 5.0});

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Padding(
        padding: EdgeInsets.all(strokeWidth / 2),
        child: CustomPaint(
          painter:
              DashRectPainter(color: color, strokeWidth: strokeWidth, gap: gap),
        ),
      ),
    );
  }
}

class DashRectPainter extends CustomPainter {
  double strokeWidth;
  Color color;
  double gap;

  DashRectPainter(
      {this.strokeWidth = 5.0, this.color = Colors.red, this.gap = 5.0});

  @override
  void paint(Canvas canvas, Size size) {
    Paint dashedPaint = Paint()
      ..color = color
      ..strokeWidth = strokeWidth
      ..style = PaintingStyle.stroke;

    double x = size.width;
    double y = size.height;

    Path _topPath = getDashedPath(
      a: math.Point(0, 0),
      b: math.Point(x, 0),
      gap: gap,
    );

    Path _rightPath = getDashedPath(
      a: math.Point(x, 0),
      b: math.Point(x, y),
      gap: gap,
    );

    Path _bottomPath = getDashedPath(
      a: math.Point(0, y),
      b: math.Point(x, y),
      gap: gap,
    );

    Path _leftPath = getDashedPath(
      a: math.Point(0, 0),
      b: math.Point(0.001, y),
      gap: gap,
    );

    canvas.drawPath(_topPath, dashedPaint);
    canvas.drawPath(_rightPath, dashedPaint);
    canvas.drawPath(_bottomPath, dashedPaint);
    canvas.drawPath(_leftPath, dashedPaint);
  }

  Path getDashedPath({
    @required math.Point<double> a,
    @required math.Point<double> b,
    @required gap,
  }) {
    Size size = Size(b.x - a.x, b.y - a.y);
    Path path = Path();
    path.moveTo(a.x, a.y);
    bool shouldDraw = true;
    math.Point currentPoint = math.Point(a.x, a.y);

    num radians = math.atan(size.height / size.width);

    num dx = math.cos(radians) * gap < 0
        ? math.cos(radians) * gap * -1
        : math.cos(radians) * gap;

    num dy = math.sin(radians) * gap < 0
        ? math.sin(radians) * gap * -1
        : math.sin(radians) * gap;

    while (currentPoint.x <= b.x && currentPoint.y <= b.y) {
      shouldDraw
          ? path.lineTo(currentPoint.x, currentPoint.y)
          : path.moveTo(currentPoint.x, currentPoint.y);
      shouldDraw = !shouldDraw;
      currentPoint = math.Point(
        currentPoint.x + dx,
        currentPoint.y + dy,
      );
    }
    return path;
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) {
    return true;
  }
}

我并不期望这能适合所有的用例,而且还有很大的定制和改进空间。如果您发现任何bug,请进行评论。

票数 55
EN

Stack Overflow用户

发布于 2020-11-20 12:38:31

这是一个fdottedline flutter包,可以轻松地在小部件周围添加虚线边框。这使用最简单的方法来创建虚线视图,并为开发人员提供创建虚线的能力。它还支持为Widget创建虚线边框。支持控制虚线边框的粗细、间距和边角。

  1. 要使用此软件包,请将fdottedline作为依赖项添加到pubspec.yaml文件中。

  1. 用法:虚线形状演示

FDottedLine(颜色: Colors.lightBlue600,高度: 100.0,宽度: 50,strokeWidth: 2.0,dottedLength: 10.0,间距: 2.0,)

票数 4
EN

Stack Overflow用户

发布于 2021-10-22 06:16:53

你可以使用dotted_border Flutter软件包

代码语言:javascript
复制
return DottedBorder(
   borderType: BorderType.RRect,
   radius: Radius.circular(20),
   dashPattern: [10, 10],
   color: Colors.grey,
   strokeWidth: 2,
   child: Card(
       color: Colors.amber,
       shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(10),
       ),
       child: Center(child: Text("hi")),
   )

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55373829

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档