44 lines
1.1 KiB
Dart
44 lines
1.1 KiB
Dart
import 'package:flutter/material.dart';
|
|
|
|
/// タップ時に軽く沈み込むスケールアニメーションを付与するラッパー。
|
|
/// 既存の GestureDetector / InkWell の代わりに使用する。
|
|
class Pressable extends StatefulWidget {
|
|
const Pressable({
|
|
super.key,
|
|
required this.child,
|
|
this.onTap,
|
|
this.scale = 0.97,
|
|
this.duration = const Duration(milliseconds: 100),
|
|
});
|
|
|
|
final Widget child;
|
|
final VoidCallback? onTap;
|
|
final double scale;
|
|
final Duration duration;
|
|
|
|
@override
|
|
State<Pressable> createState() => _PressableState();
|
|
}
|
|
|
|
class _PressableState extends State<Pressable> {
|
|
bool _pressed = false;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return GestureDetector(
|
|
onTapDown: (_) => setState(() => _pressed = true),
|
|
onTapUp: (_) {
|
|
setState(() => _pressed = false);
|
|
widget.onTap?.call();
|
|
},
|
|
onTapCancel: () => setState(() => _pressed = false),
|
|
child: AnimatedScale(
|
|
scale: _pressed ? widget.scale : 1.0,
|
|
duration: widget.duration,
|
|
curve: Curves.easeOut,
|
|
child: widget.child,
|
|
),
|
|
);
|
|
}
|
|
}
|