mirror of
https://github.com/krawieck/lemmur/
synced 2024-12-12 16:46:16 +01:00
45 lines
1.1 KiB
Dart
45 lines
1.1 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_hooks/flutter_hooks.dart';
|
|
|
|
import 'ref.dart';
|
|
|
|
class DelayedLoading {
|
|
final bool pending;
|
|
final bool loading;
|
|
final void Function() start;
|
|
final void Function() cancel;
|
|
|
|
const DelayedLoading({
|
|
@required this.pending,
|
|
@required this.loading,
|
|
@required this.start,
|
|
@required this.cancel,
|
|
});
|
|
}
|
|
|
|
/// When loading is [.start()]ed, it goes into a pending state
|
|
/// and loading is triggered after [delayDuration].
|
|
/// Everything can be reset with [.cancel()]
|
|
DelayedLoading useDelayedLoading(
|
|
[Duration delayDuration = const Duration(milliseconds: 500)]) {
|
|
final loading = useState(false);
|
|
final pending = useState(false);
|
|
final timerHandle = useRef<Timer>(null);
|
|
|
|
return DelayedLoading(
|
|
loading: loading.value,
|
|
pending: pending.value,
|
|
start: () {
|
|
timerHandle.current = Timer(delayDuration, () => loading.value = true);
|
|
pending.value = true;
|
|
},
|
|
cancel: () {
|
|
timerHandle.current?.cancel();
|
|
pending.value = false;
|
|
loading.value = false;
|
|
},
|
|
);
|
|
}
|