260 lines
8.7 KiB
Dart
260 lines
8.7 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:bot_toast/bot_toast.dart';
|
|
import 'package:didvan/config/design_config.dart';
|
|
import 'package:didvan/config/theme_data.dart';
|
|
import 'package:didvan/models/enums.dart';
|
|
import 'package:didvan/models/view/action_sheet_data.dart';
|
|
import 'package:didvan/models/view/alert_data.dart';
|
|
import 'package:didvan/views/widgets/didvan/button.dart';
|
|
import 'package:didvan/views/widgets/didvan/text.dart';
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_spinkit/flutter_spinkit.dart';
|
|
|
|
class ActionSheetUtils {
|
|
static late BuildContext context;
|
|
|
|
static MediaQueryData get mediaQueryData {
|
|
final ds = MediaQuery.of(context).size;
|
|
double width = ds.width;
|
|
final shortestSide = ds.shortestSide;
|
|
final bool useMobileLayout = shortestSide < 600;
|
|
if (kIsWeb && !useMobileLayout) {
|
|
width = ds.height * 9 / 16;
|
|
}
|
|
return MediaQuery.of(context).copyWith(size: Size(width, ds.height));
|
|
}
|
|
|
|
static Future<void> showLogoLoadingIndicator() async {
|
|
await showDialog(
|
|
barrierDismissible: false,
|
|
context: context,
|
|
builder: (context) => SpinKitChasingDots(
|
|
color: DesignConfig.isDark
|
|
? Theme.of(context).colorScheme.primary
|
|
: Theme.of(context).colorScheme.navigation,
|
|
),
|
|
);
|
|
}
|
|
|
|
static Future<void> showAlert(AlertData alertData) async {
|
|
bool isInit = true;
|
|
Color backgroundColor;
|
|
Color foregroundColor;
|
|
switch (alertData.aLertType) {
|
|
case ALertType.info:
|
|
backgroundColor = Theme.of(context).colorScheme.focused;
|
|
foregroundColor = Theme.of(context).colorScheme.focusedBorder;
|
|
break;
|
|
case ALertType.success:
|
|
backgroundColor = Theme.of(context).colorScheme.successBack;
|
|
foregroundColor = Theme.of(context).colorScheme.success;
|
|
break;
|
|
case ALertType.error:
|
|
backgroundColor = Theme.of(context).colorScheme.errorBack;
|
|
foregroundColor = Theme.of(context).colorScheme.error;
|
|
break;
|
|
}
|
|
BotToast.showNotification(
|
|
backgroundColor: backgroundColor,
|
|
title: (cancelFunc) => StatefulBuilder(
|
|
builder: (context, setState) {
|
|
if (isInit) {
|
|
Future.delayed(Duration.zero, () {
|
|
setState(() {});
|
|
isInit = false;
|
|
});
|
|
}
|
|
return Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
DidvanText(alertData.message, color: foregroundColor),
|
|
AnimatedContainer(
|
|
duration: const Duration(seconds: 2),
|
|
width: isInit ? MediaQuery.of(context).size.width - 32 : 0,
|
|
height: 2,
|
|
color: foregroundColor,
|
|
),
|
|
],
|
|
);
|
|
},
|
|
),
|
|
);
|
|
}
|
|
|
|
static Future<void> showBottomSheet({required ActionSheetData data}) async {
|
|
await showModalBottomSheet(
|
|
constraints: BoxConstraints(
|
|
maxWidth: mediaQueryData.size.width,
|
|
),
|
|
backgroundColor: Colors.transparent,
|
|
isScrollControlled: true,
|
|
context: context,
|
|
builder: (context) => Container(
|
|
padding: data.hasPadding
|
|
? const EdgeInsets.all(20).copyWith(top: 0)
|
|
: EdgeInsets.zero,
|
|
decoration: BoxDecoration(
|
|
color: Theme.of(context).colorScheme.surface,
|
|
borderRadius: const BorderRadius.vertical(
|
|
top: Radius.circular(10),
|
|
),
|
|
),
|
|
child: SingleChildScrollView(
|
|
child: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
const SizedBox(height: 20),
|
|
Center(
|
|
child: Container(
|
|
height: 3,
|
|
width: 50,
|
|
color: Theme.of(context).colorScheme.hint,
|
|
),
|
|
),
|
|
const SizedBox(height: 8),
|
|
if (data.title != null)
|
|
Row(
|
|
children: [
|
|
if (data.titleIcon != null)
|
|
Icon(
|
|
data.titleIcon,
|
|
color: data.titleColor ??
|
|
Theme.of(context).colorScheme.title,
|
|
),
|
|
if (data.titleIcon != null) const SizedBox(width: 8),
|
|
DidvanText(
|
|
data.title!,
|
|
style: Theme.of(context).textTheme.subtitle1,
|
|
color: data.titleColor ??
|
|
Theme.of(context).colorScheme.title,
|
|
)
|
|
],
|
|
),
|
|
const SizedBox(height: 28),
|
|
data.content,
|
|
const SizedBox(height: 28),
|
|
if (!data.withoutButtonMode)
|
|
Row(
|
|
children: [
|
|
if (data.hasDismissButton)
|
|
Expanded(
|
|
child: DidvanButton(
|
|
onPressed: () {
|
|
Navigator.of(context).pop();
|
|
data.onDismissed?.call();
|
|
},
|
|
title: data.dismissTitle ?? 'بازگشت',
|
|
style: ButtonStyleMode.secondary,
|
|
),
|
|
),
|
|
if (data.hasDismissButton) const SizedBox(width: 20),
|
|
if (data.hasConfirmButton)
|
|
Expanded(
|
|
flex: data.smallDismissButton ? 2 : 1,
|
|
child: DidvanButton(
|
|
style: ButtonStyleMode.primary,
|
|
onPressed: () {
|
|
Navigator.of(context).pop();
|
|
data.onConfirmed?.call();
|
|
},
|
|
title: data.confrimTitle ?? 'تایید',
|
|
),
|
|
),
|
|
],
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
static Future<void> openDialog({required ActionSheetData data}) async {
|
|
await showDialog(
|
|
context: context,
|
|
builder: (context) => Dialog(
|
|
backgroundColor: Theme.of(context).colorScheme.surface,
|
|
shape: const RoundedRectangleBorder(
|
|
borderRadius: DesignConfig.mediumBorderRadius,
|
|
),
|
|
child: Container(
|
|
width: mediaQueryData.size.width * 0.8,
|
|
padding: const EdgeInsets.all(24.0),
|
|
child: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
if (data.title != null)
|
|
Row(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
GestureDetector(
|
|
onTap: () => Navigator.of(context).pop(),
|
|
child: Icon(
|
|
data.titleIcon,
|
|
size: 20,
|
|
color: data.titleColor,
|
|
),
|
|
),
|
|
const SizedBox(
|
|
width: 8,
|
|
),
|
|
Expanded(
|
|
child: DidvanText(
|
|
data.title!,
|
|
style: Theme.of(context).textTheme.headline3,
|
|
color: data.titleColor,
|
|
fontWeight: FontWeight.bold,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
const SizedBox(
|
|
height: 12,
|
|
),
|
|
data.content,
|
|
const SizedBox(
|
|
height: 12,
|
|
),
|
|
Row(
|
|
children: [
|
|
if (data.hasDismissButton)
|
|
Expanded(
|
|
child: DidvanButton(
|
|
onPressed: data.onDismissed ?? () => pop(),
|
|
title: data.dismissTitle ?? 'بازگشت',
|
|
style: ButtonStyleMode.flat,
|
|
),
|
|
),
|
|
if (data.hasDismissButton)
|
|
const SizedBox(
|
|
width: 20,
|
|
),
|
|
Expanded(
|
|
child: DidvanButton(
|
|
onPressed: () {
|
|
pop();
|
|
data.onConfirmed?.call();
|
|
},
|
|
title: data.confrimTitle ?? 'تایید',
|
|
),
|
|
),
|
|
],
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
static void pop() {
|
|
DesignConfig.updateSystemUiOverlayStyle();
|
|
Navigator.of(context).pop();
|
|
}
|
|
}
|