// ignore_for_file: inference_failure_on_function_invocation import 'dart:async'; import 'package:flutter/material.dart'; import 'package:flutter_gen/gen_l10n/app_localizations.dart'; import 'package:flutter_iconpicker/flutter_iconpicker.dart'; import 'package:prasule/api/category.dart'; import 'package:prasule/api/wallet.dart'; import 'package:prasule/api/walletmanager.dart'; import 'package:prasule/main.dart'; import 'package:prasule/pw/platformdialog.dart'; import 'package:prasule/pw/platformfield.dart'; import 'package:prasule/pw/platformroute.dart'; import 'package:prasule/views/settings/settings.dart'; import 'package:prasule/views/setup.dart'; /// Allows adding, editing or removing [WalletCategory]s class EditCategoriesView extends StatefulWidget { /// Allows adding, editing or removing [WalletCategory]s const EditCategoriesView({super.key}); @override State createState() => _EditCategoriesViewState(); } class _EditCategoriesViewState extends State { Wallet? selectedWallet; List wallets = []; List categories = []; @override void initState() { super.initState(); loadWallet(); } Future loadWallet() async { wallets = await WalletManager.listWallets(); if (wallets.isEmpty && mounted) { unawaited( Navigator.of(context) .pushReplacement(platformRoute((c) => const SetupView())), ); return; } selectedWallet = wallets.first; categories = selectedWallet!.categories; setState(() {}); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: DropdownButton( value: (selectedWallet == null) ? -1 : wallets.indexOf(selectedWallet!), items: [ ...wallets.map( (e) => DropdownMenuItem( value: wallets.indexOf( e, ), child: Text(e.name), ), ), DropdownMenuItem( value: -1, child: Text(AppLocalizations.of(context).newWallet), ), ], onChanged: (v) async { if (v == null || v == -1) { await Navigator.of(context).push( platformRoute( (c) => const SetupView( newWallet: true, ), ), ); wallets = await WalletManager.listWallets(); logger.i(wallets.length); selectedWallet = wallets.last; setState(() {}); return; } selectedWallet = wallets[v]; setState(() {}); }, ), actions: [ PopupMenuButton( itemBuilder: (context) => [ AppLocalizations.of(context).settings, AppLocalizations.of(context).about, ].map((e) => PopupMenuItem(value: e, child: Text(e))).toList(), onSelected: (value) { if (value == AppLocalizations.of(context).settings) { Navigator.of(context).push( platformRoute( (context) => const SettingsView(), ), ); } else if (value == AppLocalizations.of(context).about) { showAboutDialog( context: context, applicationLegalese: AppLocalizations.of(context).license, applicationName: "PraĊĦule", ); } }, ), ], ), body: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Text( AppLocalizations.of(context).setupCategoriesEditHint, textAlign: TextAlign.center, ), SizedBox( height: MediaQuery.of(context).size.height * 0.64, child: ListView.builder( shrinkWrap: true, itemBuilder: (context, i) => ListTile( leading: GestureDetector( onTap: () async { final icon = await FlutterIconPicker.showIconPicker(context); if (icon == null) return; categories[i].icon = icon; setState(() {}); }, child: Container( decoration: BoxDecoration( borderRadius: BorderRadius.circular(16), color: Theme.of(context).colorScheme.secondary, ), child: Padding( padding: const EdgeInsets.all(8), child: Icon( categories[i].icon, color: Theme.of(context).colorScheme.onSecondary, ), ), ), ), trailing: IconButton( icon: const Icon(Icons.cancel), onPressed: () { categories.removeAt(i); setState(() {}); }, ), title: GestureDetector( onTap: () { final controller = TextEditingController(text: categories[i].name); showDialog( context: context, builder: (c) => PlatformDialog( actions: [ TextButton( onPressed: () { if (controller.text.isEmpty) return; categories[i].name = controller.text; Navigator.of(context).pop(); }, child: Text(AppLocalizations.of(context).ok), ), TextButton( onPressed: () { Navigator.of(context).pop(); }, child: Text(AppLocalizations.of(context).cancel), ), ], title: AppLocalizations.of(context) .setupCategoriesEditingName, content: SizedBox( width: 400, child: PlatformField(controller: controller), ), ), ); }, child: Text( categories[i].name, style: const TextStyle(fontWeight: FontWeight.bold), ), ), ), itemCount: categories.length, ), ), IconButton( onPressed: () { var id = 1; while ( categories.where((element) => element.id == id).isNotEmpty) { id++; // create unique ID } categories.add( WalletCategory( name: AppLocalizations.of(context).setupWalletNamePlaceholder, type: EntryType.expense, id: id, icon: IconData( Icons.question_mark.codePoint, fontFamily: 'MaterialIcons', ), ), ); setState(() {}); }, icon: const Icon(Icons.add), ), ], ), ); } }