Cleanup and widget refactor
This commit is contained in:
+28
-138
@@ -2,14 +2,19 @@ import 'package:cloud_firestore/cloud_firestore.dart';
|
||||
import 'package:como_gasto/como_gasto_icons.dart';
|
||||
import 'package:como_gasto/como_gasto_localizations.dart';
|
||||
import 'package:como_gasto/others/month_widget.dart';
|
||||
import 'package:como_gasto/pages/notifications/local_notifications.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:get_it/get_it.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:rect_getter/rect_getter.dart';
|
||||
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
||||
|
||||
import '../expenses_repository.dart';
|
||||
import '../utils.dart';
|
||||
import 'ui/loading_widget.dart';
|
||||
import 'ui/month_selector_slider.dart';
|
||||
import 'ui/no_data_widget.dart';
|
||||
import 'ui/stream_widget.dart';
|
||||
|
||||
class HomePage extends StatefulWidget {
|
||||
@override
|
||||
@@ -17,11 +22,9 @@ class HomePage extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _HomePageState extends State<HomePage> {
|
||||
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin;
|
||||
var globalKey = RectGetter.createGlobalKey();
|
||||
Rect buttonRect;
|
||||
|
||||
PageController _controller;
|
||||
int currentPage = DateTime.now().month - 1;
|
||||
Stream<QuerySnapshot> _query;
|
||||
GraphType currentType = GraphType.LINES;
|
||||
@@ -30,11 +33,6 @@ class _HomePageState extends State<HomePage> {
|
||||
void initState() {
|
||||
super.initState();
|
||||
|
||||
_controller = PageController(
|
||||
initialPage: currentPage,
|
||||
viewportFraction: 0.4,
|
||||
);
|
||||
|
||||
setupNotificationPlugin();
|
||||
}
|
||||
|
||||
@@ -98,139 +96,43 @@ class _HomePageState extends State<HomePage> {
|
||||
}
|
||||
|
||||
Widget _body() {
|
||||
ComoGastoLocalizations localizations =
|
||||
Localizations.of<ComoGastoLocalizations>(
|
||||
context, ComoGastoLocalizations);
|
||||
return SafeArea(
|
||||
child: Column(
|
||||
children: <Widget>[
|
||||
_selector(localizations),
|
||||
StreamBuilder<QuerySnapshot>(
|
||||
stream: _query,
|
||||
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> data) {
|
||||
if (data.connectionState == ConnectionState.active) {
|
||||
if (data.data.documents.length > 0) {
|
||||
return MonthWidget(
|
||||
days: daysInMonth(currentPage + 1),
|
||||
documents: data.data.documents,
|
||||
graphType: currentType,
|
||||
month: currentPage,
|
||||
);
|
||||
} else {
|
||||
return Expanded(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: <Widget>[
|
||||
Image.asset('assets/no_data.png'),
|
||||
SizedBox(height: 80),
|
||||
Text(
|
||||
localizations.t('home.emptyList'),
|
||||
style: Theme.of(context).textTheme.caption,
|
||||
)
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
MonthSelectorSlider(
|
||||
currentPage: currentPage,
|
||||
onPageChanged: (newPage) {
|
||||
var db = Provider.of<ExpensesRepository>(context, listen: false);
|
||||
|
||||
return Expanded(
|
||||
child: Center(
|
||||
child: CircularProgressIndicator(),
|
||||
),
|
||||
);
|
||||
setState(() {
|
||||
currentPage = newPage;
|
||||
_query = db.queryByMonth(currentPage + 1);
|
||||
});
|
||||
},
|
||||
),
|
||||
StreamWidget(
|
||||
stream: _query,
|
||||
loading: (_) => LoadingWidget(),
|
||||
empty: (_) => NoDataWidget(),
|
||||
done: (_, data) => MonthWidget(
|
||||
days: daysInMonth(currentPage + 1),
|
||||
documents: data.documents,
|
||||
graphType: currentType,
|
||||
month: currentPage,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _pageItem(String name, int position) {
|
||||
var _alignment;
|
||||
final selected = TextStyle(
|
||||
fontSize: 20.0,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: Colors.blueGrey,
|
||||
);
|
||||
final unselected = TextStyle(
|
||||
fontSize: 20.0,
|
||||
fontWeight: FontWeight.normal,
|
||||
color: Colors.blueGrey.withOpacity(0.4),
|
||||
);
|
||||
|
||||
if (position == currentPage) {
|
||||
_alignment = Alignment.center;
|
||||
} else if (position > currentPage) {
|
||||
_alignment = Alignment.centerRight;
|
||||
} else {
|
||||
_alignment = Alignment.centerLeft;
|
||||
}
|
||||
|
||||
return Align(
|
||||
alignment: _alignment,
|
||||
child: Text(
|
||||
name,
|
||||
style: position == currentPage ? selected : unselected,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _selector(ComoGastoLocalizations localizations) {
|
||||
return SizedBox.fromSize(
|
||||
size: Size.fromHeight(70.0),
|
||||
child: PageView(
|
||||
onPageChanged: (newPage) {
|
||||
var db = Provider.of<ExpensesRepository>(context);
|
||||
|
||||
setState(() {
|
||||
currentPage = newPage;
|
||||
_query = db.queryByMonth(currentPage + 1);
|
||||
});
|
||||
},
|
||||
controller: _controller,
|
||||
children: <Widget>[
|
||||
_pageItem(localizations.t("months.jan"), 0),
|
||||
_pageItem(localizations.t("months.feb"), 1),
|
||||
_pageItem(localizations.t("months.mar"), 2),
|
||||
_pageItem(localizations.t("months.apr"), 3),
|
||||
_pageItem(localizations.t("months.may"), 4),
|
||||
_pageItem(localizations.t("months.jun"), 5),
|
||||
_pageItem(localizations.t("months.jul"), 6),
|
||||
_pageItem(localizations.t("months.aug"), 7),
|
||||
_pageItem(localizations.t("months.sep"), 8),
|
||||
_pageItem(localizations.t("months.oct"), 9),
|
||||
_pageItem(localizations.t("months.nov"), 10),
|
||||
_pageItem(localizations.t("months.dec"), 11),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void setupNotificationPlugin() {
|
||||
if (kIsWeb) {
|
||||
return;
|
||||
}
|
||||
flutterLocalNotificationsPlugin = new FlutterLocalNotificationsPlugin();
|
||||
LocalNotifications localNotifications = GetIt.I();
|
||||
|
||||
// initialise the plugin. app_icon needs to be a added as a drawable resource to the Android head project
|
||||
var initializationSettingsAndroid =
|
||||
new AndroidInitializationSettings('app_icon');
|
||||
var initializationSettingsIOS = new IOSInitializationSettings(
|
||||
localNotifications.init(
|
||||
onSelectNotification: onSelectNotification,
|
||||
onDidReceiveLocalNotification: onDidReceiveLocalNotification,
|
||||
);
|
||||
var initializationSettings = new InitializationSettings(
|
||||
initializationSettingsAndroid,
|
||||
initializationSettingsIOS,
|
||||
);
|
||||
|
||||
flutterLocalNotificationsPlugin
|
||||
.initialize(
|
||||
initializationSettings,
|
||||
onSelectNotification: onSelectNotification,
|
||||
)
|
||||
.then((init) {
|
||||
setupNotification();
|
||||
});
|
||||
}
|
||||
|
||||
Future onSelectNotification(String payload) async {
|
||||
@@ -260,16 +162,4 @@ class _HomePageState extends State<HomePage> {
|
||||
],
|
||||
));
|
||||
}
|
||||
|
||||
void setupNotification() async {
|
||||
var time = new Time(16, 11, 0);
|
||||
var androidPlatformChannelSpecifics = new AndroidNotificationDetails(
|
||||
'daily-notifications', 'Daily Notifications', 'Daily Notifications');
|
||||
var iOSPlatformChannelSpecifics = new IOSNotificationDetails();
|
||||
var platformChannelSpecifics = new NotificationDetails(
|
||||
androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
|
||||
|
||||
await flutterLocalNotificationsPlugin.showDailyAtTime(0, 'Spend-o-meter',
|
||||
"Don't forget to add your expenses", time, platformChannelSpecifics);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user