Flutter如何实现长按带进度边框、满足时长触发功能的TextButton
实现思路
- 你考虑用GestureDetector的方向完全没问题,完全可以和现有TextButton组件兼容,不需要依赖原有方案的CircularProgressIndicator,直接通过自定义绘制+手势监听就能实现。
- 先定义核心状态:声明
AnimationController用来同步进度和时长、bool变量标记当前是否处于按压状态、Duration变量设置触发目标函数需要的长按总时长,比如设为2秒就写const Duration(seconds: 2)。 - 手势监听逻辑:用GestureDetector包裹TextButton,监听
onTapDown/onPanStart事件时启动正向动画,监听onTapUp/onPanEnd/onTapCancel事件时停止动画,如果此时动画进度未到100%就启动反向动画重置进度;如果动画完成回调触发,就调用你要执行的目标函数。 - 边框进度绘制:可以直接通过CustomPaint包裹TextButton,根据
AnimationController的value值(范围0-1,刚好对应按压时长占总触发时长的比例)绘制对应进度的边框,也可以直接动态修改TextButton的style属性里的边框参数,实现进度更新。 - 可选优化:给动画设置线性曲线保证进度匀速变化,也可以加按压缩放的联动效果,提升交互体验。
核心代码示例:
class HoldProgressTextButton extends StatefulWidget { final String text; final Duration requiredHoldDuration; final VoidCallback onHoldComplete; const HoldProgressTextButton({ super.key, required this.text, required this.requiredHoldDuration, required this.onHoldComplete, }); @override State<HoldProgressTextButton> createState() => _HoldProgressTextButtonState(); } class _HoldProgressTextButtonState extends State<HoldProgressTextButton> with SingleTickerProviderStateMixin { late AnimationController _progressController; @override void initState() { super.initState(); _progressController = AnimationController( vsync: this, duration: widget.requiredHoldDuration, ); _progressController.addStatusListener((status) { if (status == AnimationStatus.completed) { widget.onHoldComplete(); _progressController.reset(); } }); } @override void dispose() { _progressController.dispose(); super.dispose(); } @override Widget build(BuildContext context) { return GestureDetector( onTapDown: (_) => _progressController.forward(), onTapUp: (_) { if (_progressController.status != AnimationStatus.completed) { _progressController.reverse(); } }, onTapCancel: () { if (_progressController.status != AnimationStatus.completed) { _progressController.reverse(); } }, child: AnimatedBuilder( animation: _progressController, builder: (context, child) { return CustomPaint( painter: ProgressBorderPainter(progress: _progressController.value), child: TextButton( onPressed: () {}, // 逻辑由外层手势处理即可 child: Text(widget.text), ), ); }, ), ); } } // 自定义边框绘制类 class ProgressBorderPainter extends CustomPainter { final double progress; final Color borderColor; final double borderWidth; ProgressBorderPainter({ required this.progress, this.borderColor = Colors.blue, this.borderWidth = 2, }); @override void paint(Canvas canvas, Size size) { final paint = Paint() ..color = borderColor ..style = PaintingStyle.stroke ..strokeWidth = borderWidth; // 此处为圆角矩形边框进度,可按需修改为圆形样式 final rect = Rect.fromLTWH(0, 0, size.width, size.height); final rrect = RRect.fromRectAndRadius(rect, const Radius.circular(8)); final path = Path()..addRRect(rrect); final pathLength = path.computeMetrics().first.length; final currentPath = Path() ..addPath( path.computeMetrics().first.extractPath(0, pathLength * progress), Offset.zero, ); canvas.drawPath(currentPath, paint); } @override bool shouldRepaint(covariant ProgressBorderPainter oldDelegate) { return oldDelegate.progress != progress; } }
内容的提问来源于stack exchange,提问作者Elonas Marcauskas
相关产品推荐
相关产品推荐

