How to fix animation glitches in Flutter Lottie - flutter

I have an app with lots of Lottie animations. They work fine in a Windows UWP app and AirBnB sample Android app. Also look fine in lottiefiles.com online test tool.
But they are glitchy in Flutter using Lottie for Flutter package. Not all of them, but many, maybe around a third.
Full source code below, you can try for yourself.
Glitches are either frame overlaps or blinking, as if there is a gap between frames.
Glitches are consistent, i.e. always happen in the same place, so does not seem like a performance issue.
Also, they are not happening between repeats, but in the middle of the animation, so again not an application issue but seems to be the issue with how they are rendered.
I have tried loading them as composition from memory with a controller. I have also tried a vanilla asset load to rule out issues with my implementation, and they are behaving the same. The issue appears both in the emulator and the actual phone. So it seems to me it is not caused by the way I implemented but by the Lottie package itself, or perhaps an issue with Lottie JSON that for some reason only affects Android.
Full example is below. Just paste this into main.dart and add dependency to yaml: lottie: ^1.2.1.
import 'package:flutter/material.dart';
import 'package:lottie/lottie.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(),
darkTheme: ThemeData.dark(),
themeMode: ThemeMode.dark,
home: MyHomePage(),
);
}
}
class MyHomePage extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Lottie Test"),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Expanded(
child: Lottie.network(
'https://assets6.lottiefiles.com/packages/lf20_uz92k22x.json'),
),
],
),
),
);
}
}

Try adding frameRate: FrameRate.max like stated below.
Expanded(
child: Lottie.network('https://assets6.lottiefiles.com/packages/lf20_uz92k22x.json',
frameRate: FrameRate.max,
),
),
this will render the Lottie animation with the same frame rate of that of the app. In Lottie plugin it will render the json as per the frame rate mentioned in the json file. An animation encoded with a 25 FPS will only be painted 25 times per seconds even though the AnimationController will invalidate the widget 60 times per seconds. Hence this framerate was introduced from Lottie 0.6.0

I use Lottie for Flutter on my applications and it works well also on emulator or on low-performance phones. The problem can be related to bad converted animations, or you can try to use Lottie.asset(yourjson) to avoid potential network fetch issues.

According to the official package documentation
For Flutter Web, run the app with: flutter run --web-renderer canvaskit
Works fine for me in the latest version: lottie: ^1.2.1

Related

Flutter (Unable to load asset)

I'm unable to load images to my app although I tried many solutions
my code is
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
backgroundColor: Colors.amber,
appBar: AppBar(
backgroundColor: Colors.amber[900],
title: const Text("hi"),
),
body: Image(image: AssetImage("images/ab.png"),
),
),
);
}
}
this is my pubspec.yaml
flutter doctor
1- I used $ flutter clean and then $ flutter pub get.
2- I updated flutter SDK.
3- I mention only the folder (images).
4- I used a smaller image.
5- I restarted my laptop.
6- I run my code in another IDE.
but nothing is working for me, so can anyone help me?
thanks in advance
I copied your main.dart code, I created a images folder with a .png file.
I think the problem could be the position of the images folder, you should place it beside the lib folder.
Without seeing the project folder structure I cannot be sure, but if you do it like that your code should work fine.
Here's the pubspec.yaml:
flutter:
assets:
- images/
Here's a screenshot of the your code running:

Page Transition is not working specifically scale or fade functions

I am using animated splash screen, but my page transition does not working.
Widget build(BuildContext context) {
return AnimatedSplashScreen(
splash: Image.asset(
'lib/logo/MedicteLogo-4a2e31cd2358bb08ff8d12acb2761357.png'),
nextScreen: HomePage(),
splashTransition: SplashTransition.scaleTransition,
pageTransitionType: PageTransitionType.topToBottomJoined,
);
}
try import this code. I succeeded after importing the code
import 'package:page_transition/page_transition.dart';
I'm also having the same problem here "PageTransition" is not working at all, after duration of splash screen finishes it removes splash screen without an PageTransition and shows next screen.
I think it has something to do with my Android version which is Android 12

Animation from assets in Flutter

I'm currently working over an app similar to good old Tamagotchi with modern twists. Right now I prepared some code and didn't need any character animations but now I'm stuck. I want to make a scaffold with title, line of buttons below it, yet still on top of the screen, second bunch on bottom and create some kind of interactivity inside between those two, with character made from assets, some animations and reactions on touch. To be clear - something like this:
class _AnimationState extends State<Animation> {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
"some title here"
),
body:
Column(
children: [
Row(here some buttons)
Column(with animation, height as was left on screen)
Row(another line of buttons)
],
),
);
}
}
I was thinking about flame engine, found even tutorial, but when I'm trying to implement it i receive exception
The method '_mulFromInteger' was called on null. Receiver: null Tried
calling: _mulFromInteger(0)
This is a tutorial I worked on:
https://medium.com/flutter-community/sprite-sheet-animations-in-flutter-1b693630bfb3
And code i tried to implement:
Center(
child:
Flame.util.animationAsWidget(Position(70, 70),
animation.Animation.sequenced('minotaur.png', 5, textureWidth: 50),
),
),
The other idea was to use Sprite Widget but I never used any animations in my previous projects, nor in Flutter, C# or whatever language i had in college.
I would appreciate any advices, tutorials or examples how to handle animation from assets.
That tutorial is quite old, a lot has happened in the world of Flame since then.
I'm guessing you are using version 1.0.0-rc9 of Flame?
Something like this should work with the that version:
final animationData = SpriteAnimationData.sequenced(
amount: 5, stepTime: 1.0, textureSize: Vector2.all(50));
final spriteAnimation = await SpriteAnimation.load(
'minotaur.png' animationData);
...
Center(
child: SpriteAnimationWidget(
animation: spriteAnimation,
playing: true,
anchor: Anchor.center,
),
)
An example of it can be found here.
There is also some short documentation for it here.

My app lost connection when I try play a sound using audioplayers

I am using Flutter and whenever I want to to play a sound it crashes and throw me this:
iOS => call startHeadlessService, playerId d580eb03-1cab-4a44-9b7d-5b80ac32a53c
iOS => call play, playerId d580eb03-1cab-4a44-9b7d-5b80ac32a53c
play!
Lost connection to device.
I don't know why, I followed step by step.
Here's the code I write if it's helping:
import 'package:flutter/material.dart';
import 'package:audioplayers/audio_cache.dart';
void main() => runApp(XylophoneApp());
class XylophoneApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: SafeArea(
child: Center(
child: FlatButton(
onPressed: () {
final player = AudioCache();
player.play('note1.wav');
},
child: Text('Play me!'),
),
),
),
),
);
}
}
And Here is my pubspec.yaml :
dependencies:
flutter:
sdk: flutter
cupertino_icons: ^0.1.2
audioplayers: ^0.16.0
I've done everthing that was in that course step by step, but instead of asking me to allow microphones ( that was in course ), the app just lost connection.
I don't know why, in the course never explain what to do if you meet errors.
Can you help me understand why is this happening and how can be handled? It would help me a lot. Thank you in advance!
The emulator is iPhone 11 pro max.
Also I tried running it by calling " flutter run " in the terminal
With the help of J. Saw from the comments above, we changed the package version from 0.16 to 0.10 and it's working

Starting with plain (non material ui) canvas in flutter

All tutorials and documentation I've seen so far start of with importing flutter material. I am wondering is this an absolute requirement? What if I want to start with a plain canvas and build my own theme / widgets. Can I do this, if so what package should be used here so I get access to default widgets?
Although the answers here are correct, I want to add a few more points. To use raw widgets use import 'package:flutter/widgets.dart';
Here is a working example:
import 'package:flutter/widgets.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Container(
padding: EdgeInsets.all(25.0),
child: Directionality(
textDirection: TextDirection.ltr,
child:Text("hello world",
style: TextStyle(
color: Color.fromRGBO(255, 255, 255, 1)
),
)));
}
}
The directionality is needed, the padding was added so that we see the message, otherwise it is masked by the menubar in phone.
Widgets in flutter makes the developers day easy. All the widgets are built on top dart:ui lib. It is up to you, to decide to use existing set of widgets or develop your ui from scratch. Flutter does not stop you from writing your own widgets.
You can find a few raw example of here, that does not use any widgets at all.
If you simple don't want only material widgets, then you can just build your own themed widgets with all other basic widgets and layouts available in flutter.
If you wanted to build few of your own widgets with a canvas and use it along with other widgets in flutter, you look into CustomPaint and CustomPainter widgets.
Hope this helped!
Just as Chandan Purohit answered, use import 'package:flutter/widgets.dart';. Flutter official gives a minimal app in Introduction to widgets as follows:
import 'package:flutter/material.dart';
void main() {
runApp(
const Center(
child: Text(
'Hello, world!',
textDirection: TextDirection.ltr,
),
),
);
}
Just change flutter/material.dart to flutter/widgets.dart, and you get a plain canvas to start.
Note: The color of Text is white by default.