Created
January 14, 2022 11:46
-
-
Save Kurogoma4D/f5125124cd4b22b78042ba6fbcc35713 to your computer and use it in GitHub Desktop.
This file contains hidden or 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'; | |
import 'package:flutter/physics.dart'; | |
import 'package:flutter_hooks/flutter_hooks.dart'; | |
void main() => runApp(MyApp()); | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
debugShowCheckedModeBanner: false, | |
theme: ThemeData(primarySwatch: Colors.blue), | |
home: const Home(), | |
); | |
} | |
} | |
class Home extends StatelessWidget { | |
const Home({Key? key}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar(title: const Text('Animation')), | |
body: const Center(child: _Animation()), | |
); | |
} | |
} | |
const kRestitution = 0.6; | |
class _Animation extends HookWidget { | |
const _Animation({Key? key}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
const boxSize = 20.0; | |
final barHeight = MediaQuery.of(context).size.height * 0.8; | |
final controller = | |
useAnimationController(duration: const Duration(seconds: 3)); | |
final animation = | |
CurvedAnimation(parent: controller, curve: Curves.bounceOut); | |
return Stack( | |
children: [ | |
Container( | |
decoration: BoxDecoration( | |
border: Border.all(color: Colors.black54, width: 2.0), | |
), | |
height: barHeight, | |
width: boxSize, | |
), | |
AnimatedBuilder( | |
animation: controller, | |
builder: (context, _) { | |
return Positioned( | |
top: animation.value * (barHeight - boxSize), | |
left: 0, | |
child: GestureDetector( | |
onTap: () => controller.animateWith( | |
GravitySimulation(1.0, 0.0, barHeight, 0.0), | |
), | |
child: Container( | |
height: boxSize, | |
width: boxSize, | |
color: Colors.red.withOpacity(0.54), | |
), | |
), | |
); | |
}), | |
], | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment