-
Notifications
You must be signed in to change notification settings - Fork 17
feat: Integrate dotlottie_flutter for enhanced Lottie animation support #2249
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
M-Talha4
wants to merge
2
commits into
main
Choose a base branch
from
chore/migrate-lottie-to-dotlottie-flutter
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
164 changes: 164 additions & 0 deletions
164
modules/ensemble/lib/widget/lottie/dot_lottie_bytes_view.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,164 @@ | ||
| import 'package:ensemble/widget/lottie/ensemble_dot_lottie_controller.dart'; | ||
| import 'package:flutter/foundation.dart'; | ||
| import 'package:flutter/gestures.dart'; | ||
| import 'package:flutter/material.dart'; | ||
| import 'package:flutter/services.dart'; | ||
|
|
||
| /// [DotLottieView] with `sourceType: 'asset'` converts `.lottie` bytes to | ||
| /// `sourceType: 'data'` before creating the platform view. Android's native | ||
| /// player does not accept `data:` URLs via `sourceType: 'url'` (iOS does). | ||
| /// | ||
| /// This widget passes bundle-loaded bytes the same way the package does for assets. | ||
| class DotLottieBytesView extends StatefulWidget { | ||
| final Uint8List bytes; | ||
| final bool? autoplay; | ||
| final bool? loop; | ||
| final BoxFit? fit; | ||
| final int? width; | ||
| final int? height; | ||
| final Function(EnsembleDotLottieController)? onViewCreated; | ||
| final VoidCallback? onComplete; | ||
| final VoidCallback? onLoad; | ||
| final VoidCallback? onLoadError; | ||
| final VoidCallback? onPlay; | ||
| final VoidCallback? onStop; | ||
|
|
||
| const DotLottieBytesView({ | ||
| super.key, | ||
| required this.bytes, | ||
| this.autoplay, | ||
| this.loop, | ||
| this.fit, | ||
| this.width, | ||
| this.height, | ||
| this.onViewCreated, | ||
| this.onComplete, | ||
| this.onLoad, | ||
| this.onLoadError, | ||
| this.onPlay, | ||
| this.onStop, | ||
| }); | ||
|
|
||
| @override | ||
| State<DotLottieBytesView> createState() => _DotLottieBytesViewState(); | ||
| } | ||
|
|
||
| class _DotLottieBytesViewState extends State<DotLottieBytesView> { | ||
| MethodChannel? _methodChannel; | ||
| int _platformViewGeneration = 0; | ||
|
|
||
| @override | ||
| void didUpdateWidget(DotLottieBytesView oldWidget) { | ||
| super.didUpdateWidget(oldWidget); | ||
| if (!identical(oldWidget.bytes, widget.bytes)) { | ||
| setState(() => _platformViewGeneration++); | ||
| } | ||
| } | ||
|
|
||
| @override | ||
| Widget build(BuildContext context) { | ||
| final params = _creationParams(); | ||
| if (defaultTargetPlatform == TargetPlatform.android) { | ||
| return AndroidView( | ||
| key: ValueKey(_platformViewGeneration), | ||
| viewType: 'dotlottie_view', | ||
| creationParams: params, | ||
| creationParamsCodec: const StandardMessageCodec(), | ||
| onPlatformViewCreated: _onPlatformViewCreated, | ||
| gestureRecognizers: const <Factory<OneSequenceGestureRecognizer>>{}, | ||
| ); | ||
| } | ||
| if (defaultTargetPlatform == TargetPlatform.iOS) { | ||
| return UiKitView( | ||
| key: ValueKey(_platformViewGeneration), | ||
| viewType: 'dotlottie_view', | ||
| creationParams: params, | ||
| creationParamsCodec: const StandardMessageCodec(), | ||
| onPlatformViewCreated: _onPlatformViewCreated, | ||
| gestureRecognizers: const <Factory<OneSequenceGestureRecognizer>>{}, | ||
| ); | ||
| } | ||
| if (defaultTargetPlatform == TargetPlatform.macOS) { | ||
| return AppKitView( | ||
| key: ValueKey(_platformViewGeneration), | ||
| viewType: 'dotlottie_view', | ||
| creationParams: params, | ||
| creationParamsCodec: const StandardMessageCodec(), | ||
| onPlatformViewCreated: _onPlatformViewCreated, | ||
| gestureRecognizers: const <Factory<OneSequenceGestureRecognizer>>{}, | ||
| ); | ||
| } | ||
| return const SizedBox.shrink(); | ||
| } | ||
|
|
||
| Map<String, dynamic> _creationParams() { | ||
| return { | ||
| 'autoplay': widget.autoplay, | ||
| 'loop': widget.loop, | ||
| 'speed': 1.0, | ||
| 'mode': 'forward', | ||
| 'useFrameInterpolation': false, | ||
| if (widget.fit != null) 'fit': _boxFitToString(widget.fit), | ||
| if (widget.width != null) 'width': widget.width, | ||
| if (widget.height != null) 'height': widget.height, | ||
| 'sourceType': 'data', | ||
| 'source': widget.bytes, | ||
| }; | ||
| } | ||
|
|
||
| static String? _boxFitToString(BoxFit? fit) { | ||
| switch (fit) { | ||
| case BoxFit.contain: | ||
| return 'contain'; | ||
| case BoxFit.cover: | ||
| return 'cover'; | ||
| case BoxFit.fill: | ||
| return 'fill'; | ||
| case BoxFit.fitWidth: | ||
| return 'fitWidth'; | ||
| case BoxFit.fitHeight: | ||
| return 'fitHeight'; | ||
| case BoxFit.none: | ||
| return 'none'; | ||
| case BoxFit.scaleDown: | ||
| return 'contain'; | ||
| case null: | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| void _onPlatformViewCreated(int viewId) { | ||
| _methodChannel?.setMethodCallHandler(null); | ||
|
|
||
| _methodChannel = MethodChannel('dotlottie_view_$viewId'); | ||
| _methodChannel!.setMethodCallHandler(_handleMethodCall); | ||
| widget.onViewCreated?.call(EnsembleDotLottieController.fromViewId(viewId)); | ||
| } | ||
|
|
||
| Future<void> _handleMethodCall(MethodCall call) async { | ||
| if (!mounted) return; | ||
| switch (call.method) { | ||
| case 'onComplete': | ||
| widget.onComplete?.call(); | ||
| break; | ||
| case 'onLoad': | ||
| widget.onLoad?.call(); | ||
| break; | ||
| case 'onLoadError': | ||
| widget.onLoadError?.call(); | ||
| break; | ||
| case 'onPlay': | ||
| widget.onPlay?.call(); | ||
| break; | ||
| case 'onStop': | ||
| widget.onStop?.call(); | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| @override | ||
| void dispose() { | ||
| _methodChannel?.setMethodCallHandler(null); | ||
| super.dispose(); | ||
| } | ||
| } | ||
67 changes: 67 additions & 0 deletions
67
modules/ensemble/lib/widget/lottie/ensemble_dot_lottie_controller.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| import 'package:dotlottie_flutter/dotlottie_flutter.dart'; | ||
| import 'package:flutter/services.dart'; | ||
|
|
||
| /// Playback handle for dotlottie platform views (JSON/URL via [DotLottieView], | ||
| /// bundle `.lottie` bytes via [DotLottieBytesView]). | ||
| class EnsembleDotLottieController { | ||
| EnsembleDotLottieController._(this._delegate, this._viewId); | ||
|
|
||
| factory EnsembleDotLottieController.wrap(DotLottieViewController delegate) { | ||
| return EnsembleDotLottieController._(delegate, null); | ||
| } | ||
|
|
||
| factory EnsembleDotLottieController.fromViewId(int viewId) { | ||
| return EnsembleDotLottieController._(null, viewId); | ||
| } | ||
|
|
||
| final DotLottieViewController? _delegate; | ||
| final int? _viewId; | ||
|
|
||
| MethodChannel get _channel => MethodChannel('dotlottie_view_$_viewId'); | ||
|
|
||
| Future<bool?> play() async { | ||
| final delegate = _delegate; | ||
| if (delegate != null) return delegate.play(); | ||
| return _channel.invokeMethod<bool>('play'); | ||
| } | ||
|
|
||
| Future<bool?> stop() async { | ||
| final delegate = _delegate; | ||
| if (delegate != null) return delegate.stop(); | ||
| return _channel.invokeMethod<bool>('stop'); | ||
| } | ||
|
|
||
| Future<bool?> setProgress(double progress) async { | ||
| final delegate = _delegate; | ||
| if (delegate != null) return delegate.setProgress(progress); | ||
| return _channel.invokeMethod<bool>('setProgress', {'progress': progress}); | ||
| } | ||
|
|
||
| Future<void> setMode(String mode) async { | ||
| final delegate = _delegate; | ||
| if (delegate != null) { | ||
| await delegate.setMode(mode); | ||
| return; | ||
| } | ||
| await _channel.invokeMethod('setMode', {'mode': mode}); | ||
| } | ||
|
|
||
| Future<void> setLoop(bool loop) async { | ||
| final delegate = _delegate; | ||
| if (delegate != null) { | ||
| await delegate.setLoop(loop); | ||
| return; | ||
| } | ||
| await _channel.invokeMethod('setLoop', {'loop': loop}); | ||
| } | ||
|
|
||
| Future<Map<String, dynamic>?> manifest() async { | ||
| final delegate = _delegate; | ||
| if (delegate != null) return delegate.manifest(); | ||
| final result = await _channel.invokeMethod('manifest'); | ||
| if (result is Map) { | ||
| return result.cast<String, dynamic>(); | ||
| } | ||
| return null; | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.