/// Dialog for adding or editing a supermarket name and color. library; import 'package:flutter/material.dart'; import '../models/supermarket.dart'; Future showStoreEditDialog( BuildContext context, { Supermarket? store, }) { return showDialog( context: context, builder: (context) => _StoreEditDialog(store: store), ); } class _StoreEditDialog extends StatefulWidget { const _StoreEditDialog({this.store}); final Supermarket? store; @override State<_StoreEditDialog> createState() => _StoreEditDialogState(); } class _StoreEditDialogState extends State<_StoreEditDialog> { late final TextEditingController _name; late int _color; @override void initState() { super.initState(); _name = TextEditingController(text: widget.store?.name ?? ''); _color = widget.store?.colorValue ?? kStoreColorPalette.first; } @override void dispose() { _name.dispose(); super.dispose(); } @override Widget build(BuildContext context) { return AlertDialog( title: Text( widget.store == null ? 'Add supermarket' : 'Edit supermarket', ), content: Column( mainAxisSize: MainAxisSize.min, children: [ TextField( controller: _name, textCapitalization: TextCapitalization.words, decoration: const InputDecoration(labelText: 'Name'), autofocus: widget.store == null, ), const SizedBox(height: 16), Align( alignment: Alignment.centerLeft, child: Text( 'Color', style: Theme.of(context).textTheme.titleMedium, ), ), const SizedBox(height: 8), Wrap( spacing: 8, runSpacing: 8, children: [ for (final value in kStoreColorPalette) GestureDetector( onTap: () => setState(() => _color = value), child: Container( width: 32, height: 32, decoration: BoxDecoration( color: Color(value), shape: BoxShape.circle, border: Border.all( color: _color == value ? Theme.of(context).colorScheme.onSurface : Colors.transparent, width: 2, ), ), ), ), ], ), ], ), actions: [ TextButton( onPressed: () => Navigator.pop(context), child: const Text('Cancel'), ), FilledButton( onPressed: () { final name = _name.text.trim(); if (name.isEmpty) return; Navigator.pop( context, Supermarket( id: widget.store?.id, name: name, colorValue: _color, sortOrder: widget.store?.sortOrder ?? 0, bonusImagePath: widget.store?.bonusImagePath, ), ); }, child: const Text('Save'), ), ], ); } }