Created
June 5, 2021 15:10
-
-
Save IsmailAlamKhan/47c49e19c64c42f373a7f344d2c75369 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:math'; | |
import 'package:flutter/material.dart'; | |
void main() => runApp(MyApp()); | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Material App', | |
home: Home(), | |
); | |
} | |
} | |
class Home extends StatefulWidget { | |
const Home({ | |
Key? key, | |
}) : super(key: key); | |
@override | |
_HomeState createState() => _HomeState(); | |
} | |
class _HomeState extends State<Home> { | |
Color color = Colors.red; | |
final random = Random(); | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text('Material App Bar'), | |
), | |
body: Center( | |
child: SizedBox( | |
height: 300, | |
width: 300, | |
child: AnimatedCard( | |
child: InkWell( | |
onTap: () { | |
setState(() { | |
color = Colors | |
.primaries[random.nextInt(Colors.primaries.length - 1)]; | |
}); | |
}, | |
child: Center(child: Text('Helo')), | |
), | |
duration: Duration(milliseconds: 300), | |
cardTheme: CardTheme(color: color), | |
), | |
), | |
), | |
); | |
} | |
} | |
class AnimatedCard extends ImplicitlyAnimatedWidget { | |
AnimatedCard({ | |
Key? key, | |
Duration? duration, | |
Curve? curve, | |
required this.child, | |
this.cardTheme, | |
}) : super( | |
key: key, | |
duration: duration ?? const Duration(milliseconds: 500), | |
curve: curve ?? Curves.easeIn, | |
); | |
final Widget child; | |
final CardTheme? cardTheme; | |
@override | |
_AnimatedCardState createState() => _AnimatedCardState(); | |
} | |
class _AnimatedCardState extends AnimatedWidgetBaseState<AnimatedCard> { | |
CardThemeTween? _data; | |
@override | |
Widget build(BuildContext context) { | |
return Card( | |
color: _data!.evaluate(animation).color, | |
elevation: _data!.evaluate(animation).elevation, | |
shadowColor: _data!.evaluate(animation).shadowColor, | |
shape: _data!.evaluate(animation).shape, | |
clipBehavior: _data!.evaluate(animation).clipBehavior, | |
margin: _data!.evaluate(animation).margin, | |
child: widget.child, | |
); | |
} | |
@override | |
void forEachTween(TweenVisitor<dynamic> visitor) { | |
final _cardTheme = widget.cardTheme ?? Theme.of(context).cardTheme; | |
_data = visitor( | |
_data, | |
_cardTheme, | |
(begin) => CardThemeTween(begin: begin), | |
) as CardThemeTween?; | |
} | |
} | |
class CardThemeTween extends Tween<CardTheme> { | |
CardThemeTween({CardTheme? begin, CardTheme? end}) | |
: super(begin: begin, end: end); | |
@override | |
CardTheme lerp(double t) => CardTheme.lerp(begin!, end!, t); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment