groceries_manager/lib/utils/get_interface_extension.dart
2025-01-05 16:01:21 +07:00

84 lines
2.3 KiB
Dart
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import 'package:flutter/material.dart';
import 'package:get/get.dart';
extension GetInterfaceExtension on GetInterface {
Future<bool> confirm({
required String title,
required String content,
String yes = "Да",
String no = "Нет",
}) async {
final result = await Get.defaultDialog(
title: title,
content: Text(content),
textConfirm: yes,
textCancel: no,
onConfirm: () => Get.backLegacy(result: true),
onCancel: () => Get.backLegacy(result: false),
);
return result == true;
}
Future<String?> prompt({
required String title,
String message = "",
String hintText = "",
String label = "",
String defaultValue = "",
String positiveLabel = "ОК",
String negativeLabel = "Отмена",
int? maxLength,
int maxLines = 1,
Widget? prefix,
required String? Function(String? x) stringValidator,
}) async {
final controller = TextEditingController(text: defaultValue);
final value = Get.dialog<String>(
AlertDialog(
title: Text(title),
content: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
if (message.isNotEmpty)
Padding(
padding: const EdgeInsets.only(bottom: 8),
child: Text(message),
),
TextFormField(
controller: controller,
decoration: InputDecoration(
hintText: hintText,
filled: true,
prefix: prefix,
label: Text(label),
),
maxLength: maxLength,
maxLines: maxLines,
autovalidateMode: AutovalidateMode.onUserInteraction,
validator: stringValidator,
)
],
),
actions: [
ElevatedButton(
onPressed: () {
if (stringValidator(controller.value.text) == null) {
Get.backLegacy(result: controller.value.text);
}
},
child: Text(positiveLabel),
),
ElevatedButton(
onPressed: () => Get.backLegacy(result: null),
child: Text(negativeLabel),
),
],
),
barrierDismissible: false,
useSafeArea: true,
);
return value;
}
}