dvStudioAskComponentName function

Future<String?> dvStudioAskComponentName(
  1. BuildContext context
)

Asks for a name for a new component; null when somebody thinks better of it.

Implementation

Future<String?> dvStudioAskComponentName(BuildContext context) {
  String name = '';
  final RegExp valid = RegExp(r'^[A-Z][A-Za-z0-9]*$');
  return showGeneralDialog<String>(
    context: context,
    barrierDismissible: true,
    barrierLabel: 'Cancel',
    barrierColor: const Color(0x33000000),
    pageBuilder: (BuildContext dialog, _, _) => Center(
      child: Material(
        color: DVStudioStyle.surface,
        borderRadius: .circular(DVStudioStyle.radiusLarge),
        elevation: 8,
        child: SizedBox(
          width: 380,
          child: StatefulBuilder(
            builder: (BuildContext context, StateSetter set) {
              final bool ok = valid.hasMatch(name.trim());
              return Padding(
                padding: const .all(DVStudioStyle.space5),
                child: Column(
                  mainAxisSize: .min,
                  crossAxisAlignment: .stretch,
                  children: <Widget>[
                    DVStudioStyle.heading('Make a component'),
                    const SizedBox(height: 4),
                    DVStudioStyle.caption(
                        'The selection becomes a part you can put on any page. '
                        'Name it with one word, like PriceCard.'),
                    const SizedBox(height: DVStudioStyle.space3),
                    KeyedSubtree(
                      key: const ValueKey<String>('dv-studio-make-component-name'),
                      child: DVStudioTextInput(
                        value: '',
                        placeholder: 'PriceCard',
                        onChanged: (String v) => set(() => name = v),
                        onSubmitted: (_) {
                          if (ok) Navigator.of(dialog).pop(name.trim());
                        },
                      ),
                    ),
                    const SizedBox(height: DVStudioStyle.space3),
                    Align(
                      alignment: .centerRight,
                      child: _control(
                        'dv-studio-make-component-confirm',
                        'Make component',
                        ok ? () => Navigator.of(dialog).pop(name.trim()) : null,
                        primary: true,
                      ),
                    ),
                  ],
                ),
              );
            },
          ),
        ),
      ),
    ),
  );
}