Stub drawing
This commit is contained in:
parent
381ec9c717
commit
d748b4a5fa
|
|
@ -7,7 +7,6 @@ void main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
class MyApp extends StatelessWidget {
|
class MyApp extends StatelessWidget {
|
||||||
// This widget is the root of your application.
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return MaterialApp(
|
return MaterialApp(
|
||||||
|
|
@ -17,8 +16,6 @@ class MyApp extends StatelessWidget {
|
||||||
),
|
),
|
||||||
home: Scaffold(
|
home: Scaffold(
|
||||||
appBar: AppBar(
|
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"),
|
title: Text("Stiletto"),
|
||||||
),
|
),
|
||||||
body: Center(
|
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 {
|
class StilettoCanvas extends StatefulWidget {
|
||||||
StilettoCanvas({Key? key}) : super(key: key);
|
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
|
@override
|
||||||
_StilettoCanvasState createState() => _StilettoCanvasState();
|
_StilettoCanvasState createState() => _StilettoCanvasState();
|
||||||
}
|
}
|
||||||
|
|
||||||
class _StilettoCanvasState extends State<StilettoCanvas> {
|
class _StilettoCanvasState extends State<StilettoCanvas> {
|
||||||
Offset _lastMousePos = Offset.zero;
|
List<Offset> _currentPath = [];
|
||||||
|
bool _isDrawing = false;
|
||||||
RenderBox? _currentRenderBox = null;
|
RenderBox? _currentRenderBox = null;
|
||||||
|
|
||||||
GlobalKey _globalKey = GlobalKey();
|
final GlobalKey _globalKey = GlobalKey();
|
||||||
|
|
||||||
_afterLayout(Duration) {
|
_afterLayout(Duration) {
|
||||||
RenderObject? renderObject = _globalKey.currentContext?.findRenderObject();
|
RenderObject? renderObject = _globalKey.currentContext?.findRenderObject();
|
||||||
|
|
@ -65,29 +50,38 @@ class _StilettoCanvasState extends State<StilettoCanvas> {
|
||||||
}
|
}
|
||||||
|
|
||||||
void _onPointerDown(PointerDownEvent event) {
|
void _onPointerDown(PointerDownEvent event) {
|
||||||
print(event.position);
|
setState(() {
|
||||||
|
_currentPath = [];
|
||||||
|
_isDrawing = true;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
void _onPointerUp(PointerUpEvent event) {
|
||||||
|
setState(() {
|
||||||
|
_currentPath = [];
|
||||||
|
_isDrawing = false;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
void _onPointerMove(PointerMoveEvent event) {
|
||||||
setState(() {
|
setState(() {
|
||||||
Offset? localMousePos = _currentRenderBox?.globalToLocal(event.position);
|
Offset? localMousePos = _currentRenderBox?.globalToLocal(event.position);
|
||||||
if (localMousePos is Offset) {
|
if (localMousePos is Offset) {
|
||||||
_lastMousePos = localMousePos;
|
_currentPath.add(localMousePos);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
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);
|
WidgetsBinding.instance?.addPostFrameCallback(_afterLayout);
|
||||||
return Listener(
|
return Listener(
|
||||||
onPointerDown: _onPointerDown,
|
onPointerDown: _onPointerDown,
|
||||||
|
onPointerUp: _onPointerUp,
|
||||||
|
onPointerMove: _onPointerMove,
|
||||||
child: ClipRect(
|
child: ClipRect(
|
||||||
child: CustomPaint(
|
child: CustomPaint(
|
||||||
painter: StilettoPainter(_lastMousePos),
|
painter: StilettoPainter(_currentPath, _isDrawing),
|
||||||
),
|
),
|
||||||
key: _globalKey
|
key: _globalKey
|
||||||
)
|
)
|
||||||
|
|
@ -96,34 +90,26 @@ class _StilettoCanvasState extends State<StilettoCanvas> {
|
||||||
}
|
}
|
||||||
|
|
||||||
class StilettoPainter extends CustomPainter {
|
class StilettoPainter extends CustomPainter {
|
||||||
StilettoPainter(this.sunPos) : super();
|
StilettoPainter(this.currentPath, this.isDrawing) : super();
|
||||||
|
|
||||||
final Offset sunPos;
|
final List<Offset> currentPath;
|
||||||
|
final bool isDrawing;
|
||||||
Alignment _offsetToAlignment(Offset offset, Rect rect) {
|
|
||||||
return Alignment(
|
|
||||||
2 * offset.dx / rect.width - 1,
|
|
||||||
2 * offset.dy / rect.height - 1
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void paint(Canvas canvas, Size size) {
|
void paint(Canvas canvas, Size size) {
|
||||||
final Rect rect = Offset.zero & 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(
|
canvas.drawRect(
|
||||||
rect,
|
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
|
@override
|
||||||
bool shouldRepaint(StilettoPainter oldDelegate) {
|
bool shouldRepaint(StilettoPainter oldDelegate) {
|
||||||
return sunPos != oldDelegate.sunPos;
|
return currentPath != oldDelegate.currentPath || isDrawing;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue