prasule/lib/views/setup.dart

398 lines
16 KiB
Dart
Raw Normal View History

// ignore_for_file: inference_failure_on_function_invocation
2023-09-08 11:50:21 +02:00
import 'package:currency_picker/currency_picker.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
2023-09-08 11:50:21 +02:00
import 'package:flutter_iconpicker/flutter_iconpicker.dart';
import 'package:introduction_screen/introduction_screen.dart';
import 'package:prasule/api/category.dart';
import 'package:prasule/api/wallet.dart';
import 'package:prasule/api/walletmanager.dart';
import 'package:prasule/pw/platformbutton.dart';
import 'package:prasule/pw/platformdialog.dart';
import 'package:prasule/pw/platformfield.dart';
import 'package:prasule/pw/platformroute.dart';
2023-09-08 11:50:21 +02:00
import 'package:prasule/views/home.dart';
/// View that shows on first-time setup
2023-09-08 11:50:21 +02:00
class SetupView extends StatefulWidget {
/// View that shows on first-time setup
const SetupView({super.key, this.newWallet = false});
2023-09-08 11:50:21 +02:00
/// We are only creating a new wallet, no first-time setup
final bool newWallet;
2023-09-08 11:50:21 +02:00
@override
State<SetupView> createState() => _SetupViewState();
}
class _SetupViewState extends State<SetupView> {
var _selectedCurrency = Currency.from(
json: {
"code": "USD",
"name": "United States Dollar",
"symbol": r"$",
"flag": "USD",
"decimal_digits": 2,
"number": 840,
"name_plural": "US dollars",
"thousands_separator": ",",
"decimal_separator": ".",
"space_between_amount_and_symbol": false,
"symbol_on_left": true,
},
);
List<WalletCategory> categories = <WalletCategory>[];
String name = "";
double balance = 0;
2023-10-11 18:37:18 +02:00
@override
void didChangeDependencies() {
super.didChangeDependencies();
if (categories.isEmpty) {
categories = [
WalletCategory(
name: AppLocalizations.of(context).noCategory,
id: 0,
icon: IconData(
Icons.payments.codePoint,
fontFamily: 'MaterialIcons',
),
),
WalletCategory(
name: AppLocalizations.of(context).categoryHealth,
id: 1,
icon: IconData(
Icons.medical_information.codePoint,
fontFamily: 'MaterialIcons',
),
),
WalletCategory(
name: AppLocalizations.of(context).categoryCar,
id: 2,
icon:
IconData(Icons.car_repair.codePoint, fontFamily: 'MaterialIcons'),
),
WalletCategory(
name: AppLocalizations.of(context).categoryFood,
id: 3,
icon:
IconData(Icons.restaurant.codePoint, fontFamily: 'MaterialIcons'),
),
WalletCategory(
name: AppLocalizations.of(context).categoryTravel,
id: 4,
icon: IconData(Icons.train.codePoint, fontFamily: 'MaterialIcons'),
),
];
setState(() {});
}
2023-10-11 18:37:18 +02:00
}
2023-09-08 11:50:21 +02:00
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: SizedBox(
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height,
child: IntroductionScreen(
dotsDecorator: DotsDecorator(
activeColor: Theme.of(context).colorScheme.primary,
),
showBackButton: true,
next: Text(AppLocalizations.of(context).next),
back: Text(AppLocalizations.of(context).back),
done: Text(AppLocalizations.of(context).finish),
2023-09-08 11:50:21 +02:00
onDone: () {
if (name.isEmpty) {
ScaffoldMessenger.of(context)
.clearSnackBars(); // TODO: iOS replacement
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(AppLocalizations.of(context).errorEmptyName),
),
);
2023-09-08 11:50:21 +02:00
return;
}
final wallet = Wallet(
name: name,
currency: _selectedCurrency,
categories: categories,
);
2023-09-08 11:50:21 +02:00
WalletManager.saveWallet(wallet).then(
(value) {
if (!value) {
ScaffoldMessenger.of(context).clearSnackBars();
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content:
Text(AppLocalizations.of(context).walletExists),
),
);
return;
}
if (widget.newWallet) {
Navigator.of(context).pop();
return;
}
Navigator.of(context).pushReplacement(
platformRoute(
(c) => const HomeView(),
),
);
},
2023-09-08 11:50:21 +02:00
);
},
pages: [
PageViewModel(
decoration:
const PageDecoration(bodyAlignment: Alignment.center),
2023-10-11 18:37:18 +02:00
titleWidget: Padding(
padding: const EdgeInsets.all(8),
2023-09-08 11:50:21 +02:00
child: Text(
AppLocalizations.of(context).welcome,
2023-10-11 18:37:18 +02:00
style: const TextStyle(
fontSize: 24,
fontWeight: FontWeight.bold,
),
2023-09-08 11:50:21 +02:00
textAlign: TextAlign.center,
),
),
2023-10-11 18:37:18 +02:00
bodyWidget: Column(
2023-09-08 11:50:21 +02:00
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: [
if (!widget.newWallet)
Flexible(
child: Text(
AppLocalizations.of(context).welcomeAboutPrasule,
),
),
if (!widget.newWallet)
const SizedBox(
height: 5,
),
2023-09-08 11:50:21 +02:00
Flexible(
2023-10-11 18:37:18 +02:00
child: Text(
AppLocalizations.of(context).welcomeInstruction,
),
2023-10-11 18:37:18 +02:00
),
2023-09-08 11:50:21 +02:00
],
),
),
PageViewModel(
decoration:
const PageDecoration(bodyAlignment: Alignment.center),
2023-10-11 18:37:18 +02:00
titleWidget: Padding(
padding: const EdgeInsets.all(8),
2023-09-08 11:50:21 +02:00
child: Text(
AppLocalizations.of(context).setupWalletNameCurrency,
2023-09-08 11:50:21 +02:00
textAlign: TextAlign.center,
2023-10-11 18:37:18 +02:00
style: const TextStyle(
fontSize: 24,
fontWeight: FontWeight.bold,
),
2023-09-08 11:50:21 +02:00
),
),
bodyWidget: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
SizedBox(
width: MediaQuery.of(context).size.width * 0.7,
child: PlatformField(
2023-10-11 18:37:18 +02:00
labelText:
AppLocalizations.of(context).setupNamePlaceholder,
2023-09-08 11:50:21 +02:00
onChanged: (t) {
name = t;
},
),
),
const SizedBox(
height: 5,
),
PlatformButton(
text: AppLocalizations.of(context)
2023-10-11 18:37:18 +02:00
.setupCurrency(_selectedCurrency.code),
2023-09-08 11:50:21 +02:00
onPressed: () {
showCurrencyPicker(
context: context,
onSelect: (currency) {
_selectedCurrency = currency;
setState(() {});
},
);
},
),
const SizedBox(
height: 5,
),
SizedBox(
width: MediaQuery.of(context).size.width * 0.7,
child: PlatformField(
labelText:
AppLocalizations.of(context).setupStartingBalance,
keyboardType: const TextInputType.numberWithOptions(
decimal: true,
),
inputFormatters: [
FilteringTextInputFormatter.allow(
RegExp(r'\d+[\.,]{0,1}\d{0,}'),
),
],
onChanged: (t) {
balance = double.parse(t);
},
),
),
2023-09-08 11:50:21 +02:00
],
),
),
PageViewModel(
decoration:
const PageDecoration(bodyAlignment: Alignment.center),
2023-10-11 18:37:18 +02:00
titleWidget: Padding(
padding: const EdgeInsets.all(8),
2023-09-08 11:50:21 +02:00
child: Text(
AppLocalizations.of(context).setupCategoriesHeading,
2023-09-08 11:50:21 +02:00
textAlign: TextAlign.center,
2023-10-11 18:37:18 +02:00
style: const TextStyle(
fontSize: 24,
fontWeight: FontWeight.bold,
),
2023-09-08 11:50:21 +02:00
),
),
bodyWidget: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
2023-10-11 18:37:18 +02:00
Text(
AppLocalizations.of(context).setupCategoriesEditHint,
2023-09-08 11:50:21 +02:00
textAlign: TextAlign.center,
),
SizedBox(
height: MediaQuery.of(context).size.height * 0.64,
child: ListView.builder(
shrinkWrap: true,
itemBuilder: (context, i) => (i == 0)
? const SizedBox()
: 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,
2023-09-08 11:50:21 +02:00
),
child: Padding(
padding: const EdgeInsets.all(8),
child: Icon(
categories[i].icon,
color: Theme.of(context)
.colorScheme
.onSecondary,
),
2023-09-08 11:50:21 +02:00
),
),
),
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),
),
),
),
2023-09-08 11:50:21 +02:00
itemCount: categories.length,
),
),
IconButton(
onPressed: () {
var id = 0;
2023-09-08 11:50:21 +02:00
while (categories
.where((element) => element.id == id)
.isNotEmpty) {
id++; // create unique ID
}
categories.add(
WalletCategory(
name: AppLocalizations.of(context)
2023-10-11 18:37:18 +02:00
.setupWalletNamePlaceholder,
2023-09-08 11:50:21 +02:00
id: id,
icon: IconData(
Icons.question_mark.codePoint,
fontFamily: 'MaterialIcons',
),
2023-09-08 11:50:21 +02:00
),
);
setState(() {});
},
icon: const Icon(Icons.add),
),
2023-09-08 11:50:21 +02:00
],
),
),
],
),
),
),
);
}
}