fluffychat/lib/widgets/permission_slider_dialog.dart

81 lines
2.1 KiB
Dart
Raw Permalink Normal View History

2020-12-05 12:03:57 +00:00
import 'package:flutter/cupertino.dart';
2021-10-26 16:50:34 +00:00
import 'package:flutter/material.dart';
2022-07-30 06:36:17 +00:00
import 'package:adaptive_dialog/adaptive_dialog.dart';
2020-12-05 12:03:57 +00:00
import 'package:flutter_gen/gen_l10n/l10n.dart';
2022-07-30 06:36:17 +00:00
enum PermissionLevel {
user,
moderator,
admin,
custom,
2020-12-05 12:03:57 +00:00
}
2022-07-30 06:36:17 +00:00
extension on PermissionLevel {
String toLocalizedString(BuildContext context) {
switch (this) {
case PermissionLevel.user:
return L10n.of(context)!.user;
case PermissionLevel.moderator:
return L10n.of(context)!.moderator;
case PermissionLevel.admin:
return L10n.of(context)!.admin;
case PermissionLevel.custom:
default:
return L10n.of(context)!.custom;
}
2020-12-05 12:03:57 +00:00
}
2022-07-30 06:36:17 +00:00
}
2020-12-05 12:03:57 +00:00
Future<int?> showPermissionChooser(
BuildContext context, {
int currentLevel = 0,
}) async {
2023-01-08 11:32:35 +00:00
final permissionLevel = await showConfirmationDialog(
2022-07-30 06:36:17 +00:00
context: context,
title: L10n.of(context)!.setPermissionsLevel,
actions: PermissionLevel.values
.map(
2023-01-08 11:32:35 +00:00
(level) => AlertDialogAction(
2022-07-30 06:36:17 +00:00
key: level,
label: level.toLocalizedString(context),
),
2022-07-30 06:36:17 +00:00
)
.toList(),
);
if (permissionLevel == null) return null;
switch (permissionLevel) {
case PermissionLevel.user:
return 0;
case PermissionLevel.moderator:
return 50;
case PermissionLevel.admin:
return 100;
case PermissionLevel.custom:
final customLevel = await showTextInputDialog(
context: context,
title: L10n.of(context)!.setPermissionsLevel,
textFields: [
DialogTextField(
initialText: currentLevel.toString(),
keyboardType: TextInputType.number,
autocorrect: false,
validator: (text) {
if (text == null) {
return L10n.of(context)!.pleaseEnterANumber;
}
final level = int.tryParse(text);
if (level == null || level < 0) {
return L10n.of(context)!.pleaseEnterANumber;
}
return null;
},
2023-08-18 05:24:31 +00:00
),
2022-07-30 06:36:17 +00:00
],
2020-12-05 12:03:57 +00:00
);
2022-07-30 06:36:17 +00:00
if (customLevel == null) return null;
return int.tryParse(customLevel.first);
2020-12-05 12:03:57 +00:00
}
}