Skip to content

Instantly share code, notes, and snippets.

@vinceramcesoliveros
Created December 5, 2018 14:22
Show Gist options
  • Save vinceramcesoliveros/1278445bbc7fb6808ee4d0d7b4472866 to your computer and use it in GitHub Desktop.
Save vinceramcesoliveros/1278445bbc7fb6808ee4d0d7b4472866 to your computer and use it in GitHub Desktop.
Stateful Animation
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
showPerformanceOverlay: true,
title: 'Flutter Demo',
theme: ThemeData(
brightness: Brightness.dark,
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatelessWidget {
final String title;
MyHomePage({this.title});
@override
Widget build(BuildContext context) {
return AnimatedBackground();
}
}
class AnimatedBackground extends StatefulWidget {
const AnimatedBackground({
Key key,
}) : super(key: key);
@override
AnimatedBackgroundState createState() {
return new AnimatedBackgroundState();
}
}
class AnimatedBackgroundState extends State<AnimatedBackground>
with SingleTickerProviderStateMixin {
Animation animation;
AnimationController animationController;
@override
void dispose() {
// TODO: implement dispose
super.dispose();
animationController.dispose();
}
@override
void initState() {
// TODO: implement initState
super.initState();
animationController =
AnimationController(vsync: this, duration: Duration(seconds: 2));
animation = Tween(begin: -1, end: 0).animate( //This is the problem, I use -1.0 & 0.0 instead
CurvedAnimation(curve: Curves.easeInOut, parent: animationController));
animationController.repeat();
}
@override
Widget build(BuildContext context) {
final double width = MediaQuery.of(context).size.width;
return AnimatedBuilder(
animation: animationController,
builder: (context, _) => Scaffold(
body: Transform(
transform: Matrix4.translationValues(
animation.value * width, 0.0, 0.0),
child: Center(child: Text("Dash")))));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment