mirror of
https://github.com/lifegpc/eh_downloader_flutter.git
synced 2026-07-08 01:30:28 +08:00
update server settings page
This commit is contained in:
70
lib/components/number_field.dart
Normal file
70
lib/components/number_field.dart
Normal file
@@ -0,0 +1,70 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
||||
|
||||
class NumberFormField extends StatelessWidget {
|
||||
const NumberFormField(
|
||||
{Key? key,
|
||||
this.min,
|
||||
this.max,
|
||||
this.initialValue,
|
||||
this.errorMsg,
|
||||
this.decoration,
|
||||
this.onChanged})
|
||||
: super(key: key);
|
||||
|
||||
final int? min;
|
||||
final int? max;
|
||||
final int? initialValue;
|
||||
final String? errorMsg;
|
||||
final InputDecoration? decoration;
|
||||
final Function(int?)? onChanged;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final i18n = AppLocalizations.of(context)!;
|
||||
return TextFormField(
|
||||
initialValue: initialValue?.toString(),
|
||||
keyboardType: TextInputType.number,
|
||||
inputFormatters: [
|
||||
FilteringTextInputFormatter.allow(RegExp(r"[\-0-9]")),
|
||||
],
|
||||
autovalidateMode: AutovalidateMode.onUserInteraction,
|
||||
validator: (String? value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
int v = int.parse(value);
|
||||
if (min != null && v < min!) {
|
||||
return errorMsg ?? i18n.numberOutOfRange;
|
||||
}
|
||||
if (max != null && v > max!) {
|
||||
return errorMsg ?? i18n.numberOutOfRange;
|
||||
}
|
||||
} catch (e) {
|
||||
return errorMsg ?? i18n.invalidNumber;
|
||||
}
|
||||
return null;
|
||||
},
|
||||
decoration: decoration,
|
||||
onChanged: (String? value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
onChanged?.call(null);
|
||||
} else {
|
||||
try {
|
||||
int v = int.parse(value);
|
||||
if (max != null && v > max!) {
|
||||
onChanged?.call(null);
|
||||
} else if (min != null && v < min!) {
|
||||
onChanged?.call(null);
|
||||
} else {
|
||||
onChanged?.call(v);
|
||||
}
|
||||
} catch (e) {
|
||||
onChanged?.call(null);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user