nc-photos/lib/widget/animated_visibility.dart

65 lines
1.4 KiB
Dart
Raw Normal View History

2021-05-05 10:45:56 +02:00
import 'package:flutter/widgets.dart';
/// AnimatedOpacity + Visibility
///
/// The point is to disable non-visible buttons
class AnimatedVisibility extends StatefulWidget {
const AnimatedVisibility({
2021-07-23 22:05:57 +02:00
Key? key,
required this.child,
required this.opacity,
2021-05-05 10:45:56 +02:00
this.curve = Curves.linear,
2021-07-23 22:05:57 +02:00
required this.duration,
2021-05-05 10:45:56 +02:00
this.onEnd,
this.alwaysIncludeSemantics = false,
2021-07-23 22:05:57 +02:00
}) : assert(opacity >= 0.0 && opacity <= 1.0),
2021-05-05 10:45:56 +02:00
super(key: key);
@override
createState() => _AnimatedVisibilityState();
final Widget child;
final double opacity;
final Curve curve;
final Duration duration;
2021-07-23 22:05:57 +02:00
final VoidCallback? onEnd;
2021-05-05 10:45:56 +02:00
final bool alwaysIncludeSemantics;
}
class _AnimatedVisibilityState extends State<AnimatedVisibility> {
@override
initState() {
super.initState();
_isActive = widget.opacity > 0;
}
2021-05-05 10:45:56 +02:00
@override
build(BuildContext context) {
if (!_isActive && widget.opacity > 0) {
_isActive = true;
}
return AnimatedOpacity(
opacity: widget.opacity,
curve: widget.curve,
duration: widget.duration,
onEnd: _onEnd,
alwaysIncludeSemantics: widget.alwaysIncludeSemantics,
child: Visibility(
visible: _isActive,
child: widget.child,
),
);
}
void _onEnd() {
if (widget.opacity == 0) {
setState(() {
_isActive = false;
});
}
widget.onEnd?.call();
}
late bool _isActive;
2021-05-05 10:45:56 +02:00
}