flutter 如何用曲线点绘制倒三角形

omhiaaxx  于 2023-06-24  发布在  Flutter
关注(0)|答案(1)|浏览(182)

我试过这个密码

Path getClip(Size size) {

    final path = Path();
    path.lineTo(size.width, 0.0);
    path.lineTo(size.width / 2, size.height);
    path.close();
    return path;
  }

但我没有得到完美的输出

Path getClip(Size size) {

    final path = Path();
    path.lineTo(size.width, 0.0);
    path.lineTo(size.width / 2, size.height);
    path.close();
    return path;
  }

但我想要这个形状

w6mmgewl

w6mmgewl1#

试试这个代码!!!

import 'package:flutter/material.dart';

class InvertedTriangle extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Inverted Triangle'),
      ),
      body: Center(
        child: CustomPaint(
          painter: InvertedTrianglePainter(),
          size: Size(500, 500), // Adjust the size as per your requirement
        ),
      ),
    );
  }
}

class InvertedTrianglePainter extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    final double width = size.width;
    final double height = size.height;

    final double bluntHeight = height * 0.2; // Adjust the blunt height as per your requirement
    final double bluntOffset = width * 0.01; // Adjust the blunt offset as per your requirement

    final path = Path();
    path.moveTo(0, 0); // Top left corner

    path.lineTo(width, 0); // Top right corner
    path.lineTo(width / 2 + bluntOffset, bluntHeight); // Top blunt corner
    path.lineTo(width / 2 - bluntOffset, bluntHeight); // Top blunt corner

    path.lineTo(0, 0); // Top left corner
    path.close();

    final paint = Paint()..color = Colors.blue;

    canvas.drawPath(path, paint);
  }

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

void main() {
  runApp(MaterialApp(
    home: InvertedTriangle(),
  ));
}

相关问题