简体   繁体   English

如何在矩形中创建三角形 flutter

[英]How to create a triangle in a rectangle flutter

I want to create a triangle at the edge of a rectangle and to be able to write in them using Flutter platform enter image description here我想在矩形的边缘创建一个三角形,并能够使用 Flutter 平台在此处输入图像描述

You can use customPainter to do that this is the way:您可以使用 customPainter 来做到这一点:

import 'package:flutter/material.dart';

class CustomFigure extends StatelessWidget {
  const CustomFigure({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        height: double.infinity,
        width: double.infinity,
        child: CustomPaint(
          painter: _Figure(),
        ),
      ),
    );
  }
}

class _Figure extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    final paint = new Paint();
    paint.color = Colors.blue;
    paint.style = PaintingStyle.stroke;
    paint.strokeWidth = 5;

    final path = new Path();

    // Drawing triangle
    path.moveTo(10, size.height * 0.3);
    path.lineTo(size.width * 0.5, 50);
    path.lineTo(10, 50);
    path.lineTo(10, size.height * 0.3);

    // Drawing figure
    path.moveTo(10, size.width * 0.70);
    path.lineTo(size.width * 0.55, 50);
    path.lineTo(size.width - 10, 50);
    path.lineTo(size.width - 10, size.height * 0.5);
    path.lineTo(10, size.height * 0.5);
    path.lineTo(10, size.width * 0.70);

    canvas.drawPath(path, paint);
  }

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


在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM