From d748b4a5fa5f8cb22c30b0ffaf0c6cafc2802072 Mon Sep 17 00:00:00 2001 From: Enrico Lumetti Date: Mon, 9 Aug 2021 16:54:13 +0200 Subject: [PATCH] Stub drawing --- lib/main.dart | 74 +++++++++++++++++++++------------------------------ 1 file changed, 30 insertions(+), 44 deletions(-) diff --git a/lib/main.dart b/lib/main.dart index ba6d43e..411f7ce 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -7,7 +7,6 @@ void main() { } class MyApp extends StatelessWidget { - // This widget is the root of your application. @override Widget build(BuildContext context) { return MaterialApp( @@ -17,8 +16,6 @@ class MyApp extends StatelessWidget { ), home: Scaffold( appBar: AppBar( - // Here we take the value from the StilettoCanvas object that was created by - // the App.build method, and use it to set our appbar title. title: Text("Stiletto"), ), body: Center( @@ -27,11 +24,6 @@ class MyApp extends StatelessWidget { ) ) ) - // home: Center( - // child: SizedBox.expand( - // child: StilettoCanvas() - // ) - // ) ); } } @@ -39,23 +31,16 @@ class MyApp extends StatelessWidget { class StilettoCanvas extends StatefulWidget { StilettoCanvas({Key? key}) : super(key: key); - // This widget is the home page of your application. It is stateful, meaning - // that it has a State object (defined below) that contains fields that affect - // how it looks. - - // This class is the configuration for the state. It holds the values (in this - // case the title) provided by the parent (in this case the App widget) and - // used by the build method of the State. Fields in a Widget subclass are - // always marked "final". @override _StilettoCanvasState createState() => _StilettoCanvasState(); } class _StilettoCanvasState extends State { - Offset _lastMousePos = Offset.zero; + List _currentPath = []; + bool _isDrawing = false; RenderBox? _currentRenderBox = null; - GlobalKey _globalKey = GlobalKey(); + final GlobalKey _globalKey = GlobalKey(); _afterLayout(Duration) { RenderObject? renderObject = _globalKey.currentContext?.findRenderObject(); @@ -65,29 +50,38 @@ class _StilettoCanvasState extends State { } void _onPointerDown(PointerDownEvent event) { - print(event.position); + setState(() { + _currentPath = []; + _isDrawing = true; + }); + } + + void _onPointerUp(PointerUpEvent event) { + setState(() { + _currentPath = []; + _isDrawing = false; + }); + } + + void _onPointerMove(PointerMoveEvent event) { setState(() { Offset? localMousePos = _currentRenderBox?.globalToLocal(event.position); if (localMousePos is Offset) { - _lastMousePos = localMousePos; + _currentPath.add(localMousePos); } }); } @override Widget build(BuildContext context) { - // This method is rerun every time setState is called, for instance as done - // by the _incrementCounter method above. - // - // The Flutter framework has been optimized to make rerunning build methods - // fast, so that you can just rebuild anything that needs updating rather - // than having to individually change instances of widgets. WidgetsBinding.instance?.addPostFrameCallback(_afterLayout); return Listener( onPointerDown: _onPointerDown, + onPointerUp: _onPointerUp, + onPointerMove: _onPointerMove, child: ClipRect( child: CustomPaint( - painter: StilettoPainter(_lastMousePos), + painter: StilettoPainter(_currentPath, _isDrawing), ), key: _globalKey ) @@ -96,34 +90,26 @@ class _StilettoCanvasState extends State { } class StilettoPainter extends CustomPainter { - StilettoPainter(this.sunPos) : super(); + StilettoPainter(this.currentPath, this.isDrawing) : super(); - final Offset sunPos; - - Alignment _offsetToAlignment(Offset offset, Rect rect) { - return Alignment( - 2 * offset.dx / rect.width - 1, - 2 * offset.dy / rect.height - 1 - ); - } + final List currentPath; + final bool isDrawing; @override void paint(Canvas canvas, Size size) { final Rect rect = Offset.zero & size; - final RadialGradient gradient = RadialGradient( - center: _offsetToAlignment(sunPos, rect), - radius: 0.2, - colors: [Color(0xFFFFFF00), Color(0xFF0099FF)], - stops: [0.4, 1.0], - ); canvas.drawRect( rect, - Paint()..shader = gradient.createShader(rect), + Paint()..color = Color(0xFFFFFFFF) ); + final strokePaint = Paint() + ..color = Colors.black + ..strokeWidth = 1.0; + canvas.drawPoints(PointMode.polygon, currentPath, strokePaint); } @override bool shouldRepaint(StilettoPainter oldDelegate) { - return sunPos != oldDelegate.sunPos; + return currentPath != oldDelegate.currentPath || isDrawing; } }