Created
March 28, 2023 08:14
-
-
Save mono0926/a2a1039c6c963b2e432b3f4be6457465 to your computer and use it in GitHub Desktop.
Shake animation
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 'dart:math'; | |
import 'package:flutter/material.dart'; | |
void main() { | |
runApp(const MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
const MyApp({super.key}); | |
@override | |
Widget build(BuildContext context) { | |
return const MaterialApp( | |
title: 'Flutter Demo', | |
home: HomePage(), | |
); | |
} | |
} | |
class HomePage extends StatefulWidget { | |
const HomePage({super.key}); | |
@override | |
State<HomePage> createState() => _HomePageState(); | |
} | |
class _HomePageState extends State<HomePage> { | |
int _counter = 0; | |
void _incrementCounter() { | |
setState(() { | |
_counter++; | |
}); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: const Text('Flutter Demo Home Page'), | |
), | |
body: Center( | |
child: TweenAnimationBuilder( | |
tween: Tween<double>(begin: 0, end: _counter.toDouble()), | |
duration: const Duration(milliseconds: 500), | |
builder: (context, value, child) => Transform.translate( | |
offset: Offset(20 * sin(4 * pi * value), 0), | |
child: const FlutterLogo(size: 64), | |
), | |
), | |
), | |
floatingActionButton: FloatingActionButton( | |
onPressed: _incrementCounter, | |
tooltip: 'Shake', | |
child: const Icon(Icons.refresh), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment