Created
March 2, 2023 19:14
-
-
Save hhoang/2c54c8b13d9a88d0c74972cdb05869ea to your computer and use it in GitHub Desktop.
Flutter bug
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 'package:flutter/material.dart'; | |
void main() { | |
runApp(const MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
const MyApp({super.key}); | |
// This widget is the root of your application. | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: const MyHomePage(title: 'Flutter Demo Home Page'), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
const MyHomePage({super.key, required this.title}); | |
final String title; | |
@override | |
State<MyHomePage> createState() => _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> with TickerProviderStateMixin { | |
late AnimationController _controller; | |
static const _animationDuration = Duration(milliseconds: 100); | |
static const _curve = Curves.easeIn; | |
final _curveFlipped = _curve.flipped; | |
Animation<double> get _scale => Tween<double>( | |
begin: 1.0, | |
end: 0.76, | |
).animate( | |
CurvedAnimation( | |
parent: _controller, | |
curve: _curve, | |
reverseCurve: _curveFlipped, | |
), | |
); | |
@override | |
void initState() { | |
super.initState(); | |
_controller = AnimationController( | |
vsync: this, | |
duration: _animationDuration, | |
); | |
} | |
final scrollController = ScrollController(); | |
@override | |
Widget build(BuildContext context) { | |
// This method is rerun every time setState is called, for instance as done | |
// by the _incrementCounter method above. | |
// | |
// The Flutter framework has been optimized to make rerunning build methods | |
// fast, so that you can just rebuild anything that needs updating rather | |
// than having to individually change instances of widgets. | |
return Scaffold( | |
appBar: AppBar( | |
// Here we take the value from the MyHomePage object that was created by | |
// the App.build method, and use it to set our appbar title. | |
title: Text(widget.title), | |
), | |
body: Center( | |
// Center is a layout widget. It takes a single child and positions it | |
// in the middle of the parent. | |
child: CustomScrollView( | |
controller: scrollController, | |
cacheExtent: double.maxFinite, | |
slivers: [ | |
SliverToBoxAdapter( | |
child: Listener( | |
onPointerDown: _handleOnPointerDown, | |
onPointerUp: _handleOnPointerUp, | |
child: AnimatedBuilder( | |
builder: _buildAnimation, | |
animation: _controller, | |
child: Text('hello'), | |
), | |
) | |
), | |
], | |
), | |
), | |
); | |
} | |
void _handleOnPointerDown(_) { | |
setState(() { | |
_isPointerDown = true; | |
}); | |
_controller.forward().whenComplete(() { | |
if (!_isPointerDown) _controller.reverse(); | |
}); | |
} | |
bool _isPointerDown = false; | |
bool get _isAnimating => _controller.isAnimating; | |
void _handleOnPointerUp(_) { | |
setState(() { | |
_isPointerDown = false; | |
}); | |
if (!_isAnimating) _controller.reverse(); | |
} | |
Widget _buildAnimation(BuildContext context, Widget? child){ | |
return Transform.scale( | |
scale: _scale.value, | |
child:Container( | |
decoration: const ShapeDecoration( | |
shape: RoundedRectangleBorder( | |
side: BorderSide(color: Colors.red, width: 1), | |
borderRadius: BorderRadius.all(Radius.circular(8) | |
), | |
), | |
color: Colors.white, | |
), | |
child: Transform.scale( | |
scale: 1 + (1 - _scale.value), | |
child: child, | |
), | |
) | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment