Skip to content

Instantly share code, notes, and snippets.

@HansMuller
Created December 6, 2022 01:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HansMuller/ff8f8e53fa86932d2f3984b7b3421d9f to your computer and use it in GitHub Desktop.
Save HansMuller/ff8f8e53fa86932d2f3984b7b3421d9f to your computer and use it in GitHub Desktop.
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