Skip to content

Instantly share code, notes, and snippets.

@VB10
Last active October 25, 2021 03:46
Show Gist options
  • Save VB10/a1bf07e7a6aec157666664923e58bd79 to your computer and use it in GitHub Desktop.
Save VB10/a1bf07e7a6aec157666664923e58bd79 to your computer and use it in GitHub Desktop.
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:lottie/lottie.dart';
class NotiferChanges<T> {
final StreamController<T> _fetchDoneController = StreamController.broadcast();
changeNavigate(T data) {
_fetchDoneController.add(data); // send an arbitrary event
}
Stream<T> get fetchDone => _fetchDoneController.stream;
}
class LottieAssetCustom extends StatefulWidget {
final String lottiePath;
final double next;
final double back;
final NotiferChanges<bool> changes;
const LottieAssetCustom(
{Key? key, required this.lottiePath, required this.next, required this.back, required this.changes})
: super(key: key);
@override
State<LottieAssetCustom> createState() => _LottieCustomState();
}
class _LottieCustomState extends State<LottieAssetCustom> with SingleTickerProviderStateMixin {
late final AnimationController _controller;
@override
void initState() {
super.initState();
_controller = AnimationController(vsync: this);
widget.changes.fetchDone.listen((event) {
event ? _nextAnimation() : _backAnimation();
});
}
void _nextAnimation() {
_controller.animateTo(widget.next);
}
void _backAnimation() {
_controller.animateBack(widget.back);
}
@override
Widget build(BuildContext context) {
return Lottie.asset(widget.lottiePath, controller: _controller, onLoaded: (item) {
_controller.duration = item.duration;
});
}
}
ListTile(
title: Text(LocaleKeys.settings_changeTheme.tr()),
onTap: () async {
if (context.read<ThemeManager>().themeEnums == ThemeType.light) {
_notiferChanges.changeNavigate(false);
} else {
_notiferChanges.changeNavigate(true);
}
},
trailing: LottieAssetCustom(
lottiePath: ImageConstants.instance.lottieAssets.darkMode,
next: 0.4,
back: 0,
changes: _notiferChanges,
)),
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment