Skip to content

Instantly share code, notes, and snippets.

@funwithflutter
Last active August 10, 2021 22:05
Show Gist options
  • Save funwithflutter/c1838c8ac832a9e4935ccd7f516b9fe0 to your computer and use it in GitHub Desktop.
Save funwithflutter/c1838c8ac832a9e4935ccd7f516b9fe0 to your computer and use it in GitHub Desktop.

Revisions

  1. funwithflutter revised this gist Aug 10, 2021. 1 changed file with 15 additions and 13 deletions.
    28 changes: 15 additions & 13 deletions paint_demo.dart
    Original file line number Diff line number Diff line change
    @@ -2,19 +2,21 @@ import 'dart:math';

    import 'package:flutter/material.dart';

    void main() => runApp(MyApp());
    void main() => runApp(const MyApp());

    class MyApp extends StatelessWidget {
    const MyApp({Key? key}) : super(key: key);

    @override
    Widget build(BuildContext context) {
    return MaterialApp(
    showPerformanceOverlay: false,
    title: 'Paint Demo',
    home: Scaffold(
    appBar: AppBar(
    title: Text('Paint Demo'),
    title: const Text('Paint Demo'),
    ),
    body: Center(
    body: const Center(
    child: PaintDemo(),
    ),
    ),
    @@ -23,21 +25,21 @@ class MyApp extends StatelessWidget {
    }

    class PaintDemo extends StatefulWidget {
    PaintDemo({Key key}) : super(key: key);
    const PaintDemo({Key? key}) : super(key: key);

    @override
    _PaintDemoState createState() => _PaintDemoState();
    }

    class _PaintDemoState extends State<PaintDemo>
    with SingleTickerProviderStateMixin {
    AnimationController _controller;
    CurvedAnimation _animation;
    late AnimationController _controller;
    late CurvedAnimation _animation;

    @override
    void initState() {
    _controller =
    AnimationController(vsync: this, duration: Duration(seconds: 3));
    AnimationController(vsync: this, duration: const Duration(seconds: 3));
    _animation = CurvedAnimation(parent: _controller, curve: Curves.slowMiddle);

    _controller.repeat();
    @@ -60,7 +62,7 @@ class _PaintDemoState extends State<PaintDemo>
    child: child,
    );
    },
    child: Container(
    child: const SizedBox(
    width: 200,
    height: 75,
    child: Text('Loading...'),
    @@ -75,7 +77,7 @@ class LinePainter extends CustomPainter {
    final double progress;

    static final linePaint = Paint()
    ..color = Color(0xFFF4AC45)
    ..color = const Color(0xFFF4AC45)
    ..style = PaintingStyle.stroke
    ..strokeWidth = 6
    ..strokeCap = StrokeCap.round;
    @@ -90,9 +92,9 @@ class LinePainter extends CustomPainter {
    final point1 = max(0.0, widthProgress - _gap);
    final point2 = min(widthProgress + _gap, size.width);

    canvas.drawLine(Offset(0, height), Offset(point1, height), linePaint);
    canvas.drawLine(
    Offset(point2, height), Offset(size.width, height), linePaint);
    canvas
    ..drawLine(Offset(0, height), Offset(point1, height), linePaint)
    ..drawLine(Offset(point2, height), Offset(size.width, height), linePaint);
    }

    @override
    @@ -102,4 +104,4 @@ class LinePainter extends CustomPainter {
    }
    return false;
    }
    }
    }
  2. funwithflutter revised this gist Feb 28, 2020. 1 changed file with 7 additions and 1 deletion.
    8 changes: 7 additions & 1 deletion paint_demo.dart
    Original file line number Diff line number Diff line change
    @@ -44,6 +44,12 @@ class _PaintDemoState extends State<PaintDemo>
    super.initState();
    }

    @override
    void dispose() {
    _controller.dispose();
    super.dispose();
    }

    @override
    Widget build(BuildContext context) {
    return AnimatedBuilder(
    @@ -96,4 +102,4 @@ class LinePainter extends CustomPainter {
    }
    return false;
    }
    }
    }
  3. funwithflutter created this gist Feb 28, 2020.
    99 changes: 99 additions & 0 deletions paint_demo.dart
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,99 @@
    import 'dart:math';

    import 'package:flutter/material.dart';

    void main() => runApp(MyApp());

    class MyApp extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
    return MaterialApp(
    showPerformanceOverlay: false,
    title: 'Paint Demo',
    home: Scaffold(
    appBar: AppBar(
    title: Text('Paint Demo'),
    ),
    body: Center(
    child: PaintDemo(),
    ),
    ),
    );
    }
    }

    class PaintDemo extends StatefulWidget {
    PaintDemo({Key key}) : super(key: key);

    @override
    _PaintDemoState createState() => _PaintDemoState();
    }

    class _PaintDemoState extends State<PaintDemo>
    with SingleTickerProviderStateMixin {
    AnimationController _controller;
    CurvedAnimation _animation;

    @override
    void initState() {
    _controller =
    AnimationController(vsync: this, duration: Duration(seconds: 3));
    _animation = CurvedAnimation(parent: _controller, curve: Curves.slowMiddle);

    _controller.repeat();
    super.initState();
    }

    @override
    Widget build(BuildContext context) {
    return AnimatedBuilder(
    animation: _animation,
    builder: (_, child) {
    return CustomPaint(
    painter: LinePainter(progress: _animation.value),
    child: child,
    );
    },
    child: Container(
    width: 200,
    height: 75,
    child: Text('Loading...'),
    ),
    );
    }
    }

    class LinePainter extends CustomPainter {
    LinePainter({this.progress = 0.5});

    final double progress;

    static final linePaint = Paint()
    ..color = Color(0xFFF4AC45)
    ..style = PaintingStyle.stroke
    ..strokeWidth = 6
    ..strokeCap = StrokeCap.round;

    static const _gap = 12.0;

    @override
    void paint(Canvas canvas, Size size) {
    final height = size.height / 2;

    final widthProgress = size.width * progress;
    final point1 = max(0.0, widthProgress - _gap);
    final point2 = min(widthProgress + _gap, size.width);

    canvas.drawLine(Offset(0, height), Offset(point1, height), linePaint);
    canvas.drawLine(
    Offset(point2, height), Offset(size.width, height), linePaint);
    }

    @override
    bool shouldRepaint(LinePainter oldDelegate) {
    if (progress != oldDelegate.progress) {
    return true;
    }
    return false;
    }
    }