2023-06-12 20:53:03 +02:00
|
|
|
import 'dart:convert';
|
2023-06-14 00:26:04 +02:00
|
|
|
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';
|
2023-06-14 00:26:04 +02:00
|
|
|
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
|
|
|
|
2023-06-14 00:26:04 +02:00
|
|
|
ThemeManager _themeManager = ThemeManager();
|
2023-06-12 20:53:03 +02:00
|
|
|
|
|
|
|
class FinancialPlannerApp extends StatelessWidget {
|
2023-06-14 00:26:04 +02:00
|
|
|
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,
|
2023-06-14 00:26:04 +02:00
|
|
|
debugShowCheckedModeBanner: false,
|
2023-06-14 02:46:11 +02:00
|
|
|
title: 'title'.tr(),
|
2023-06-14 00:26:04 +02:00
|
|
|
theme: lightTheme,
|
|
|
|
darkTheme: darkTheme,
|
|
|
|
themeMode: _themeManager.themeMode,
|
|
|
|
home: const HomePage(),
|
2023-06-12 20:53:03 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class HomePage extends StatefulWidget {
|
2023-06-14 00:26:04 +02:00
|
|
|
const HomePage({super.key});
|
|
|
|
|
2023-06-12 20:53:03 +02:00
|
|
|
@override
|
2023-06-14 00:26:04 +02:00
|
|
|
HomePageState createState() => HomePageState();
|
2023-06-12 20:53:03 +02:00
|
|
|
}
|
|
|
|
|
2023-06-14 00:26:04 +02:00
|
|
|
class HomePageState extends State<HomePage> {
|
2023-06-12 20:53:03 +02:00
|
|
|
List<Account> accounts = [];
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
2023-06-14 00:26:04 +02:00
|
|
|
_themeManager.addListener(themeListener);
|
2023-06-12 20:53:03 +02:00
|
|
|
loadAccounts();
|
2023-06-14 00:26:04 +02:00
|
|
|
}
|
2023-06-15 00:55:02 +02:00
|
|
|
|
2023-06-14 00:26:04 +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-14 00:26:04 +02:00
|
|
|
}
|
|
|
|
}
|
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(
|
2023-06-14 00:26:04 +02:00
|
|
|
centerTitle: true,
|
|
|
|
backgroundColor: Colors.transparent,
|
|
|
|
elevation: 0,
|
2023-06-14 02:46:11 +02:00
|
|
|
title: Text(
|
|
|
|
'title'.tr(),
|
|
|
|
style: const TextStyle(
|
2023-06-14 00:26:04 +02:00
|
|
|
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(
|
2023-06-14 00:26:04 +02:00
|
|
|
context,
|
2023-06-15 00:55:02 +02:00
|
|
|
MaterialPageRoute(builder: (context) => const Settings()),
|
2023-06-14 00:26:04 +02:00
|
|
|
);
|
2023-06-15 00:55:02 +02:00
|
|
|
setState(() {});
|
2023-06-14 00:26:04 +02:00
|
|
|
},
|
|
|
|
style: NeumorphicStyle(
|
|
|
|
shape: NeumorphicShape.convex,
|
2023-06-15 00:55:02 +02:00
|
|
|
boxShape: const NeumorphicBoxShape.circle(),
|
2023-06-14 00:26:04 +02:00
|
|
|
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-14 00:26:04 +02:00
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
2023-06-12 20:53:03 +02:00
|
|
|
),
|
|
|
|
body: ListView.builder(
|
2023-06-14 00:26:04 +02:00
|
|
|
physics: const BouncingScrollPhysics(),
|
2023-06-12 20:53:03 +02:00
|
|
|
itemCount: accounts.length,
|
|
|
|
itemBuilder: (context, index) {
|
2023-06-14 00:26:04 +02:00
|
|
|
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();
|
2023-06-14 00:26:04 +02:00
|
|
|
},
|
|
|
|
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)),
|
2023-06-14 00:26:04 +02:00
|
|
|
),
|
|
|
|
child: ListTile(
|
|
|
|
title: Text(accounts[index].name),
|
2023-06-15 00:55:02 +02:00
|
|
|
subtitle: Text(
|
|
|
|
'${'balance'.tr()}: \$${accounts[index].balance.toStringAsFixed(2)}'),
|
2023-06-14 00:26:04 +02:00
|
|
|
onTap: () {
|
|
|
|
Navigator.push(
|
|
|
|
context,
|
|
|
|
MaterialPageRoute(
|
|
|
|
builder: (context) => AccountDetailPage(
|
|
|
|
account: accounts[index],
|
|
|
|
updateAccountBalance: updateAccountBalance,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
2023-06-12 20:53:03 +02:00
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
2023-06-14 00:26:04 +02:00
|
|
|
floatingActionButton: NeumorphicButton(
|
2023-06-12 20:53:03 +02:00
|
|
|
onPressed: () {
|
|
|
|
showDialog(
|
|
|
|
context: context,
|
|
|
|
builder: (BuildContext context) {
|
|
|
|
return AddAccountDialog(
|
|
|
|
addAccount: addAccount,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
},
|
2023-06-14 00:26:04 +02:00
|
|
|
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(
|
2023-06-14 00:26:04 +02:00
|
|
|
Icons.add,
|
2023-06-15 00:55:02 +02:00
|
|
|
size: 60,
|
2023-06-14 00:26:04 +02:00
|
|
|
color: Colors.black12,
|
|
|
|
),
|
2023-06-12 20:53:03 +02:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|