From 4e217d210c50622f3ca9f945d1dd32eef3045510 Mon Sep 17 00:00:00 2001 From: Arlind <56867496+Lindi31@users.noreply.github.com> Date: Fri, 16 Jun 2023 04:27:34 +0200 Subject: [PATCH] Make currenciychange work --- lib/account/account_detail.dart | 34 ++++++++++++++++++++++--- lib/assets/translations/de-DE.json | 3 ++- lib/assets/translations/en-US.json | 3 ++- lib/main.dart | 23 ++++++++++++++++- lib/preferences.dart | 40 +++++++++++++++++++++--------- lib/saving_tips.dart | 3 ++- 6 files changed, 86 insertions(+), 20 deletions(-) diff --git a/lib/account/account_detail.dart b/lib/account/account_detail.dart index 98e49c4..c66dc60 100644 --- a/lib/account/account_detail.dart +++ b/lib/account/account_detail.dart @@ -6,6 +6,7 @@ import 'package:shared_preferences/shared_preferences.dart'; import 'package:tests/saving_tips.dart'; import 'package:tests/transaction/transaction_dialog.dart'; import 'package:tests/transaction/transaction.dart'; +import '../main.dart'; import 'account.dart'; import '../chart/expense_chart.dart'; import '../chart/expense_data.dart'; @@ -29,11 +30,32 @@ class AccountDetailPageState extends State List incomeTransactions = []; List expenseTransactions = []; List expenseData = []; + String _selectedCurrency="€"; + Future getCurrencyFromSharedPreferences(String key) async { + SharedPreferences prefs = await SharedPreferences.getInstance(); + if (prefs.getString(key)=="Euro"){ + _selectedCurrency="€"; + } + if (prefs.getString(key)=="Dollar"){ + _selectedCurrency=r"$"; + } + if (prefs.getString(key)=="CHF"){ + _selectedCurrency="CHF"; + } + + return prefs.getString(key) ?? 'Euro'; + } @override void initState() { super.initState(); _tabController = TabController(length: 3, vsync: this); + + getCurrencyFromSharedPreferences("currency").then((value) { + setState(() { + + }); + }); loadTransactions(); } @@ -167,7 +189,11 @@ class AccountDetailPageState extends State padding: const EdgeInsets.only(left: 16.0), child: NeumorphicButton( onPressed: () { - Navigator.pop(context); + Navigator.pop(context); // Zurück zur vorherigen Seite + Navigator.pushReplacement( // Neue Seite öffnen und vorherige Seite ersetzen + context, + MaterialPageRoute(builder: (context) => const HomePage()), + ); }, style: NeumorphicStyle( shape: NeumorphicShape.flat, @@ -329,7 +355,7 @@ class AccountDetailPageState extends State mainAxisSize: MainAxisSize.min, children: [ Text( - '${value.round()}' '€', + '${value.round()}$_selectedCurrency', ), Text( 'progress'.tr(), @@ -349,8 +375,8 @@ class AccountDetailPageState extends State subtitle: Text(transactionsList[index].title), trailing: Text( transactionsList[index].isExpense - ? '-\$${transactionsList[index].amount.toStringAsFixed(2)}' - : '+\$${transactionsList[index].amount.toStringAsFixed(2)}', + ? '-$_selectedCurrency${transactionsList[index].amount.toStringAsFixed(2)}' + : '+$_selectedCurrency${transactionsList[index].amount.toStringAsFixed(2)}', style: TextStyle( color: transactionsList[index].isExpense ? Colors.red : Colors.green, diff --git a/lib/assets/translations/de-DE.json b/lib/assets/translations/de-DE.json index 9d23159..b0422f9 100644 --- a/lib/assets/translations/de-DE.json +++ b/lib/assets/translations/de-DE.json @@ -23,5 +23,6 @@ "currency": "Währung", "budget": "Budgetcheck", "progress": "von deinem Budget ausgegeben", - "monthlyexpenses": "Monatliche Ausgaben" + "monthlyexpenses": "Monatliche Ausgaben", + "close": "Schließen" } \ No newline at end of file diff --git a/lib/assets/translations/en-US.json b/lib/assets/translations/en-US.json index 1a209bf..aaac8e7 100644 --- a/lib/assets/translations/en-US.json +++ b/lib/assets/translations/en-US.json @@ -23,5 +23,6 @@ "currency": "Currency", "budget": "Budget check", "progress": "of your budget spent", - "monthlyexpenses": "Monthly Expenses" + "monthlyexpenses": "Monthly Expenses", + "close": "Close" } \ No newline at end of file diff --git a/lib/main.dart b/lib/main.dart index 15765df..73a6715 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -55,11 +55,32 @@ class HomePage extends StatefulWidget { class HomePageState extends State { List accounts = []; + String _selectedCurrency="€"; + + Future getCurrencyFromSharedPreferences(String key) async { + SharedPreferences prefs = await SharedPreferences.getInstance(); + if (prefs.getString(key)=="Euro"){ + _selectedCurrency="€"; + } + if (prefs.getString(key)=="Dollar"){ + _selectedCurrency=r"$"; + } + if (prefs.getString(key)=="CHF"){ + _selectedCurrency="CHF"; + } + + return prefs.getString(key) ?? 'Euro'; + } @override void initState() { super.initState(); _themeManager.addListener(themeListener); + getCurrencyFromSharedPreferences("currency").then((value) { + setState(() { + + }); + }); loadAccounts(); } @@ -187,7 +208,7 @@ class HomePageState extends State { child: ListTile( title: Text(accounts[index].name), subtitle: Text( - '${'balance'.tr()}: \$${accounts[index].balance.toStringAsFixed(2)}'), + '${'balance'.tr()}: $_selectedCurrency${accounts[index].balance.toStringAsFixed(2)}'), onTap: () { Navigator.push( context, diff --git a/lib/preferences.dart b/lib/preferences.dart index 6891c38..f7de23a 100644 --- a/lib/preferences.dart +++ b/lib/preferences.dart @@ -1,8 +1,13 @@ +import 'dart:async'; + import 'package:easy_localization/easy_localization.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter_neumorphic/flutter_neumorphic.dart'; +import 'package:shared_preferences/shared_preferences.dart'; import 'package:tests/theme/theme_manager.dart'; +import 'main.dart'; + ThemeManager _themeManager = ThemeManager(); class Settings extends StatefulWidget { @@ -40,6 +45,16 @@ class SettingsState extends State { super.dispose(); } + Future saveCurrencyToSharedPreferences(String key, String value) async { + SharedPreferences prefs = await SharedPreferences.getInstance(); + await prefs.setString(key, value); + } + + Future getCurrencyFromSharedPreferences(String key) async { + SharedPreferences prefs = await SharedPreferences.getInstance(); + return prefs.getString(key) ?? 'Euro'; + } + themeListener() { if (mounted) { setState(() {}); @@ -50,6 +65,11 @@ class SettingsState extends State { void initState() { super.initState(); + getCurrencyFromSharedPreferences("currency").then((value) { + setState(() { + _selectedCurrency = value; + }); + }); _themeManager.addListener(themeListener); } @@ -82,7 +102,11 @@ class SettingsState extends State { padding: const EdgeInsets.only(left: 16.0), child: NeumorphicButton( onPressed: () { - Navigator.pop(context, "Change"); + Navigator.pop(context); // Zurück zur vorherigen Seite + Navigator.pushReplacement( // Neue Seite öffnen und vorherige Seite ersetzen + context, + MaterialPageRoute(builder: (context) => const HomePage()), + ); }, style: NeumorphicStyle( shape: NeumorphicShape.flat, @@ -120,16 +144,10 @@ class SettingsState extends State { lightSource: LightSource.topLeft, thumbShape: NeumorphicShape.concave, trackDepth: 5, - // Weitere Style-Eigenschaften hier anpassen - // Um den Switch heller zu machen, kannst du die Farben anpassen activeTrackColor: Colors.lightGreen, - // Farbe für den aktiven Track inactiveTrackColor: Colors.grey.shade300, - // Farbe für den inaktiven Track activeThumbColor: Colors.grey.shade100, - // Farbe für den aktiven Thumb - inactiveThumbColor: - Colors.grey.shade200, // Farbe für den inaktiven Thumb + inactiveThumbColor: Colors.grey.shade200, ), ), ), @@ -185,12 +203,10 @@ class SettingsState extends State { onChanged: (String? newValue) { setState(() { _selectedCurrency = newValue!; - // Hier kannst du die Währungseinstellung entsprechend anpassen - // z.B. mit einer Funktion, die die Währung ändert. + saveCurrencyToSharedPreferences("currency", _selectedCurrency); }); }, - items: - _currencies.map>((String value) { + items: _currencies.map>((String value) { return DropdownMenuItem( value: value, child: Text(value), diff --git a/lib/saving_tips.dart b/lib/saving_tips.dart index dbb083a..0c26bea 100644 --- a/lib/saving_tips.dart +++ b/lib/saving_tips.dart @@ -1,4 +1,5 @@ import 'package:carousel_slider/carousel_slider.dart'; +import 'package:easy_localization/easy_localization.dart'; import 'package:flutter_neumorphic/flutter_neumorphic.dart'; class SavingsTipsDialog extends StatelessWidget { @@ -89,7 +90,7 @@ class SavingsTipsDialog extends StatelessWidget { boxShape: NeumorphicBoxShape.roundRect(BorderRadius.circular(15.0)), ), - child: const Text('Close'), + child: Text('close'.tr()), ), ], ),