cpd_app_gruppe_finanzplaner/lib/main.dart

231 lines
6.5 KiB
Dart
Raw Normal View History

2023-06-12 20:53:03 +02:00
import 'dart:convert';
import 'package:awesome_dialog/awesome_dialog.dart';
import 'package:flutter_neumorphic/flutter_neumorphic.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(
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-12 20:53:03 +02:00
}
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) {
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,
themeMode: _themeManager.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 = [];
@override
void initState() {
super.initState();
_themeManager.addListener(themeListener);
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 =
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();
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
centerTitle: true,
backgroundColor: Colors.transparent,
elevation: 0,
2023-06-14 02:46:11 +02:00
title: Text(
'title'.tr(),
style: const TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
color: Colors.black54,
),
),
actions: [
Padding(
padding: const EdgeInsets.only(right: 16.0, top: 3, bottom: 7),
child: NeumorphicButton(
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(),
depth: 6,
intensity: 0.9,
color: Colors.grey.shade100,
),
2023-06-15 00:55:02 +02:00
child: const Icon(Icons.settings, color: Colors.black38),
),
),
],
2023-06-12 20:53:03 +02:00
),
body: ListView.builder(
physics: const BouncingScrollPhysics(),
2023-06-12 20:53:03 +02:00
itemCount: accounts.length,
itemBuilder: (context, index) {
return GestureDetector(
onLongPress: () {
AwesomeDialog(
2023-06-14 02:46:11 +02:00
btnOkText: "Delete".tr(),
2023-06-15 00:55:02 +02:00
btnOkColor: Colors.lightGreen,
btnCancelColor: Colors.grey,
context: context,
animType: AnimType.bottomSlide,
dialogType: DialogType.info,
title: 'deleteaccount'.tr(),
headerAnimationLoop: false,
desc: 'sure'.tr(),
btnCancelOnPress: () {},
btnOkOnPress: () {
deleteAccount(accounts[index]);
},
).show();
},
child: Neumorphic(
margin: const EdgeInsets.all(16),
style: NeumorphicStyle(
depth: 7,
intensity: 1,
shadowDarkColor: Colors.grey.shade300,
color: Colors.grey.shade100,
2023-06-15 00:55:02 +02:00
boxShape:
NeumorphicBoxShape.roundRect(BorderRadius.circular(15)),
),
child: ListTile(
title: Text(accounts[index].name),
2023-06-15 00:55:02 +02:00
subtitle: Text(
'${'balance'.tr()}: \$${accounts[index].balance.toStringAsFixed(2)}'),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => AccountDetailPage(
account: accounts[index],
updateAccountBalance: updateAccountBalance,
),
),
);
},
),
2023-06-12 20:53:03 +02:00
),
);
},
),
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,
shadowDarkColor: Colors.grey.shade400,
color: Colors.grey.shade100,
boxShape: const NeumorphicBoxShape.circle(),
),
2023-06-15 00:55:02 +02:00
child: const Icon(
Icons.add,
2023-06-15 00:55:02 +02:00
size: 60,
color: Colors.black12,
),
2023-06-12 20:53:03 +02:00
),
);
}
}