193 lines
4.5 KiB
Dart

// Flutter
import 'package:flutter/material.dart';
import '/models/item_type.dart';
import 'dart:io';
import 'package:package_info_plus/package_info_plus.dart';
// Local
import '/pages/tracked_item.dart';
import '/pages/report.dart';
import '/pages/settings.dart';
import '/pages/help.dart';
class HomePage extends StatefulWidget {
const HomePage({
super.key,
});
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
var pageSelected = 0;
refresh() {
setState(() {});
}
PackageInfo _packageInfo = PackageInfo(
appName: 'Unknown',
packageName: 'Unknown',
version: 'Unknown',
buildNumber: 'Unknown',
);
Future _initPackageInfo() async {
final PackageInfo info = await PackageInfo.fromPlatform();
setState(() {
_packageInfo = info;
});
}
@override
void initState() {
super.initState();
// Get package details
_initPackageInfo();
}
@override
Widget build(BuildContext context) {
Widget page;
Widget? dialog;
switch (pageSelected) {
case 0:
page = TrackedItemPage(
assetType: ItemType.expense,
notifyParent: refresh,
);
dialog = TrackedItemInputDialog(
notifyParent: refresh,
type: ItemType.expense,
);
break;
case 1:
page = TrackedItemPage(
assetType: ItemType.income,
notifyParent: refresh,
);
dialog = TrackedItemInputDialog(
notifyParent: refresh,
type: ItemType.income,
);
break;
case 2:
page = TrackedItemPage(
assetType: ItemType.asset,
notifyParent: refresh,
);
dialog = TrackedItemInputDialog(
notifyParent: refresh,
type: ItemType.asset,
);
break;
case 3:
page = ProjectionPage();
break;
case 4:
page = SettingsPage();
break;
case 5:
page = HelpPage();
break;
default:
throw UnimplementedError('No widget for page $pageSelected yet!');
}
Future<void> addNewValue(BuildContext context) {
return showDialog(
context: context,
builder: (_) => AlertDialog(content: dialog),
);
}
Widget? floatingButton;
if (dialog != null) {
floatingButton = IconButton(
onPressed: () {
addNewValue(context);
},
icon: Icon(Icons.add),
color: Theme.of(context).colorScheme.onSurface,
);
}
Widget navigation = NavigationRail(
extended: true,
destinations: [
NavigationRailDestination(
icon: Icon(Icons.payment),
label: Text(ItemType.expense.plural),
),
NavigationRailDestination(
icon: Icon(Icons.account_balance),
label: Text(ItemType.income.plural),
),
NavigationRailDestination(
icon: Icon(Icons.attach_money),
label: Text(ItemType.asset.plural),
),
NavigationRailDestination(
icon: Icon(Icons.bar_chart),
label: Text('Reports'),
),
NavigationRailDestination(
icon: Icon(Icons.settings),
label: Text('Settings'),
),
NavigationRailDestination(
icon: Icon(Icons.help),
label: Text('Help'),
),
],
selectedIndex: pageSelected,
onDestinationSelected: (value) {
setState(() {
pageSelected = value;
if (Platform.isAndroid || Platform.isIOS) {
Navigator.pop(context);
}
});
},
leading: Text("Menu"),
trailing: Text("v${_packageInfo.version}"),
);
Widget main = Container(
color: Theme.of(context).colorScheme.primaryContainer,
child: Center(child: page),
);
Widget? drawer;
Widget body;
if (Platform.isAndroid || Platform.isIOS) {
drawer = navigation;
body = SafeArea(child: main);
} else {
drawer = null;
body = Row(
children: [
SafeArea(child: navigation),
Expanded(child: main),
],
);
}
return LayoutBuilder(builder: (context, constraints) {
return Scaffold(
appBar: AppBar(
title: Text("Expense Tracker"),
),
drawer: drawer,
body: body,
floatingActionButton: floatingButton,
extendBody: false,
extendBodyBehindAppBar: false,
resizeToAvoidBottomInset: true,
);
});
}
}