Stub drawing

This commit is contained in:
Enrico Lumetti 2021-08-09 16:54:13 +02:00
parent 381ec9c717
commit d748b4a5fa
1 changed files with 30 additions and 44 deletions

View File

@ -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<StilettoCanvas> {
Offset _lastMousePos = Offset.zero;
List<Offset> _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<StilettoCanvas> {
}
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<StilettoCanvas> {
}
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<Offset> 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>[Color(0xFFFFFF00), Color(0xFF0099FF)],
stops: <double>[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;
}
}