2020-07-27 20:43:58 +00:00
|
|
|
import 'dart:io';
|
|
|
|
|
|
|
|
import 'package:flutter/cupertino.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
|
|
|
// This is a button that pushes the bare minimum onto you, it doesn't even respect button themes - unless you tell it to
|
|
|
|
class SpecialButton extends StatefulWidget {
|
2022-09-21 20:27:35 +00:00
|
|
|
const SpecialButton({Key? key, this.child, this.color, this.onPressed, this.useButtonTheme = false, this.decoration})
|
2020-07-27 20:43:58 +00:00
|
|
|
: super(key: key);
|
|
|
|
|
2022-09-21 20:27:35 +00:00
|
|
|
final Widget? child;
|
|
|
|
final Color? color;
|
2020-07-27 20:43:58 +00:00
|
|
|
final bool useButtonTheme;
|
2022-09-21 20:27:35 +00:00
|
|
|
final BoxDecoration? decoration;
|
2020-07-27 20:43:58 +00:00
|
|
|
|
2022-09-21 20:27:35 +00:00
|
|
|
final GestureTapCallback? onPressed;
|
2020-07-27 20:43:58 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
_SpecialButtonState createState() => _SpecialButtonState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _SpecialButtonState extends State<SpecialButton> with SingleTickerProviderStateMixin {
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Platform.isAndroid ? _buildAndroid() : _buildGeneric();
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget _buildAndroid() {
|
|
|
|
var textStyle;
|
|
|
|
if (widget.useButtonTheme) {
|
|
|
|
textStyle = Theme.of(context).textTheme.button;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Material(
|
|
|
|
textStyle: textStyle,
|
|
|
|
child: Ink(
|
|
|
|
decoration: widget.decoration,
|
|
|
|
color: widget.color,
|
|
|
|
child: InkWell(
|
|
|
|
child: widget.child,
|
|
|
|
onTap: widget.onPressed,
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget _buildGeneric() {
|
|
|
|
var textStyle = CupertinoTheme.of(context).textTheme.textStyle;
|
|
|
|
if (widget.useButtonTheme) {
|
|
|
|
textStyle = CupertinoTheme.of(context).textTheme.actionTextStyle;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Container(
|
|
|
|
decoration: widget.decoration,
|
|
|
|
child: GestureDetector(
|
|
|
|
behavior: HitTestBehavior.opaque,
|
|
|
|
onTapDown: _handleTapDown,
|
|
|
|
onTapUp: _handleTapUp,
|
|
|
|
onTapCancel: _handleTapCancel,
|
|
|
|
onTap: widget.onPressed,
|
|
|
|
child: Semantics(
|
|
|
|
button: true,
|
|
|
|
child: FadeTransition(
|
2022-09-21 20:27:35 +00:00
|
|
|
opacity: _opacityAnimation!,
|
2020-07-27 20:43:58 +00:00
|
|
|
child: DefaultTextStyle(style: textStyle, child: Container(child: widget.child, color: widget.color)),
|
2021-05-03 21:58:04 +00:00
|
|
|
),
|
2020-07-27 20:43:58 +00:00
|
|
|
),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Eyeballed values. Feel free to tweak.
|
|
|
|
static const Duration kFadeOutDuration = Duration(milliseconds: 10);
|
|
|
|
static const Duration kFadeInDuration = Duration(milliseconds: 100);
|
|
|
|
final Tween<double> _opacityTween = Tween<double>(begin: 1.0);
|
|
|
|
|
2022-09-21 20:27:35 +00:00
|
|
|
AnimationController? _animationController;
|
|
|
|
Animation<double>? _opacityAnimation;
|
2020-07-27 20:43:58 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
_animationController = AnimationController(
|
|
|
|
duration: const Duration(milliseconds: 200),
|
|
|
|
value: 0.0,
|
|
|
|
vsync: this,
|
|
|
|
);
|
2022-09-21 20:27:35 +00:00
|
|
|
_opacityAnimation = _animationController!.drive(CurveTween(curve: Curves.decelerate)).drive(_opacityTween);
|
2020-07-27 20:43:58 +00:00
|
|
|
_setTween();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void didUpdateWidget(SpecialButton old) {
|
|
|
|
super.didUpdateWidget(old);
|
|
|
|
_setTween();
|
|
|
|
}
|
|
|
|
|
|
|
|
void _setTween() {
|
|
|
|
_opacityTween.end = 0.4;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
2022-09-21 20:27:35 +00:00
|
|
|
_animationController?.dispose();
|
2020-07-27 20:43:58 +00:00
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool _buttonHeldDown = false;
|
|
|
|
|
|
|
|
void _handleTapDown(TapDownDetails event) {
|
|
|
|
if (!_buttonHeldDown) {
|
|
|
|
_buttonHeldDown = true;
|
|
|
|
_animate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void _handleTapUp(TapUpDetails event) {
|
|
|
|
if (_buttonHeldDown) {
|
|
|
|
_buttonHeldDown = false;
|
|
|
|
_animate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void _handleTapCancel() {
|
|
|
|
if (_buttonHeldDown) {
|
|
|
|
_buttonHeldDown = false;
|
|
|
|
_animate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void _animate() {
|
2022-09-21 20:27:35 +00:00
|
|
|
if (_animationController == null || _animationController!.isAnimating) {
|
2020-07-27 20:43:58 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
final bool wasHeldDown = _buttonHeldDown;
|
|
|
|
final TickerFuture ticker = _buttonHeldDown
|
2022-09-21 20:27:35 +00:00
|
|
|
? _animationController!.animateTo(1.0, duration: kFadeOutDuration)
|
|
|
|
: _animationController!.animateTo(0.0, duration: kFadeInDuration);
|
2020-07-27 20:43:58 +00:00
|
|
|
|
|
|
|
ticker.then<void>((void value) {
|
|
|
|
if (mounted && wasHeldDown != _buttonHeldDown) {
|
|
|
|
_animate();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|