136 lines
No EOL
4.3 KiB
Dart
136 lines
No EOL
4.3 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
class EventAdder extends StatefulWidget {
|
|
const EventAdder({super.key});
|
|
|
|
@override
|
|
State<EventAdder> createState() => _EventAdderState();
|
|
}
|
|
|
|
class _EventAdderState extends State<EventAdder> {
|
|
final _customEventNameController = TextEditingController();
|
|
final _customEventCodeController = TextEditingController();
|
|
List<String> customEventNames = [];
|
|
List<String> customEventCodes = [];
|
|
final _formKey = GlobalKey<FormState>();
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_loadEvents();
|
|
}
|
|
|
|
Future<void> _loadEvents() async {
|
|
final SharedPreferences prefs = await SharedPreferences.getInstance();
|
|
setState(() {
|
|
customEventNames = prefs.getStringList('custom_event_names') ?? [];
|
|
customEventCodes = prefs.getStringList('custom_event_codes') ?? [];
|
|
});
|
|
}
|
|
|
|
Future<void> _saveEvent() async {
|
|
if (!_formKey.currentState!.validate()) {
|
|
return;
|
|
}
|
|
|
|
final SharedPreferences prefs = await SharedPreferences.getInstance();
|
|
final String eventName = _customEventNameController.text;
|
|
String eventCode;
|
|
|
|
if (_customEventCodeController.text.isEmpty) {
|
|
eventCode = '${DateTime.now().year.toString()}${eventName.replaceAll(' ', '').substring(0,4)}';
|
|
} else {
|
|
eventCode = _customEventCodeController.text;
|
|
}
|
|
|
|
customEventNames.add(eventName);
|
|
customEventCodes.add(eventCode);
|
|
|
|
await prefs.setStringList('custom_event_names', customEventNames);
|
|
await prefs.setStringList('custom_event_codes', customEventCodes);
|
|
|
|
_customEventNameController.clear();
|
|
_customEventCodeController.clear();
|
|
setState(() {});
|
|
|
|
if (mounted) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(
|
|
content: Text('Added Event: $eventName - $eventCode'),
|
|
behavior: SnackBarBehavior.floating,
|
|
margin: const EdgeInsets.all(16.0),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(title: const Text('Add Event')),
|
|
body: Form(
|
|
key: _formKey,
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Padding(
|
|
padding: const EdgeInsets.only(top: 16.0),
|
|
child: Text('Add Custom Event', style: Theme.of(context).textTheme.titleLarge)
|
|
),
|
|
Padding(
|
|
padding: const EdgeInsets.only(top: 16.0),
|
|
child: ListView(
|
|
shrinkWrap: true,
|
|
padding: const EdgeInsets.symmetric(horizontal: 16.0),
|
|
children: [
|
|
const SizedBox(height: 16),
|
|
TextFormField(
|
|
controller: _customEventNameController,
|
|
decoration: const InputDecoration(
|
|
labelText: 'Event Name',
|
|
hintText: 'gm_construct Regional',
|
|
border: OutlineInputBorder()
|
|
),
|
|
validator: (value) {
|
|
if (value == null || value.isEmpty) {
|
|
return 'Please enter a event name';
|
|
}
|
|
return null;
|
|
},
|
|
),
|
|
const SizedBox(height: 16),
|
|
TextFormField(
|
|
controller: _customEventCodeController,
|
|
decoration: const InputDecoration(
|
|
labelText: 'Event Code',
|
|
hintText: '2026gmct',
|
|
border: OutlineInputBorder()
|
|
)
|
|
),
|
|
Text('Leave this field EMPTY unless you NEED a custom code.'),
|
|
const SizedBox(height: 16.0),
|
|
ElevatedButton(
|
|
onPressed: () { _saveEvent(); },
|
|
child: const Text('Create Event'),
|
|
),
|
|
],
|
|
)
|
|
),
|
|
Divider(),
|
|
Text('Custom Events', style: Theme.of(context).textTheme.titleLarge),
|
|
ListView.builder(
|
|
shrinkWrap: true,
|
|
itemCount: customEventNames.length,
|
|
itemBuilder: (context, index) {
|
|
return ListTile(
|
|
title: Text('${customEventNames[index]} - ${customEventCodes[index]}')
|
|
);
|
|
}
|
|
)
|
|
],
|
|
)
|
|
)
|
|
);
|
|
}
|
|
} |