Line data Source code
1 : part of apptive_grid_form_widgets; 2 : 3 : /// FormComponent Widget to display a [DecimalFormComponent] 4 : class DecimalFormWidget extends StatefulWidget { 5 : /// Creates a [TextFormField] to show and edit an integer contained in [component] 6 3 : const DecimalFormWidget({ 7 : Key? key, 8 : required this.component, 9 3 : }) : super(key: key); 10 : 11 : /// Component this Widget should reflect 12 : final DecimalFormComponent component; 13 : 14 2 : @override 15 2 : _DecimalFormWidgetState createState() => _DecimalFormWidgetState(); 16 : } 17 : 18 : class _DecimalFormWidgetState extends State<DecimalFormWidget> { 19 : final TextEditingController _controller = TextEditingController(); 20 : 21 2 : @override 22 : void initState() { 23 2 : super.initState(); 24 8 : if (widget.component.data.value != null) { 25 7 : _controller.text = widget.component.data.value!.toString(); 26 : } 27 6 : _controller.addListener(() { 28 6 : if (_controller.text.isNotEmpty) { 29 8 : if (double.tryParse(_controller.text.replaceAll(',', '.')) != null) { 30 8 : widget.component.data.value = 31 8 : double.parse(_controller.text.replaceAll(',', '.')); 32 : } else { 33 4 : _controller.text = _controller.text 34 5 : .substring(0, max(0, _controller.text.length - 1)); 35 3 : _controller.selection = TextSelection.fromPosition( 36 4 : TextPosition(offset: _controller.text.length), 37 : ); 38 : } 39 : } 40 : }); 41 : } 42 : 43 2 : @override 44 : void dispose() { 45 4 : _controller.dispose(); 46 2 : super.dispose(); 47 : } 48 : 49 2 : @override 50 : Widget build( 51 : BuildContext context, 52 : ) { 53 2 : return TextFormField( 54 2 : controller: _controller, 55 2 : validator: (input) { 56 7 : if (widget.component.required && (input == null || input.isEmpty)) { 57 1 : return ApptiveGridLocalization.of(context)! 58 4 : .fieldIsRequired(widget.component.property); 59 : } else { 60 : return null; 61 : } 62 : }, 63 : autovalidateMode: AutovalidateMode.onUserInteraction, 64 8 : expands: widget.component.options.multi, 65 6 : inputFormatters: [FilteringTextInputFormatter.allow(RegExp('[0-9.,]+'))], 66 : keyboardType: 67 : const TextInputType.numberWithOptions(signed: true, decimal: true), 68 2 : decoration: InputDecoration( 69 8 : helperText: widget.component.options.description, 70 : helperMaxLines: 100, 71 14 : labelText: widget.component.options.label ?? widget.component.property, 72 8 : hintText: widget.component.options.placeholder, 73 : ), 74 : ); 75 : } 76 : }