dart Flutter中具有“菱形”形状的容器

tzdcorbm  于 2023-04-27  发布在  Flutter
关注(0)|答案(2)|浏览(213)

the shape i need
你好,我想使容器在Flutter与这种形状,我希望能够确定的高度的容器,但有它的右侧和左侧总是与'钻石'形状
我试着玩周围的方块装饰选项,但我似乎无法实现我在寻找什么

eeq64g8w

eeq64g8w1#

您需要使用CustomPainter来创建该ui

class HexagonPainter extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    final paint = Paint()
      ..color = Colors.black
      ..strokeWidth = 2
      ..style = PaintingStyle.stroke;

    final path = Path()
      ..moveTo(size.width * 0.25, 0)
      ..lineTo(size.width * 0.75, 0)
      ..lineTo(size.width, size.height * 0.5)
      ..lineTo(size.width * 0.75, size.height)
      ..lineTo(size.width * 0.25, size.height)
      ..lineTo(0, size.height * 0.5)
      ..close();

    canvas.drawPath(path, paint);
  }

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

class HexagonShape extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return SizedBox(
      width: 100,
      height: 100,
      child: CustomPaint(
        painter: HexagonPainter(),
      ),
    );
  }
}
c3frrgcw

c3frrgcw2#

class HexagonContainer extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ClipPath(
      clipper: HexagonClipper(),
      child: Container(
        height: 200,
        color: Colors.blue,
      ),
    );
  }
}

class HexagonClipper extends CustomClipper<Path> {
  @override
  Path getClip(Size size) {
    Path path = Path();
    double h = size.height;
    double w = size.width;
    double r = h / 2;

    path.moveTo(r, 0);
    path.lineTo(w - r, 0);
    path.lineTo(w, r);
    path.lineTo(w - r, h);
    path.lineTo(r, h);
    path.lineTo(0, r);

    path.close();
    return path;
  }

  @override
  bool shouldReclip(CustomClipper<Path> oldClipper) => false;
}

相关问题