自定义形状的动态数据

Dynamic data on custom shapes

我想构建一个应用程序,我想在其中为我的一个设计填充动态数据的形状。这些将是自定义形状,我有两种不同的形状,它们一个在另一个下面交替。所以我有一个左边的形状,然后下一个是右边的形状,依此类推。是否可以在 Flutter 中创建它,我该怎么做?

这是一种方法。我使用 CustomPainter 创建的自定义三角形简化了形状,因此您必须根据需要修改它。

ListView(
      children: <Widget>[
        OverflowTitle(color: Colors.green),
        OverflowTitle(color: Colors.blue),
        OverflowTitle(color: Colors.red)
      ],
    );

和自定义溢出标题

class OverflowTitle extends StatelessWidget {
  final Color color;

  const OverflowTitle({this.color});
  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      height: 50,
      child: OverflowBox(
        alignment: Alignment.bottomCenter,
        minHeight: 50,
        maxHeight: 70,
        child: Container(
          height: 60,
          child: CustomPaint(
            painter: TrianglePainter(
              strokeColor: color,
            ),
            child: Text(
              'NO1',
              textAlign: TextAlign.center,
            ),
          ),
        ),
      ),
    );
  }
}

这是输出

如果您需要更多帮助,请告诉我...

更新 这是我的自定义三角形画家

import 'package:flutter/material.dart';

enum Direction { Up, Down, Left, Right }

class TrianglePainter extends CustomPainter {
  final Color strokeColor;
  final Direction direction;

  TrianglePainter({this.strokeColor = Colors.white, this.direction});

  @override
  void paint(Canvas canvas, Size size) {
    Paint paint = Paint()
      ..color = strokeColor
      ..style = PaintingStyle.fill;

    canvas.drawPath(getTrianglePath(size.width, size.height), paint);
  }

  Path getTrianglePath(double x, double y) {
    if (direction == Direction.Right) {
      return Path()
        ..moveTo(0, y)
        ..lineTo(x, y / 2)
        ..lineTo(0, 0)
        ..lineTo(0, y);
    } else if (direction == Direction.Left) {
      return Path()
        ..moveTo(x, 0)
        ..lineTo(0, y / 2)
        ..lineTo(x, y)
        ..lineTo(x, 0);
    } else if (direction == Direction.Down) {
      return Path()
        ..moveTo(0, 0)
        ..lineTo(x / 2, y)
        ..lineTo(x, 0)
        ..lineTo(0, 0);
    } else {
      return Path()
        ..moveTo(0, y)
        ..lineTo(x / 2, 0)
        ..lineTo(x, y)
        ..lineTo(0, y);
    }
  }

  @override
  bool shouldRepaint(TrianglePainter oldDelegate) {
    return oldDelegate.strokeColor != strokeColor;
  }
}