I need a Lottie to be fitted to the whole screen, so I set Lottie height and width to double.infinity
.
But the Lottie getting expanded from top left corner instead of from centre making the centre of lottie slightly moved to the right bottom direction, No matter the alignment. Also tried wrapping in Center
and Align
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Lottie.asset(
'splash_logo.json',
alignment: Alignment.center,
repeat: true,
height: double.infinity,
fit: BoxFit.cover,
width: double.infinity,
),
),
);
}
How to expand it keeping the centre of the Lottie at the Centre of the screen
2
Answers
You can make use of a
Stack
withPositioned.fill()
to make the Lottie animation expand keeping its center at the center of the screen. Also, to ensure proper scalability wrap the animation withFittedBox
. Something like:You could try to wrap the Lottie animation in a
Container
and set the height and width of theContainer
todouble.infinity
. This will allow theContainer
to fill the entire screen, and the animation will be centered within thisContainer
.Example;
Another way is to use the
OverflowBox
widget to allows its child to overflow the parent’s box, which can help with alignment issues.