mirror of
https://github.com/DefinedNet/mobile_nebula.git
synced 2025-03-06 16:46:35 +00:00
* Enable `flutter_lints` linting * Fix unmarked deps, we aren't on web so we don't need a URL strategy * ` dart fix --apply --code=use_super_parameters` * `dart fix --apply --code=use_key_in_widget_constructors` * `dart fix --apply --code=use_function_type_syntax_for_parameters` * Ignore code-generated `lib/services/theme.dart` file * `dart fix --apply --code=unnecessary_this` * `dart fix --apply --code=unnecessary_null_in_if_null_operators` * `dart fix --apply --code=unnecessary_new` * `dart fix --apply --code=sort_child_properties_last` * `dart fix --apply --code=sized_box_for_whitespace` * `dart fix --apply --code=prefer_typing_uninitialized_variables` * `dart fix --apply --code=prefer_is_empty` * `dart fix --apply --code=prefer_interpolation_to_compose_strings` * `dart fix --apply --code=prefer_final_fields` * `dart fix --apply --code=prefer_const_constructors_in_immutables` * `dart fix --apply --code=prefer_collection_literals` * `dart fix --apply --code=no_leading_underscores_for_local_identifiers` * `dart fix --apply --code=curly_braces_in_flow_control_structures` * `dart fix --apply --code=avoid_function_literals_in_foreach_calls` * `dart fix --apply --code=annotate_overrides` * Add CI for dart linting * `dart format lib/` * Re-enable the `usePathUrlStrategy` call, with proper deps https://docs.flutter.dev/ui/navigation/url-strategies#configuring-the-url-strategy
105 lines
3.7 KiB
Dart
105 lines
3.7 KiB
Dart
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/services.dart';
|
|
import 'package:mobile_nebula/components/SpecialTextField.dart';
|
|
|
|
import '../services/utils.dart';
|
|
|
|
class IPField extends StatelessWidget {
|
|
final String help;
|
|
final bool ipOnly;
|
|
final bool autoFocus;
|
|
final FocusNode? focusNode;
|
|
final FocusNode? nextFocusNode;
|
|
final ValueChanged<String>? onChanged;
|
|
final EdgeInsetsGeometry textPadding;
|
|
final TextInputAction? textInputAction;
|
|
final controller;
|
|
final textAlign;
|
|
|
|
const IPField({
|
|
super.key,
|
|
this.ipOnly = false,
|
|
this.help = "ip address",
|
|
this.autoFocus = false,
|
|
this.focusNode,
|
|
this.nextFocusNode,
|
|
this.onChanged,
|
|
this.textPadding = const EdgeInsets.all(6.0),
|
|
this.textInputAction,
|
|
this.controller,
|
|
this.textAlign = TextAlign.center,
|
|
});
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
var textStyle = CupertinoTheme.of(context).textTheme.textStyle;
|
|
final double? ipWidth = ipOnly ? Utils.textSize("000000000000000", textStyle).width + 12 : null;
|
|
|
|
return SizedBox(
|
|
width: ipWidth,
|
|
child: SpecialTextField(
|
|
keyboardType: ipOnly ? TextInputType.numberWithOptions(decimal: true, signed: true) : null,
|
|
textAlign: textAlign,
|
|
autofocus: autoFocus,
|
|
focusNode: focusNode,
|
|
nextFocusNode: nextFocusNode,
|
|
controller: controller,
|
|
onChanged: onChanged,
|
|
maxLength: ipOnly ? 15 : null,
|
|
maxLengthEnforcement: ipOnly ? MaxLengthEnforcement.enforced : MaxLengthEnforcement.none,
|
|
inputFormatters: ipOnly ? [IPTextInputFormatter()] : [FilteringTextInputFormatter.allow(RegExp(r'[^\s]+'))],
|
|
textInputAction: textInputAction,
|
|
placeholder: help,
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
class IPTextInputFormatter extends TextInputFormatter {
|
|
final Pattern whitelistedPattern = RegExp(r'[\d\.,]+');
|
|
|
|
@override
|
|
TextEditingValue formatEditUpdate(TextEditingValue oldValue, TextEditingValue newValue) {
|
|
return _selectionAwareTextManipulation(newValue, (String substring) {
|
|
return whitelistedPattern
|
|
.allMatches(substring)
|
|
.map<String>((Match match) => match.group(0)!)
|
|
.join()
|
|
.replaceAll(RegExp(r','), '.');
|
|
});
|
|
}
|
|
}
|
|
|
|
TextEditingValue _selectionAwareTextManipulation(
|
|
TextEditingValue value,
|
|
String Function(String substring) substringManipulation,
|
|
) {
|
|
final int selectionStartIndex = value.selection.start;
|
|
final int selectionEndIndex = value.selection.end;
|
|
String manipulatedText;
|
|
TextSelection? manipulatedSelection;
|
|
if (selectionStartIndex < 0 || selectionEndIndex < 0) {
|
|
manipulatedText = substringManipulation(value.text);
|
|
} else {
|
|
final String beforeSelection = substringManipulation(value.text.substring(0, selectionStartIndex));
|
|
final String inSelection = substringManipulation(value.text.substring(selectionStartIndex, selectionEndIndex));
|
|
final String afterSelection = substringManipulation(value.text.substring(selectionEndIndex));
|
|
manipulatedText = beforeSelection + inSelection + afterSelection;
|
|
if (value.selection.baseOffset > value.selection.extentOffset) {
|
|
manipulatedSelection = value.selection.copyWith(
|
|
baseOffset: beforeSelection.length + inSelection.length,
|
|
extentOffset: beforeSelection.length,
|
|
);
|
|
} else {
|
|
manipulatedSelection = value.selection.copyWith(
|
|
baseOffset: beforeSelection.length,
|
|
extentOffset: beforeSelection.length + inSelection.length,
|
|
);
|
|
}
|
|
}
|
|
return TextEditingValue(
|
|
text: manipulatedText,
|
|
selection: manipulatedSelection ?? const TextSelection.collapsed(offset: -1),
|
|
composing: manipulatedText == value.text ? value.composing : TextRange.empty,
|
|
);
|
|
}
|