LCOV - code coverage report
Current view: top level - lib/widgets/form_widget - date_form_widget.dart (source / functions) Hit Total Coverage
Test: lcov.info Lines: 34 34 100.0 %
Date: 2021-11-04 14:59:29 Functions: 0 0 -

          Line data    Source code
       1             : part of apptive_grid_form_widgets;
       2             : 
       3             : /// FormComponent Widget to display a [DateFormComponent]
       4             : class DateFormWidget extends StatefulWidget {
       5             :   /// Creates a Widget to display and select a Date contained in [component]
       6             :   ///
       7             :   /// Clicking on this will show a DatePicker using [showDatePicker]
       8           3 :   const DateFormWidget({
       9             :     Key? key,
      10             :     required this.component,
      11           3 :   }) : super(key: key);
      12             : 
      13             :   /// Component this Widget should reflect
      14             :   final DateFormComponent component;
      15             : 
      16           2 :   @override
      17           2 :   State<StatefulWidget> createState() => _DateFormWidgetState();
      18             : }
      19             : 
      20             : class _DateFormWidgetState extends State<DateFormWidget> {
      21             :   final TextEditingController _controller = TextEditingController();
      22             :   final GlobalKey<FormFieldState> _formKey = GlobalKey<FormFieldState>();
      23             : 
      24           2 :   @override
      25             :   Widget build(
      26             :     BuildContext context,
      27             :   ) {
      28           2 :     final dateFormat = DateFormat.yMd();
      29           8 :     if (widget.component.data.value != null) {
      30           5 :       final dateString = dateFormat.format(widget.component.data.value!);
      31           2 :       _controller.text = dateString;
      32             :     }
      33           2 :     return InkWell(
      34           1 :       onTap: () {
      35           5 :         final initialDate = widget.component.data.value ?? DateTime.now();
      36           1 :         showDatePicker(
      37             :           context: context,
      38             :           initialDate: initialDate,
      39           1 :           firstDate: DateTime.fromMillisecondsSinceEpoch(0),
      40           1 :           lastDate: DateTime.fromMillisecondsSinceEpoch(
      41           1 :             const Duration(days: 100000000).inMilliseconds,
      42             :           ),
      43           2 :         ).then((value) {
      44             :           if (value != null) {
      45           4 :             _formKey.currentState!.didChange(dateFormat.format(value));
      46           2 :             setState(() {
      47           4 :               widget.component.data.value = value;
      48             :             });
      49             :           }
      50             :         });
      51             :       },
      52           2 :       child: AbsorbPointer(
      53           2 :         child: TextFormField(
      54           2 :           key: _formKey,
      55           2 :           controller: _controller,
      56           1 :           validator: (input) {
      57           4 :             if (widget.component.required && (input == null || input.isEmpty)) {
      58           1 :               return ApptiveGridLocalization.of(context)!
      59           4 :                   .fieldIsRequired(widget.component.property);
      60             :             } else {
      61             :               return null;
      62             :             }
      63             :           },
      64             :           autovalidateMode: AutovalidateMode.onUserInteraction,
      65           2 :           decoration: InputDecoration(
      66           8 :             helperText: widget.component.options.description,
      67             :             helperMaxLines: 100,
      68             :             labelText:
      69          14 :                 widget.component.options.label ?? widget.component.property,
      70             :           ),
      71             :         ),
      72             :       ),
      73             :     );
      74             :   }
      75             : 
      76           2 :   @override
      77             :   void dispose() {
      78           4 :     _controller.dispose();
      79           2 :     super.dispose();
      80             :   }
      81             : }

Generated by: LCOV version 1.15