init
This commit is contained in:
@@ -0,0 +1,119 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
import '../labels.dart';
|
||||
import '../models/models.dart';
|
||||
import '../services/auth_controller.dart';
|
||||
import '../ui/feedback.dart';
|
||||
import 'event_form_screen.dart';
|
||||
|
||||
class EventsScreen extends StatefulWidget {
|
||||
const EventsScreen({super.key});
|
||||
|
||||
@override
|
||||
State<EventsScreen> createState() => _EventsScreenState();
|
||||
}
|
||||
|
||||
class _EventsScreenState extends State<EventsScreen> {
|
||||
List<EventPlan>? _events;
|
||||
String? _planType;
|
||||
bool _loading = true;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_load();
|
||||
}
|
||||
|
||||
Future<void> _load() async {
|
||||
try {
|
||||
final events = await context.read<AuthController>().api.events(planType: _planType);
|
||||
if (!mounted) return;
|
||||
setState(() {
|
||||
_events = events;
|
||||
_loading = false;
|
||||
});
|
||||
} catch (error) {
|
||||
if (!mounted) return;
|
||||
setState(() => _loading = false);
|
||||
showError(context, error);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final events = _events ?? [];
|
||||
if (_loading && _events == null) {
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
}
|
||||
|
||||
return Scaffold(
|
||||
floatingActionButton: FloatingActionButton.extended(
|
||||
onPressed: () async {
|
||||
final created = await Navigator.of(context).push<bool>(
|
||||
MaterialPageRoute(builder: (_) => const EventFormScreen()),
|
||||
);
|
||||
if (created == true) _load();
|
||||
},
|
||||
icon: const Icon(Icons.add),
|
||||
label: const Text('New plan'),
|
||||
),
|
||||
body: RefreshIndicator(
|
||||
onRefresh: _load,
|
||||
child: ListView(
|
||||
padding: const EdgeInsets.fromLTRB(16, 12, 16, 96),
|
||||
children: [
|
||||
Wrap(
|
||||
spacing: 8,
|
||||
children: [
|
||||
ChoiceChip(
|
||||
label: const Text('All'),
|
||||
selected: _planType == null,
|
||||
onSelected: (_) {
|
||||
setState(() => _planType = null);
|
||||
_load();
|
||||
},
|
||||
),
|
||||
ChoiceChip(
|
||||
label: const Text('General'),
|
||||
selected: _planType == 'general',
|
||||
onSelected: (_) {
|
||||
setState(() => _planType = 'general');
|
||||
_load();
|
||||
},
|
||||
),
|
||||
ChoiceChip(
|
||||
label: const Text('Monthly'),
|
||||
selected: _planType == 'monthlySpecial',
|
||||
onSelected: (_) {
|
||||
setState(() => _planType = 'monthlySpecial');
|
||||
_load();
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
if (events.isEmpty)
|
||||
const Padding(
|
||||
padding: EdgeInsets.symmetric(vertical: 32),
|
||||
child: Center(child: Text('No plans yet.')),
|
||||
),
|
||||
for (final event in events)
|
||||
Card(
|
||||
child: ListTile(
|
||||
title: Text(event.title),
|
||||
subtitle: Text(
|
||||
'${planLabel(event.planType)}'
|
||||
'${event.month != null ? ' · ${event.month}/${event.year}' : ''}'
|
||||
'\n${event.dealerName ?? ''}\n${event.description}',
|
||||
),
|
||||
isThreeLine: true,
|
||||
trailing: Text(event.expense.toStringAsFixed(0)),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user