Last active
June 22, 2023 06:35
-
-
Save divyanshub024/b25b3c627d1d039e00896ab81b2485d5 to your computer and use it in GitHub Desktop.
Flutter Animation I: Background Color Transition
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( | |
title: 'Background color transition', | |
debugShowCheckedModeBanner: false, | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: MyHomePage(title: 'Flutter Demo Home Page'), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
MyHomePage({Key key, this.title}) : super(key: key); | |
final String title; | |
@override | |
_MyHomePageState createState() => _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> | |
with SingleTickerProviderStateMixin { | |
AnimationController _controller; | |
@override | |
void initState() { | |
super.initState(); | |
_controller = AnimationController( | |
duration: const Duration(seconds: 10), | |
vsync: this, | |
)..repeat(); | |
} | |
Animatable<Color> background = TweenSequence<Color>([ | |
TweenSequenceItem( | |
weight: 1.0, | |
tween: ColorTween( | |
begin: Colors.red, | |
end: Colors.green, | |
), | |
), | |
TweenSequenceItem( | |
weight: 1.0, | |
tween: ColorTween( | |
begin: Colors.green, | |
end: Colors.blue, | |
), | |
), | |
TweenSequenceItem( | |
weight: 1.0, | |
tween: ColorTween( | |
begin: Colors.blue, | |
end: Colors.pink, | |
), | |
), | |
]); | |
@override | |
Widget build(BuildContext context) { | |
return AnimatedBuilder( | |
animation: _controller, | |
builder: (context, child) { | |
return Scaffold( | |
body: Container( | |
color: background | |
.evaluate(AlwaysStoppedAnimation(_controller.value)), | |
), | |
); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This code is not working recent flutter version