cpd_app_gruppe_finanzplaner/lib/main.dart

329 lines
10 KiB
Dart
Raw Normal View History

2023-06-12 20:53:03 +02:00
import 'dart:convert';
import 'package:awesome_dialog/awesome_dialog.dart';
2023-06-15 02:23:13 +02:00
import 'package:flutter/services.dart';
import 'package:flutter_neumorphic/flutter_neumorphic.dart';
2023-06-17 03:09:31 +02:00
import 'package:provider/provider.dart';
2023-06-12 20:53:03 +02:00
import 'package:shared_preferences/shared_preferences.dart';
import 'package:tests/preferences.dart';
import 'package:tests/theme/theme_constants.dart';
import 'package:tests/theme/theme_manager.dart';
2023-06-14 02:46:11 +02:00
import "package:easy_localization/easy_localization.dart";
2023-06-15 00:55:02 +02:00
import 'account/account_dialog.dart';
import 'account/account.dart';
import 'account/account_detail.dart';
2023-06-14 02:46:11 +02:00
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await EasyLocalization.ensureInitialized();
2023-06-15 00:55:02 +02:00
runApp(
2023-06-17 03:09:31 +02:00
ChangeNotifierProvider(
create: (context) => ThemeManager(),
child: EasyLocalization(
supportedLocales: const [Locale('en', 'US'), Locale('de', 'DE')],
path: 'lib/assets/translations',
// <-- change the path of the translation files
fallbackLocale: const Locale('en', 'US'),
child: const FinancialPlannerApp()),
));
}
2023-06-15 00:55:02 +02:00
ThemeManager _themeManager = ThemeManager();
2023-06-12 20:53:03 +02:00
class FinancialPlannerApp extends StatelessWidget {
const FinancialPlannerApp({super.key});
2023-06-12 20:53:03 +02:00
@override
Widget build(BuildContext context) {
2023-06-17 03:09:31 +02:00
SystemChrome.setEnabledSystemUIMode(
SystemUiMode.manual, overlays: [SystemUiOverlay.top]);
2023-06-12 20:53:03 +02:00
return MaterialApp(
2023-06-14 02:46:11 +02:00
localizationsDelegates: context.localizationDelegates,
supportedLocales: context.supportedLocales,
locale: context.locale,
debugShowCheckedModeBanner: false,
2023-06-14 02:46:11 +02:00
title: 'title'.tr(),
theme: lightTheme,
darkTheme: darkTheme,
2023-06-17 03:09:31 +02:00
themeMode: Provider.of<ThemeManager>(context).themeMode,
home: const HomePage(),
2023-06-12 20:53:03 +02:00
);
}
}
class HomePage extends StatefulWidget {
const HomePage({super.key});
2023-06-12 20:53:03 +02:00
@override
HomePageState createState() => HomePageState();
2023-06-12 20:53:03 +02:00
}
class HomePageState extends State<HomePage> {
2023-06-12 20:53:03 +02:00
List<Account> accounts = [];
2023-06-17 03:09:31 +02:00
String _selectedCurrency = "";
2023-06-16 04:27:34 +02:00
Future<String> getCurrencyFromSharedPreferences(String key) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
2023-06-17 03:09:31 +02:00
if (prefs.getString(key) == "Euro") {
_selectedCurrency = "";
2023-06-16 04:27:34 +02:00
}
2023-06-17 03:09:31 +02:00
if (prefs.getString(key) == "Dollar") {
_selectedCurrency = r"$";
2023-06-16 04:27:34 +02:00
}
2023-06-17 03:09:31 +02:00
if (prefs.getString(key) == "CHF") {
_selectedCurrency = "CHF";
2023-06-16 04:27:34 +02:00
}
return prefs.getString(key) ?? 'Euro';
}
2023-06-12 20:53:03 +02:00
@override
void initState() {
super.initState();
_themeManager.addListener(themeListener);
2023-06-16 04:27:34 +02:00
getCurrencyFromSharedPreferences("currency").then((value) {
2023-06-17 03:09:31 +02:00
setState(() {});
2023-06-16 04:27:34 +02:00
});
2023-06-12 20:53:03 +02:00
loadAccounts();
}
2023-06-15 00:55:02 +02:00
@override
void dispose() {
_themeManager.removeListener(themeListener);
super.dispose();
2023-06-12 20:53:03 +02:00
}
2023-06-15 00:55:02 +02:00
themeListener() {
if (mounted) {
setState(() {});
}
}
2023-06-15 00:55:02 +02:00
2023-06-12 20:53:03 +02:00
Future<void> loadAccounts() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
final accountList = prefs.getStringList('accounts') ?? [];
setState(() {
2023-06-15 00:55:02 +02:00
accounts = accountList
.map((accountJson) => Account.fromJson(accountJson))
.toList();
2023-06-12 20:53:03 +02:00
});
}
Future<void> saveAccounts() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
2023-06-15 00:55:02 +02:00
List<String> accountJsonList =
2023-06-17 03:09:31 +02:00
accounts.map((account) => json.encode(account.toJson())).toList();
2023-06-12 20:53:03 +02:00
await prefs.setStringList('accounts', accountJsonList);
}
void addAccount(Account account) {
setState(() {
accounts.add(account);
saveAccounts();
});
}
void deleteAccount(Account account) {
setState(() {
accounts.remove(account);
saveAccounts();
});
}
void updateAccountBalance(Account account) {
setState(() {
saveAccounts();
});
}
2023-06-17 03:09:31 +02:00
Future<void> saveCurrencyToSharedPreferences(String key, String value) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
await prefs.setString(key, value);
}
2023-06-12 20:53:03 +02:00
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
2023-06-17 03:09:31 +02:00
toolbarHeight: 70,
centerTitle: true,
backgroundColor: Colors.transparent,
elevation: 0,
2023-06-14 02:46:11 +02:00
title: Text(
'title'.tr(),
2023-06-17 03:09:31 +02:00
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
color: Theme
.of(context)
.brightness == Brightness.dark
? Colors.white70
: Colors.black87),
),
actions: [
Padding(
2023-06-17 03:09:31 +02:00
padding: const EdgeInsets.only(right: 16.0, top: 9, bottom: 0),
child: NeumorphicButton(
2023-06-17 03:09:31 +02:00
margin: const EdgeInsets.only(bottom: 16),
2023-06-14 02:46:11 +02:00
onPressed: () async {
2023-06-15 00:55:02 +02:00
await Navigator.push(
context,
2023-06-15 00:55:02 +02:00
MaterialPageRoute(builder: (context) => const Settings()),
);
2023-06-15 00:55:02 +02:00
setState(() {});
},
style: NeumorphicStyle(
shape: NeumorphicShape.convex,
2023-06-15 00:55:02 +02:00
boxShape: const NeumorphicBoxShape.circle(),
2023-06-17 03:09:31 +02:00
depth: 8,
intensity: 0.9,
2023-06-17 03:09:31 +02:00
shadowLightColor:
Theme
.of(context)
.brightness == Brightness.light
? const NeumorphicStyle().shadowLightColor
: Theme
.of(context)
.shadowColor,
shadowDarkColor:
Theme
.of(context)
.brightness == Brightness.dark
? const NeumorphicStyle().shadowDarkColor
: grey400,
color: Theme
.of(context)
.brightness == Brightness.light
? grey200
: grey800,
),
2023-06-17 03:09:31 +02:00
child: Icon(Icons.settings,
color: Theme
.of(context)
.unselectedWidgetColor),
),
),
],
2023-06-12 20:53:03 +02:00
),
2023-06-17 03:09:31 +02:00
body: Padding(
padding: const EdgeInsets.only(top: 5),
child: ListView.builder(
physics: const BouncingScrollPhysics(),
itemCount: accounts.length,
itemBuilder: (context, index) {
return GestureDetector(
onLongPress: () {
AwesomeDialog(
btnOkText: "Delete".tr(),
btnOkColor: Colors.lightGreen,
btnCancelColor: Theme
.of(context)
.shadowColor,
context: context,
animType: AnimType.bottomSlide,
dialogType: DialogType.info,
title: 'deleteaccount'.tr(),
headerAnimationLoop: false,
desc: 'sure'.tr(),
btnCancelOnPress: () {},
btnOkOnPress: () {
deleteAccount(accounts[index]);
},
).show();
2023-06-15 00:55:02 +02:00
},
2023-06-17 03:09:31 +02:00
child: Neumorphic(
margin: const EdgeInsets.all(16),
style: NeumorphicStyle(
depth: 8,
intensity: 1,
shadowLightColor:
Theme
.of(context)
.brightness == Brightness.light
? const NeumorphicStyle().shadowLightColor
: grey800,
shadowDarkColor:
Theme
.of(context)
.brightness == Brightness.dark
? const NeumorphicStyle().shadowDarkColor
: Theme
.of(context)
.shadowColor,
color: Theme
.of(context)
.brightness == Brightness.light
? grey200
: grey800,
boxShape:
2023-06-15 00:55:02 +02:00
NeumorphicBoxShape.roundRect(BorderRadius.circular(15)),
2023-06-17 03:09:31 +02:00
),
child: ListTile(
title: Text(accounts[index].name),
subtitle: Text(
'${'balance'.tr()}: $_selectedCurrency${accounts[index]
.balance.toStringAsFixed(2)}'),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
AccountDetailPage(
account: accounts[index],
updateAccountBalance: updateAccountBalance,
),
),
);
},
),
),
);
},
)),
floatingActionButton: NeumorphicButton(
2023-06-12 20:53:03 +02:00
onPressed: () {
showDialog(
context: context,
builder: (BuildContext context) {
return AddAccountDialog(
addAccount: addAccount,
);
},
);
},
style: NeumorphicStyle(
depth: 8,
intensity: 1,
2023-06-17 03:09:31 +02:00
shadowLightColor: Theme
.of(context)
.brightness == Brightness.light
? const NeumorphicStyle().shadowLightColor
: Theme
.of(context)
.shadowColor,
shadowDarkColor: Theme
.of(context)
.brightness == Brightness.dark
? const NeumorphicStyle().shadowDarkColor
: grey400,
color: Theme
.of(context)
.brightness == Brightness.light
? grey200
: grey800,
boxShape: const NeumorphicBoxShape.circle(),
),
2023-06-17 03:09:31 +02:00
child: Icon(
Icons.add,
2023-06-15 00:55:02 +02:00
size: 60,
2023-06-17 03:09:31 +02:00
color: Theme
.of(context)
.brightness == Brightness.light
? Colors.black12
: Colors.white12,
),
2023-06-12 20:53:03 +02:00
),
);
}
}