2024-06-24 20:14:22 +08:00
|
|
|
import 'dart:async';
|
|
|
|
|
2023-05-16 14:58:24 +08:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
|
|
|
class Loading {
|
|
|
|
Loading(this.context);
|
|
|
|
|
2024-04-07 23:06:33 +08:00
|
|
|
BuildContext? loadingContext;
|
2023-05-16 14:58:24 +08:00
|
|
|
final BuildContext context;
|
|
|
|
|
2024-08-19 11:06:34 +08:00
|
|
|
bool hasStopped = false;
|
|
|
|
|
2024-06-24 20:14:22 +08:00
|
|
|
void start() => unawaited(
|
|
|
|
showDialog<void>(
|
|
|
|
context: context,
|
|
|
|
barrierDismissible: false,
|
|
|
|
builder: (BuildContext context) {
|
|
|
|
loadingContext = context;
|
2024-08-19 11:06:34 +08:00
|
|
|
|
|
|
|
if (hasStopped) {
|
|
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
2024-09-09 09:28:29 +08:00
|
|
|
Navigator.of(loadingContext!).maybePop();
|
2024-08-19 11:06:34 +08:00
|
|
|
loadingContext = null;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-06-24 20:14:22 +08:00
|
|
|
return const SimpleDialog(
|
|
|
|
elevation: 0.0,
|
|
|
|
backgroundColor:
|
|
|
|
Colors.transparent, // can change this to your preferred color
|
|
|
|
children: [
|
|
|
|
Center(
|
|
|
|
child: CircularProgressIndicator(),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
2023-05-16 14:58:24 +08:00
|
|
|
);
|
|
|
|
|
2024-06-25 01:59:38 +02:00
|
|
|
void stop() {
|
2024-04-07 23:06:33 +08:00
|
|
|
if (loadingContext != null) {
|
|
|
|
Navigator.of(loadingContext!).pop();
|
|
|
|
loadingContext = null;
|
|
|
|
}
|
2024-08-19 11:06:34 +08:00
|
|
|
|
|
|
|
hasStopped = true;
|
2024-04-07 23:06:33 +08:00
|
|
|
}
|
2023-05-16 14:58:24 +08:00
|
|
|
}
|