opt.: tag switcher
This commit is contained in:
32
lib/core/extension/widget.dart
Normal file
32
lib/core/extension/widget.dart
Normal file
@@ -0,0 +1,32 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:toolbox/core/utils/function.dart';
|
||||
|
||||
extension WidgetX on Widget {
|
||||
Widget padding(EdgeInsetsGeometry padding) {
|
||||
return Padding(padding: padding, child: this);
|
||||
}
|
||||
|
||||
Widget expanded({int flex = 1}) {
|
||||
return Expanded(flex: flex, child: this);
|
||||
}
|
||||
|
||||
Widget center() {
|
||||
return Center(child: this);
|
||||
}
|
||||
|
||||
Widget tap({
|
||||
VoidCallback? onTap,
|
||||
bool disable = false,
|
||||
VoidCallback? onLongTap,
|
||||
VoidCallback? onDoubleTap,
|
||||
}) {
|
||||
if (disable) return this;
|
||||
|
||||
return InkWell(
|
||||
onTap: () => Funcs.throttle(onTap),
|
||||
onLongPress: onLongTap,
|
||||
onDoubleTap: onDoubleTap,
|
||||
child: this,
|
||||
);
|
||||
}
|
||||
}
|
||||
24
lib/core/utils/function.dart
Normal file
24
lib/core/utils/function.dart
Normal file
@@ -0,0 +1,24 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
abstract final class Funcs {
|
||||
static const int _defaultDurationTime = 377;
|
||||
static const String _defaultThrottleId = 'default';
|
||||
static final Map<String, int> startTimeMap = <String, int>{
|
||||
_defaultThrottleId: 0
|
||||
};
|
||||
|
||||
static void throttle(
|
||||
VoidCallback? func, {
|
||||
String id = _defaultThrottleId,
|
||||
int duration = _defaultDurationTime,
|
||||
Function? continueClick,
|
||||
}) {
|
||||
final currentTime = DateTime.now().millisecondsSinceEpoch;
|
||||
if (currentTime - (startTimeMap[id] ?? 0) > duration) {
|
||||
func?.call();
|
||||
startTimeMap[id] = DateTime.now().millisecondsSinceEpoch;
|
||||
} else {
|
||||
continueClick?.call();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user