flutter - flutter 自定义矩形

标签 flutter dart

如何在Flutter中使用CustomPainter类绘制类似下面的内容?

enter image description here

忽略中间的“批次”标签。我只想实现在边框线之间有空格的边框/盒子形状。

最佳答案

试试这个

您可以在Github上检查

或者在DartPad上尝试

import 'package:flutter/material.dart';

final Color darkBlue = Color.fromARGB(255, 18, 32, 47);

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: darkBlue),
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: Center(
          child: ScanPainter(
            size: 200,
            color: Colors.grey,
          ),
        ),
      ),
    );
  }
}

class ScanPainter extends StatelessWidget {
  final double size;
  final Color color;

  ScanPainter({
    @required this.size,
    this.color = Colors.grey,
  }) : assert(size != null);

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      height: size,
      width: size,
      child: Stack(
        children: [
          Positioned(
            top: 0.0,
            left: 0.0,
            child: _buildRotatedPart(
              0,
              size * .2,
            ),
          ),
          Positioned(
            top: 0.0,
            right: 0.0,
            child: _buildRotatedPart(
              1.5708,
              size * .2,
            ),
          ),
          Positioned(
            bottom: 0.0,
            right: 0.0,
            child: _buildRotatedPart(
              3.14159,
              size * .2,
            ),
          ),
          Positioned(
            bottom: 0.0,
            left: 0.0,
            child: _buildRotatedPart(
              4.71239,
              size * .2,
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildRotatedPart(double radians, double size) {
    return Transform.rotate(
      angle: radians,
      child: CustomPaint(
        painter: ScanCustomPainter(color: this.color),
        size: Size(size, size),
      ),
    );
  }
}

class ScanCustomPainter extends CustomPainter {
  final Color color;

  ScanCustomPainter({
    @required this.color,
  }) : assert(color != null);

  @override
  void paint(Canvas canvas, Size size) {
    var paint = Paint()..color = this.color;
    var path = Path();
    double baseHeight = size.height;
    double baseWidth = size.width;

    path.moveTo(0, baseHeight);
    path.lineTo(0, baseHeight * .5);
    path.quadraticBezierTo(0, 0, baseWidth * .5, 0);
    path.lineTo(baseWidth, 0);
    path.lineTo(baseWidth, baseHeight * .3);
    path.lineTo(baseWidth * .6, baseHeight * .3);
    path.quadraticBezierTo(
        baseWidth * .3, baseHeight * .3, baseWidth * .3, baseHeight * .6);
    path.lineTo(baseWidth * .3, baseHeight);

    path.close();

    canvas.drawPath(path, paint);
  }

  @override
  bool shouldRepaint(ScanCustomPainter oldDelegate) => false;
}

关于flutter - flutter 自定义矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62154297/

相关文章:

flutter - 对两个文本小部件仅使用一次“对齐”

android - Flutter:TextFormField 图标下方的下划线

kotlin - Dart 相当于 Kotlin 的 let 是什么?

dart - PopupMenuEntry 的水平对齐方式

dart - 如何正确实现以对象为键的Map?

user-interface - 如何在同心圆Flutter中对齐我的Neumorphic小部件?

flutter - Flutter 上的自定义字体

Flutter:如何将带参数的函数传递给子类

firebase - flutter firebase 使用 future 从数据库获取字符串

android - 如何定义与另一个列表长度相同的默认列表