Is there a way to make the "chapter" text slowly move to the left, like in an animation, when clicking the floatingActionButton in the code below?
class _MyHomePageState extends State<MyHomePage> {
bool visible = false;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: const Text('Flutter Demo Home Page'),
centerTitle: true,
),
body: Center(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text(
'Chapter ',
style: TextStyle(fontSize: 22),
),
Visibility(
visible: visible,
maintainAnimation: true,
maintainState: true,
child: AnimatedOpacity(
opacity: visible ? 1 : 0,
duration: const Duration(milliseconds: 500),
child: const Text(
'01',
style: TextStyle(fontSize: 22),
),
),
),
],
),
),
floatingActionButton: FloatingActionButton(
child: const Icon(Icons.add),
onPressed: () {
setState(
() => visible = !visible,
);
},
),
);
}
}
I would like the "chapter" text to move in the same duration applied in the AnimatedOpacity Widget.
3
Answers
You can implement your own custom animation or use a package like:
After just wrap your widget with SlideInLeft and customize the duration of the animation, with the property animate you can specify when launch your animation.
You can use animated_text_kit Package
To make the "Chapter" text slowly move to the left when clicking the FloatingActionButton, you can wrap the "Chapter" text with an AnimatedPositioned widget. Here’s the updated code:
In this updated code, the "Chapter" text will slowly move to the left when the FloatingActionButton is clicked, thanks to the AnimatedPositioned widget. You can adjust the
left
value in the AnimatedPositioned widget to control the initial position of the "Chapter" text before it animates to the final position.