main
  1import 'package:blood_pressure_app/components/fullscreen_dialoge.dart';
  2import 'package:blood_pressure_app/features/export_import/export_field_format_documentation_screen.dart';
  3import 'package:flutter/material.dart';
  4import 'package:blood_pressure_app/l10n/app_localizations.dart';
  5import 'package:flutter_markdown/flutter_markdown.dart';
  6import 'package:intl/intl.dart';
  7
  8/// Fullscreen dialoge that explains the time format and pops the context with either null or a time format string.
  9class EnterTimeFormatDialoge extends StatefulWidget {
 10  /// Create dialoge for entering time formats as used by the [DateFormat] class.
 11  const EnterTimeFormatDialoge({super.key,
 12    required this.initialValue,
 13    this.previewTime,
 14    this.bottomAppBars = false,
 15  });
 16
 17  /// Text that is initially in time format field.
 18  final String initialValue;
 19
 20  /// Timestamp used to generate time format preview.
 21  ///
 22  /// When previewTime is null [DateTime.now] will be used.
 23  final DateTime? previewTime;
 24
 25  /// Whether to move the app bar for saving and loading to the bottom of the
 26  /// screen.
 27  final bool bottomAppBars;
 28
 29  @override
 30  State<EnterTimeFormatDialoge> createState() => _EnterTimeFormatDialogeState();
 31}
 32
 33class _EnterTimeFormatDialogeState extends State<EnterTimeFormatDialoge> {
 34  final timeFormatFieldController = TextEditingController();
 35  final focusNode = FocusNode();
 36
 37  @override
 38  void initState() {
 39    super.initState();
 40    timeFormatFieldController.text = widget.initialValue;
 41    timeFormatFieldController.addListener(() => setState(() {}));
 42    focusNode.requestFocus();
 43  }
 44
 45  @override
 46  void dispose() {
 47    timeFormatFieldController.dispose();
 48    focusNode.dispose();
 49    super.dispose();
 50  }
 51
 52  @override
 53  Widget build(BuildContext context) {
 54    final localizations = AppLocalizations.of(context)!;
 55    return FullscreenDialoge(
 56      actionButtonText: localizations.btnSave,
 57      bottomAppBar: widget.bottomAppBars,
 58      onActionButtonPressed: () {
 59        if(timeFormatFieldController.text.isNotEmpty) {
 60          Navigator.pop(context, timeFormatFieldController.text);
 61        }
 62      },
 63      body: SingleChildScrollView(
 64        child: Padding(
 65          padding: const EdgeInsets.all(24),
 66          child: Column(
 67            children: [
 68              Markdown(
 69                shrinkWrap: true,
 70                onTapLink: getLinkTapHandler(context),
 71                physics: const NeverScrollableScrollPhysics(),
 72                data: localizations.enterTimeFormatDesc,
 73              ),
 74              Text(DateFormat(timeFormatFieldController.text).format(widget.previewTime ?? DateTime.now())),
 75              Padding(
 76                padding: const EdgeInsets.all(12),
 77                child: TextField(
 78                  controller: timeFormatFieldController,
 79                  focusNode: focusNode,
 80                  decoration: InputDecoration(
 81                    border: const OutlineInputBorder(),
 82                    labelText: localizations.enterTimeFormatString,
 83                    errorText: timeFormatFieldController.text.isEmpty ? localizations.errNoValue : null,
 84                  ),
 85                ),
 86              ),
 87            ],
 88          ),
 89        ),
 90      ),
 91    );
 92  }
 93}
 94
 95/// Shows a dialoge that explains the ICU DateTime format and allows editing [initialTimeFormat] with a preview.
 96///
 97/// When canceled null is returned.
 98Future<String?> showTimeFormatPickerDialoge(BuildContext context, String initialTimeFormat, bool bottomAppBars) =>
 99  showDialog<String?>(context: context, builder: (context) => EnterTimeFormatDialoge(
100    initialValue: initialTimeFormat,
101    bottomAppBars: bottomAppBars,),
102  );