Skip to content

Commit

Permalink
feat: Make stroke order diagram size configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr-Pepe committed Jun 3, 2024
1 parent 4050928 commit 29a6541
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 23 deletions.
21 changes: 10 additions & 11 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -160,17 +160,16 @@ class _HomePageState extends State<HomePage> with TickerProviderStateMixin {
}

Widget _buildStrokeOrderAnimation(StrokeOrderAnimationController controller) {
return SizedBox.square(
dimension: 350,
child: ChangeNotifierProvider<StrokeOrderAnimationController>.value(
value: controller,
child: Consumer<StrokeOrderAnimationController>(
builder: (context, controller, child) {
return FittedBox(
child: StrokeOrderAnimator(controller, key: UniqueKey()),
);
},
),
return ChangeNotifierProvider<StrokeOrderAnimationController>.value(
value: controller,
child: Consumer<StrokeOrderAnimationController>(
builder: (context, controller, child) {
return StrokeOrderAnimator(
controller,
size: Size(300, 300),
key: UniqueKey(),
);
},
),
);
}
Expand Down
75 changes: 63 additions & 12 deletions lib/src/stroke_order_animator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,19 @@ import 'package:stroke_order_animator/stroke_order_animator.dart';
/// the animation when the selected page changes in order to avoid broken
/// animation behavior.
class StrokeOrderAnimator extends StatefulWidget {
const StrokeOrderAnimator(this._controller, {super.key});
const StrokeOrderAnimator(
this._controller, {
this.size = const Size(1024, 1024),
super.key,
});
final StrokeOrderAnimationController _controller;

final Size size;
@override
StrokeOrderAnimatorState createState() => StrokeOrderAnimatorState();
}

class StrokeOrderAnimatorState extends State<StrokeOrderAnimator> {
List<Offset?> _points = <Offset>[];
final List<Offset?> _points = <Offset>[];

@override
void initState() {
Expand All @@ -40,16 +44,27 @@ class StrokeOrderAnimatorState extends State<StrokeOrderAnimator> {
point.dx <= box.size.width &&
point.dy >= 0 &&
point.dy <= box.size.height) {
_points = List.from(_points)..add(point);
_points.add(point);

Check warning on line 47 in lib/src/stroke_order_animator.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/stroke_order_animator.dart#L47

Added line #L47 was not covered by tests
} else {
if (_points.last != null) {
_points = List.from(_points)..add(null);
_points.add(null);

Check warning on line 50 in lib/src/stroke_order_animator.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/stroke_order_animator.dart#L50

Added line #L50 was not covered by tests
}
}
});
},
onPanEnd: (DragEndDetails details) {
widget._controller.checkStroke(_points);
widget._controller.checkStroke(
_points
.map(
(point) => point != null
? Offset(
point.dx * 1024 / widget.size.width,
point.dy * 1024 / widget.size.height,

Check warning on line 62 in lib/src/stroke_order_animator.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/stroke_order_animator.dart#L56-L62

Added lines #L56 - L62 were not covered by tests
)
: null,
)
.toList(),

Check warning on line 66 in lib/src/stroke_order_animator.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/stroke_order_animator.dart#L66

Added line #L66 was not covered by tests
);
setState(() {
_points.clear();
});
Expand Down Expand Up @@ -79,8 +94,8 @@ class StrokeOrderAnimatorState extends State<StrokeOrderAnimator> {
: widget._controller.strokeAnimationController;

return SizedBox(
width: 1024,
height: 1024,
width: widget.size.width,
height: widget.size.height,
child: CustomPaint(
painter: StrokePainter(
widget._controller.strokeOrder.strokeOutlines[index],
Expand Down Expand Up @@ -217,18 +232,27 @@ class StrokePainter extends CustomPainter {
Offset.zero,
);

canvas.drawPath(finalOutlinePath, strokePaint);
canvas.drawPath(
_scalePath(finalOutlinePath, size),
strokePaint,
);
}
} else if (showStroke) {
canvas.drawPath(strokeOutlinePath, strokePaint);
canvas.drawPath(
_scalePath(strokeOutlinePath, size),
strokePaint,
);
}

if (showOutline) {
final borderPaint = Paint()
..color = outlineColor
..strokeWidth = outlineWidth
..style = PaintingStyle.stroke;
canvas.drawPath(strokeOutlinePath, borderPaint);
canvas.drawPath(
_scalePath(strokeOutlinePath, size),

Check warning on line 253 in lib/src/stroke_order_animator.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/stroke_order_animator.dart#L252-L253

Added lines #L252 - L253 were not covered by tests
borderPaint,
);
}

if (showMedian) {
Expand All @@ -238,7 +262,7 @@ class StrokePainter extends CustomPainter {
medianPath.lineTo(point.dx, point.dy);
}
canvas.drawPath(
medianPath,
_scalePath(medianPath, size),

Check warning on line 265 in lib/src/stroke_order_animator.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/stroke_order_animator.dart#L265

Added line #L265 was not covered by tests
Paint()
..style = PaintingStyle.stroke
..strokeWidth = medianWidth
Expand All @@ -249,6 +273,33 @@ class StrokePainter extends CustomPainter {

@override
bool shouldRepaint(CustomPainter oldDelegate) => true;

Path _scalePath(Path path, Size size) {
if (size == const Size(1024, 1024)) {
return path;
}

return path.transform(
Matrix4(
size.width / 1024,
0,
0,
0,
0,
size.height / 1024,
0,
0,
0,
0,
1,
0,
0,
0,
0,
1,
).storage,
);
}
}

List<Path> extractContourPaths(
Expand Down

0 comments on commit 29a6541

Please sign in to comment.