feat: wire + (add tx dialog) and search (filter lists)
This commit is contained in:
parent
a207302193
commit
e3baad4dfa
125
lib/app.dart
125
lib/app.dart
|
|
@ -5,6 +5,8 @@ import 'screens/dashboard_screen.dart';
|
||||||
import 'screens/budget_screen.dart';
|
import 'screens/budget_screen.dart';
|
||||||
import 'screens/accounts_screen.dart';
|
import 'screens/accounts_screen.dart';
|
||||||
import 'screens/transactions_screen.dart';
|
import 'screens/transactions_screen.dart';
|
||||||
|
import 'providers/db_provider.dart';
|
||||||
|
import 'data/db.dart';
|
||||||
|
|
||||||
final _selectedIndexProvider = StateProvider<int>((ref) => 0);
|
final _selectedIndexProvider = StateProvider<int>((ref) => 0);
|
||||||
|
|
||||||
|
|
@ -18,6 +20,8 @@ class YnabApp extends ConsumerStatefulWidget {
|
||||||
class _YnabAppState extends ConsumerState<YnabApp> {
|
class _YnabAppState extends ConsumerState<YnabApp> {
|
||||||
late final List<Widget> _screens;
|
late final List<Widget> _screens;
|
||||||
late final List<_NavItem> _navItems;
|
late final List<_NavItem> _navItems;
|
||||||
|
final _searchController = TextEditingController();
|
||||||
|
bool _searchActive = false;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
|
|
@ -36,6 +40,96 @@ class _YnabAppState extends ConsumerState<YnabApp> {
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void dispose() {
|
||||||
|
_searchController.dispose();
|
||||||
|
super.dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _addTransaction() async {
|
||||||
|
final accounts = ref.read(openAccountsProvider);
|
||||||
|
final db = ref.read(dbProvider);
|
||||||
|
if (accounts.isEmpty) {
|
||||||
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
|
const SnackBar(content: Text('Créez d\'abord un compte')),
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
final payeeCtrl = TextEditingController();
|
||||||
|
final amountCtrl = TextEditingController();
|
||||||
|
int? accountId = accounts.first.id;
|
||||||
|
DateTime date = DateTime.now();
|
||||||
|
|
||||||
|
await showDialog(
|
||||||
|
context: context,
|
||||||
|
builder: (ctx) => AlertDialog(
|
||||||
|
title: const Text('Nouvelle transaction'),
|
||||||
|
content: SingleChildScrollView(
|
||||||
|
child: Column(
|
||||||
|
mainAxisSize: MainAxisSize.min,
|
||||||
|
children: [
|
||||||
|
TextField(
|
||||||
|
controller: payeeCtrl,
|
||||||
|
decoration: const InputDecoration(labelText: 'Bénéficiaire', hintText: 'Ex: Supermarché'),
|
||||||
|
autofocus: true,
|
||||||
|
),
|
||||||
|
const SizedBox(height: 12),
|
||||||
|
TextField(
|
||||||
|
controller: amountCtrl,
|
||||||
|
decoration: const InputDecoration(labelText: 'Montant', hintText: '0,00'),
|
||||||
|
keyboardType: const TextInputType.numberWithOptions(decimal: true),
|
||||||
|
),
|
||||||
|
const SizedBox(height: 12),
|
||||||
|
DropdownButtonFormField<int>(
|
||||||
|
value: accountId,
|
||||||
|
decoration: const InputDecoration(labelText: 'Compte'),
|
||||||
|
items: accounts
|
||||||
|
.map((a) => DropdownMenuItem(value: a.id, child: Text(a.name)))
|
||||||
|
.toList(),
|
||||||
|
onChanged: (v) => accountId = v,
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
actions: [
|
||||||
|
TextButton(
|
||||||
|
onPressed: () => Navigator.pop(ctx),
|
||||||
|
child: const Text('Annuler'),
|
||||||
|
),
|
||||||
|
FilledButton(
|
||||||
|
onPressed: () async {
|
||||||
|
final payee = payeeCtrl.text.trim();
|
||||||
|
final amountStr = amountCtrl.text.trim().replaceAll(',', '.');
|
||||||
|
final amount = double.tryParse(amountStr);
|
||||||
|
if (payee.isEmpty || amount == null || accountId == null) return;
|
||||||
|
await db.into(db.transactions).insert(TransactionsCompanion.insert(
|
||||||
|
accountId: accountId!,
|
||||||
|
payee: payee,
|
||||||
|
amount: amount,
|
||||||
|
date: date,
|
||||||
|
));
|
||||||
|
if (ctx.mounted) Navigator.pop(ctx);
|
||||||
|
},
|
||||||
|
child: const Text('Ajouter'),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
);
|
||||||
|
payeeCtrl.dispose();
|
||||||
|
amountCtrl.dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
void _toggleSearch() {
|
||||||
|
setState(() {
|
||||||
|
_searchActive = !_searchActive;
|
||||||
|
if (!_searchActive) {
|
||||||
|
_searchController.clear();
|
||||||
|
ref.read(searchQueryProvider.notifier).state = '';
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
final selectedIndex = ref.watch(_selectedIndexProvider);
|
final selectedIndex = ref.watch(_selectedIndexProvider);
|
||||||
|
|
@ -46,6 +140,11 @@ class _YnabAppState extends ConsumerState<YnabApp> {
|
||||||
bottomNavigationBar: NavigationBar(
|
bottomNavigationBar: NavigationBar(
|
||||||
selectedIndex: selectedIndex,
|
selectedIndex: selectedIndex,
|
||||||
onDestinationSelected: (int index) {
|
onDestinationSelected: (int index) {
|
||||||
|
setState(() {
|
||||||
|
_searchActive = false;
|
||||||
|
_searchController.clear();
|
||||||
|
ref.read(searchQueryProvider.notifier).state = '';
|
||||||
|
});
|
||||||
ref.read(_selectedIndexProvider.notifier).state = index;
|
ref.read(_selectedIndexProvider.notifier).state = index;
|
||||||
},
|
},
|
||||||
destinations: _navItems.map((item) {
|
destinations: _navItems.map((item) {
|
||||||
|
|
@ -70,6 +169,20 @@ class _YnabAppState extends ConsumerState<YnabApp> {
|
||||||
),
|
),
|
||||||
child: Row(
|
child: Row(
|
||||||
children: [
|
children: [
|
||||||
|
if (_searchActive)
|
||||||
|
Expanded(
|
||||||
|
child: TextField(
|
||||||
|
controller: _searchController,
|
||||||
|
autofocus: true,
|
||||||
|
decoration: const InputDecoration(
|
||||||
|
hintText: 'Rechercher…',
|
||||||
|
border: InputBorder.none,
|
||||||
|
isDense: true,
|
||||||
|
),
|
||||||
|
onChanged: (v) => ref.read(searchQueryProvider.notifier).state = v,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
else
|
||||||
Text(
|
Text(
|
||||||
_navItems[selectedIndex].label,
|
_navItems[selectedIndex].label,
|
||||||
style: TextStyle(
|
style: TextStyle(
|
||||||
|
|
@ -79,17 +192,25 @@ class _YnabAppState extends ConsumerState<YnabApp> {
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
const Spacer(),
|
const Spacer(),
|
||||||
|
if (_searchActive)
|
||||||
|
IconButton(
|
||||||
|
icon: const Icon(Icons.close),
|
||||||
|
onPressed: _toggleSearch,
|
||||||
|
tooltip: 'Fermer la recherche',
|
||||||
|
)
|
||||||
|
else ...[
|
||||||
IconButton(
|
IconButton(
|
||||||
icon: Icon(Icons.add, color: colorScheme.primary),
|
icon: Icon(Icons.add, color: colorScheme.primary),
|
||||||
onPressed: () {},
|
onPressed: _addTransaction,
|
||||||
tooltip: 'Ajouter une transaction',
|
tooltip: 'Ajouter une transaction',
|
||||||
),
|
),
|
||||||
IconButton(
|
IconButton(
|
||||||
icon: Icon(Icons.search, color: colorScheme.onSurfaceVariant),
|
icon: Icon(Icons.search, color: colorScheme.onSurfaceVariant),
|
||||||
onPressed: () {},
|
onPressed: _toggleSearch,
|
||||||
tooltip: 'Rechercher',
|
tooltip: 'Rechercher',
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
// Content
|
// Content
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,8 @@ final dbProvider = Provider<AppDatabase>((ref) {
|
||||||
return db;
|
return db;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
final searchQueryProvider = StateProvider<String>((ref) => '');
|
||||||
|
|
||||||
// ── Accounts ────────────────────────────────────────────────────────────
|
// ── Accounts ────────────────────────────────────────────────────────────
|
||||||
final accountsProvider = StreamProvider<List<Account>>((ref) {
|
final accountsProvider = StreamProvider<List<Account>>((ref) {
|
||||||
final db = ref.watch(dbProvider);
|
final db = ref.watch(dbProvider);
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,11 @@ class AccountsScreen extends ConsumerWidget {
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context, WidgetRef ref) {
|
Widget build(BuildContext context, WidgetRef ref) {
|
||||||
final accounts = ref.watch(openAccountsProvider);
|
final all = ref.watch(openAccountsProvider);
|
||||||
|
final query = ref.watch(searchQueryProvider).toLowerCase();
|
||||||
|
final accounts = query.isEmpty
|
||||||
|
? all
|
||||||
|
: all.where((a) => a.name.toLowerCase().contains(query)).toList();
|
||||||
|
|
||||||
return ListView(
|
return ListView(
|
||||||
padding: const EdgeInsets.fromLTRB(16, 8, 16, 90),
|
padding: const EdgeInsets.fromLTRB(16, 8, 16, 90),
|
||||||
|
|
|
||||||
|
|
@ -10,12 +10,14 @@ class TransactionsScreen extends ConsumerWidget {
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context, WidgetRef ref) {
|
Widget build(BuildContext context, WidgetRef ref) {
|
||||||
final all = ref.watch(allTransactionsProvider).valueOrNull ?? [];
|
final all = ref.watch(allTransactionsProvider).valueOrNull ?? [];
|
||||||
|
final query = ref.watch(searchQueryProvider).toLowerCase();
|
||||||
|
final filtered = query.isEmpty ? all : all.where((t) => t.payee.toLowerCase().contains(query)).toList();
|
||||||
|
|
||||||
return ListView.builder(
|
return ListView.builder(
|
||||||
padding: const EdgeInsets.fromLTRB(16, 8, 16, 90),
|
padding: const EdgeInsets.fromLTRB(16, 8, 16, 90),
|
||||||
itemCount: all.length,
|
itemCount: filtered.length,
|
||||||
itemBuilder: (context, index) {
|
itemBuilder: (context, index) {
|
||||||
final t = all[index];
|
final t = filtered[index];
|
||||||
return Card(
|
return Card(
|
||||||
margin: const EdgeInsets.only(bottom: 4),
|
margin: const EdgeInsets.only(bottom: 4),
|
||||||
child: ListTile(
|
child: ListTile(
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue