aab29 / bezier.dart

A 2D Bézier curve math library written in Dart
BSD 2-Clause "Simplified" License
54 stars 9 forks source link

Usage as Widget in Flutter #16

Open mariusbloemhof opened 4 years ago

mariusbloemhof commented 4 years ago

Hi,

How do I use this in a Widget in Flutter?

idkq commented 3 years ago
import 'package:flutter/material.dart';
import "package:vector_math/vector_math.dart";
import "package:bezier/bezier.dart";

main() => runApp(MaterialApp(home: MyHomePage()));

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(''),
      ),
      body: CustomPaint(painter: MyPainter(),child: Container(height: 200, width: 200,),)
    );
  }
}

class MyPainter extends CustomPainter {
  final curve = CubicBezier([
    Vector2(10.0, 10.0),
    Vector2(70.0, 95.0),
    Vector2(25.0, 20.0),
    Vector2(15.0, 80.0)
  ]);

  @override
  void paint(Canvas canvas, Size size) {
    final points = curve.points;

    final path = Path()..moveTo(points[0].x, points[0].y)..cubicTo(curve.points[1].x, curve.points[1].y, curve.points[2].x,
        curve.points[2].y, curve.points[3].x, curve.points[3].y);
    canvas.drawPath(path, Paint());
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    return false;
  }
}
alxkzmn commented 3 years ago

@mariusbloemhof check out this package if you need it for charts https://pub.dev/packages/smoothie