Created
December 6, 2022 01:11
-
-
Save HansMuller/ff8f8e53fa86932d2f3984b7b3421d9f 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 'dart:ui' show lerpDouble; | |
import 'package:flutter/material.dart'; | |
class Home extends StatefulWidget { | |
const Home({ super.key }); | |
@override | |
State<Home> createState() => _HomeState(); | |
} | |
class _HomeState extends State<Home> with SingleTickerProviderStateMixin { | |
late AnimationController controller; | |
double initialOpacity = 1.0; | |
double targetOpacity = 1.0; | |
@override | |
void initState() { | |
super.initState(); | |
controller = AnimationController( | |
vsync: this, | |
duration: Duration(milliseconds: 400), | |
); | |
} | |
@override | |
void dispose() { | |
controller.dispose(); | |
super.dispose(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
body: Center( | |
child: AnimatedBuilder( | |
animation: controller, | |
builder: (BuildContext context, Widget? child) { | |
final double opacity = lerpDouble(initialOpacity, targetOpacity, controller.value)!; | |
return Container( | |
padding: EdgeInsets.all(16), | |
color: Colors.grey.withOpacity(opacity), | |
child: TextButton( | |
onPressed: () { | |
initialOpacity = opacity; | |
targetOpacity = initialOpacity - 0.2; | |
controller.value = 0; | |
controller.forward(); | |
}, | |
child: Text('Press Me'), | |
), | |
); | |
}, | |
), | |
), | |
); | |
} | |
} | |
void main() { | |
runApp( | |
MaterialApp( | |
theme: ThemeData.light(useMaterial3: true), | |
home: Home(), | |
), | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment