Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
164 changes: 164 additions & 0 deletions modules/ensemble/lib/widget/lottie/dot_lottie_bytes_view.dart
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;
}
}
Comment thread
M-Talha4 marked this conversation as resolved.

@override
void dispose() {
_methodChannel?.setMethodCallHandler(null);
super.dispose();
}
}
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;
}
}
Loading
Loading