I have a lottie animation that I'm using as a loading screen:
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:lottie/lottie.dart';
class Loading extends StatelessWidget {
final String loadingText;
Loading({this.loadingText});
Widget build(BuildContext context) {
return Container(
width: double.infinity,
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: [
if (loadingText != null) _buildLoadingText(loadingText),
_buildAnimation(),
],
),
);
}
Widget _buildLoadingText(String text) {
return Text(
loadingText,
style: GoogleFonts.poppins(
textStyle:
TextStyle(fontWeight: FontWeight.w500, color: Colors.black)),
);
}
Widget _buildAnimation() {
return Lottie.asset('assets/lottie/heartbeat_loading.json',
width: 300, repeat: true, animate: true);
}
}
And I'm using it like this for when my app initially loads:
_determineHome() {
return StreamBuilder(
stream: AppBlocContainer().authenticationBloc().loggedIn,
builder: (context, AsyncSnapshot<AuthenticationStatus> snapshot) {
// return Loading();
return AnimatedSwitcher(
duration: Duration(seconds: 2),
child: !snapshot.hasData
? Loading(
loadingText: 'Loading...',
)
: _buildSecondChild(snapshot.data));
},
);
This works, except that the lottie animation is being loaded maybe a second or two too late, which by the time the lottie asset loads, it's too late and the page has already transitioned.
I was wondering since I did was able to precache my SVG images in my main()
by doing this:
Future.wait([
precachePicture(
ExactAssetPicture(
SvgPicture.svgStringDecoder, 'assets/svg/login.svg'),
null,
),
precachePicture(
ExactAssetPicture(
SvgPicture.svgStringDecoder, 'assets/svg/logo.svg'),
null,
),
precachePicture(
ExactAssetPicture(
SvgPicture.svgStringDecoder, 'assets/svg/signup_panel_1.svg'),
null,
)
]);
Would I be able to do the same thing with lottie?