tuuli_app/lib/widgets/data_input_dialog.dart

126 lines
3.7 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter_fast_forms/flutter_fast_forms.dart';
import 'package:get/get.dart';
Future<String?> showStringInputDialog({String? originalValue}) async {
final strVal = (originalValue ?? "").obs;
return await Get.dialog<String>(
AlertDialog(
title: const Text("Введите строку"),
content: TextField(
controller: TextEditingController(text: originalValue),
onChanged: (value) {
strVal.value = value;
},
),
actions: [
TextButton(
onPressed: () {
Get.back(result: null);
},
child: const Text("Отменить"),
),
TextButton(
onPressed: () {
Get.back(result: strVal.value);
},
child: const Text("Ок"),
),
],
),
);
}
Future<double?> showDoubleInputDialog({double? originalValue}) async {
final strVal = (originalValue?.toString() ?? "").obs;
return await Get.dialog<double>(
AlertDialog(
title: const Text("Введите плавающее число"),
content: FastTextField(
name: "Number",
initialValue: originalValue?.toString(),
keyboardType: TextInputType.number,
autovalidateMode: AutovalidateMode.onUserInteraction,
validator: (value) {
if (value == null || value.isEmpty) {
return "Пожалуйста, введите плавающее число";
}
final parsed = double.tryParse(value);
if (parsed == null) {
return "Пожалуйста, введите настоящее плавающее число";
}
return null;
},
onChanged: (value) {
strVal.value = value ?? "";
},
),
actions: [
TextButton(
onPressed: () {
Get.back(result: null);
},
child: const Text("Отменить"),
),
TextButton(
onPressed: () {
final d = double.tryParse(strVal.value);
if (d != null) {
Get.back(result: d);
} else {
Get.back(result: null);
}
},
child: const Text("Ок"),
),
],
),
);
}
Future<int?> showIntInputDialog({int? originalValue}) async {
final strVal = (originalValue?.toString() ?? "").obs;
return await Get.dialog<int>(
AlertDialog(
title: const Text("Введите целое число"),
content: FastTextField(
name: "Number",
initialValue: originalValue?.toString(),
keyboardType: TextInputType.number,
autovalidateMode: AutovalidateMode.onUserInteraction,
validator: (value) {
if (value == null || value.isEmpty) {
return "Пожалуйста, введите целое число";
}
final parsed = int.tryParse(value);
if (parsed == null) {
return "Пожалуйста, введите настоящее целое число";
}
return null;
},
onChanged: (value) {
strVal.value = value ?? "";
},
),
actions: [
TextButton(
onPressed: () {
Get.back(result: null);
},
child: const Text("Отменить"),
),
TextButton(
onPressed: () {
final i = int.tryParse(strVal.value);
if (i != null) {
Get.back(result: i);
} else {
Get.back(result: null);
}
},
child: const Text("Ок"),
),
],
),
);
}