Created with <3 with dartpad.dev.
Created
January 23, 2024 05:04
-
-
Save matifdeveloper/1b11b034b7eb14210be148b226f0178b to your computer and use it in GitHub Desktop.
Text Animation Flutter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
void main() { | |
runApp(MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
home: Scaffold( | |
appBar: AppBar( | |
title: const Text('Animated Text Demo'), | |
), | |
body: Center( | |
child: AnimatedTextWidgets(), | |
), | |
), | |
); | |
} | |
} | |
class AnimatedTextWidgets extends StatefulWidget { | |
@override | |
State<AnimatedTextWidgets> createState() => _AnimatedTextWidgetsState(); | |
} | |
class _AnimatedTextWidgetsState extends State<AnimatedTextWidgets> { | |
List<String> texts = ['Hello,', 'Flutter!', 'Welcome!']; | |
int currentIndex = 0; | |
@override | |
void initState() { | |
super.initState(); | |
animateText(); | |
} | |
Future<void> animateText() async { | |
await Future.delayed(const Duration(seconds: 1)); | |
setState(() { | |
currentIndex = 1; | |
}); | |
await Future.delayed(const Duration(seconds: 2)); | |
setState(() { | |
currentIndex = 2; | |
}); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: [ | |
AnimatedOpacity( | |
duration: const Duration(seconds: 1), | |
opacity: currentIndex == 0 ? 1.0 : 0.0, | |
child: const Text( | |
'Hello,', | |
style: TextStyle(fontSize: 24.0, fontWeight: FontWeight.bold), | |
), | |
), | |
AnimatedOpacity( | |
duration: const Duration(seconds: 1), | |
opacity: currentIndex == 1 ? 1.0 : 0.0, | |
child: const Text( | |
'Flutter!', | |
style: TextStyle(fontSize: 24.0, fontWeight: FontWeight.bold), | |
), | |
), | |
AnimatedOpacity( | |
duration: const Duration(seconds: 1), | |
opacity: currentIndex == 2 ? 1.0 : 0.0, | |
child: const Text( | |
'Welcome!', | |
style: TextStyle(fontSize: 24.0, fontWeight: FontWeight.bold), | |
), | |
), | |
], | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment