daily pastebin goal
10%
SHARE
TWEET

Untitled

a guest Mar 25th, 2019 55 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import 'dart:ui';
  2.  
  3. import 'package:flutter/widgets.dart';
  4.  
  5. SizedBox box;
  6.  
  7. class Lottie extends AnimatedWidget {
  8.   const Lottie({
  9.     Key key,
  10.     @required this.lottieAnimation,
  11.     @required AnimationController controller,
  12.     @required this.width,
  13.     @required this.height,
  14.     this.semanticsLabel,
  15.   })  : assert(controller != null),
  16.         assert(lottieAnimation != null),
  17.         assert(width != null && width > 0.0),
  18.         assert(height != null && height > 0.0),
  19.         super(key: key, listenable: controller);
  20.  
  21.   Animation<double> get _progress => listenable;
  22.  
  23.   final LottieAnimation lottieAnimation;
  24.   final double width;
  25.   final double height;
  26.   final String semanticsLabel;
  27.  
  28.   @override
  29.   Widget build(BuildContext context) {
  30.     Widget widget = _Lottie(
  31.       lottieAnimation: lottieAnimation,
  32.       progress: _progress.value,
  33.       width: width,
  34.       height: height,
  35.     );
  36.     if (semanticsLabel != null) {
  37.       widget = Semantics(
  38.         label: semanticsLabel,
  39.         textDirection: Directionality.of(context),
  40.         child: widget,
  41.       );
  42.     }
  43.     return widget;
  44.   }
  45. }
  46.  
  47. class _Lottie extends LeafRenderObjectWidget {
  48.   const _Lottie(
  49.       {@required this.lottieAnimation,
  50.       @required this.progress,
  51.       @required this.width,
  52.       @required this.height,
  53.       Key key})
  54.       : assert(lottieAnimation != null),
  55.         assert(progress != null),
  56.         assert(progress >= 0.0 && progress <= 1.0),
  57.         assert(width != null && width > 0.0),
  58.         assert(height != null && height > 0.0),
  59.         super(key: key);
  60.  
  61.   final LottieAnimation lottieAnimation;
  62.   final double progress;
  63.   final double width;
  64.   final double height;
  65.  
  66.   @override
  67.   RenderObject createRenderObject(BuildContext context) =>
  68.       _LottieRenderObject(lottieAnimation, progress, width, height);
  69.  
  70.   @override
  71.   void updateRenderObject(
  72.     BuildContext context,
  73.     _LottieRenderObject renderObject,
  74.   ) {
  75.     renderObject
  76.       ..lottieAnimation = lottieAnimation
  77.       ..progress = progress
  78.       ..width = width
  79.       ..height = height;
  80.   }
  81. }
  82.  
  83. class _LottieRenderObject extends RenderBox {
  84.   _LottieRenderObject(
  85.     this._lottieAnimation,
  86.     this._progress,
  87.     this._width,
  88.     this._height,
  89.   );
  90.  
  91.   LottieAnimation _lottieAnimation;
  92.   LottieAnimation get lottieAnimation => _lottieAnimation;
  93.   set lottieAnimation(LottieAnimation value) {
  94.     if (_lottieAnimation != value) {
  95.       _lottieAnimation = value;
  96.       markNeedsPaint();
  97.     }
  98.   }
  99.  
  100.   double _progress;
  101.   double get progress => _progress;
  102.   set progress(double value) {
  103.     if (_progress != value) {
  104.       _progress = value;
  105.       markNeedsPaint();
  106.     }
  107.   }
  108.  
  109.   double _width;
  110.   set width(double value) {
  111.     if (_width != value) {
  112.       _width = value;
  113.       markNeedsPaint();
  114.     }
  115.   }
  116.  
  117.   double _height;
  118.   set height(double value) {
  119.     if (_height != value) {
  120.       _height = value;
  121.       markNeedsPaint();
  122.     }
  123.   }
  124.  
  125.   @override
  126.   bool get sizedByParent => true;
  127.  
  128.   @override
  129.   void paint(PaintingContext context, Offset offset) {
  130.     lottieAnimation.drawFrame(
  131.       context.canvas,
  132.       _progress,
  133.       _height,
  134.       _width,
  135.     );
  136.     // context.canvas.restore();
  137.   }
  138. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top