Skip to content

Commit

Permalink
Refactor the screens
Browse files Browse the repository at this point in the history
  • Loading branch information
tayormi committed Mar 17, 2020
1 parent 06f0745 commit 5bd2ff2
Show file tree
Hide file tree
Showing 2 changed files with 170 additions and 145 deletions.
155 changes: 10 additions & 145 deletions lib/home.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:async';

import 'package:covid_tracker/pages/screens/home_screen.dart';
import 'package:covid_tracker/theme/color/light_color.dart';
import 'package:covid_tracker/theme/theme.dart';
import 'package:covid_tracker/utils/calculateGrowth.dart';
Expand All @@ -22,13 +23,9 @@ class Home extends StatefulWidget {
}

class _HomeState extends State<Home> {
Completer<void> _refreshCompleter;

int selectedIndex = 0;
@override
void initState() {
super.initState();
_refreshCompleter = Completer<void>();
}


void addPostFrameCallback(FrameCallback callback) {
// Load data on widget load
Expand All @@ -41,14 +38,14 @@ class _HomeState extends State<Home> {
iconData: Icons.home,
label: 'Home',
),
FFNavigationBarItem(
iconData: Icons.note,
label: 'Guides',
),
FFNavigationBarItem(
iconData: Icons.speaker_notes,
label: 'News',
),
FFNavigationBarItem(
iconData: Icons.note,
label: 'Guides',
),
FFNavigationBarItem(
iconData: Icons.settings,
label: 'Settings',
Expand All @@ -62,148 +59,16 @@ class _HomeState extends State<Home> {
);

Widget buildPageView() {
BlocProvider.of<CaseBloc>(context).add(FetchCase());
final Function wp = Screen(context).wp;
final Function hp = Screen(context).hp;

return PageView(
controller: pageController,
onPageChanged: (index) {
pageChanged(index);
},
children: <Widget>[
SingleChildScrollView(
padding: EdgeInsets.only(left: 20, right: 20, top: 20),
child: BlocBuilder<CaseBloc, CaseState>(
builder: (BuildContext context, CaseState state) {
print(state);
if (state is CaseLoading) {
return SizedBox(
width: wp(100),
height: hp(20),
child: Shimmer.fromColors(
baseColor: Colors.grey.withOpacity(0.3),
highlightColor: Colors.white38,
child: GlobalSituationCard(
cardTitle: 'loading',
caseTitle: 'loading',
currentData: 2344,
newData: null,
percentChange: calculateGrowthPercentage(234, 5678),
icon: showGrowthIcon(234, 5678),
color: showGrowthColor(234, 5678),
),
),
);
}
if (state is CaseLoaded) {
final currentData = state.currentData;
final firstData = state.firstData;
return RefreshIndicator(
child: Column(
children: <Widget>[
GlobalSituationCard(
cardTitle: 'TOTAL CASES',
caseTitle: 'Total',
currentData: currentData.totalCases,
newData: currentData.totalNewCasesToday,
percentChange: calculateGrowthPercentage(
currentData.totalCases,
currentData.totalNewCasesToday),
icon: showGrowthIcon(currentData.totalCases,
currentData.totalNewCasesToday),
color: Colors.red,
),
SizedBox(height: hp(3)),

GlobalSituationCard(
cardTitle: 'RECOVERED CASES',
caseTitle: 'Recovered',
currentData: currentData.totalRecovered,
newData: currentData.totalRecovered -
firstData.totalRecovered,
percentChange: calculateGrowthPercentage(
currentData.totalRecovered,
firstData.totalRecovered),
icon: showGrowthIcon(currentData.totalRecovered,
firstData.totalRecovered),
color: Colors.red,
),
SizedBox(height: hp(3)),
GlobalSituationCard(
cardTitle: 'DEATH CASES',
caseTitle: 'Deaths',
currentData: currentData.totalDeaths,
newData: currentData.totalNewDeathsToday,
percentChange: calculateGrowthPercentage(
currentData.totalDeaths,
currentData.totalNewDeathsToday),
icon: showGrowthIcon(currentData.totalDeaths,
currentData.totalNewDeathsToday),
color: Colors.red,
),
SizedBox(height: hp(3)),
GlobalSituationCard(
cardTitle: 'SERIOUS CASES',
caseTitle: 'Serious',
currentData: currentData.totalSeriousCases,
newData: currentData.totalSeriousCases -
firstData.totalSeriousCases,
percentChange: calculateGrowthPercentage(
currentData.totalSeriousCases,
currentData.totalSeriousCases -
firstData.totalSeriousCases),
icon: showGrowthIcon(currentData.totalSeriousCases,
firstData.totalSeriousCases),
color: Colors.red,
),
// GlobalSituationCard(
// cardTitle: 'DEATH CASES',
// caseTitle: 'Deaths',
// currentData: currentData.deathCases,
// newData: 1457, percentChange: calculateGrowthPercentage(currentData.deathCases, firstData.deathCases),
// icon: showGrowthIcon(currentData.deathCases, firstData.deathCases), color: showGrowthColor(currentData.deathCases, firstData.deathCases),
// ),
// SizedBox(height: hp(3)),
// GlobalSituationCard(
// cardTitle: 'RECOVERED CASES',
// caseTitle: 'Recovered',
// currentData: currentData.recoveredCases,
// newData: 1457, percentChange: calculateGrowthPercentage(currentData.recoveredCases, firstData.recoveredCases),
// icon: showGrowthIcon(currentData.recoveredCases, firstData.recoveredCases),
// color: showGrowthColor(currentData.recoveredCases, firstData.recoveredCases),
// ),
SizedBox(height: hp(3)),
],
),
onRefresh: () {
BlocProvider.of<CaseBloc>(context).add(FetchCase());
return _refreshCompleter.future;
},
);
}
if (state is CaseError) {
return Text(
'Something went wrong!',
style: TextStyle(color: Colors.red),
);
}
return Center(
child: RefreshIndicator(
child: Text('Pull to refresh'),
onRefresh: () {
BlocProvider.of<CaseBloc>(context).add(FetchCase());
return _refreshCompleter.future;
},
));
},
// listener: (BuildContext context, CaseState state) {
// if (state is CaseLoaded) {
// BlocProvider.of<CaseBloc>(context).add(FetchCase());
// }
// }
)),
InformationScreen(),
HomeScreen(),
NewsPage(),
InformationScreen(),
SettingsPage(),
],
);
Expand Down
160 changes: 160 additions & 0 deletions lib/pages/screens/home_screen.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
import 'dart:async';

import 'package:covid_tracker/blocs/blocs.dart';
import 'package:covid_tracker/pages/widgets/global_card.dart';
import 'package:covid_tracker/utils/calculateGrowth.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:responsive_screen/responsive_screen.dart';
import 'package:shimmer/shimmer.dart';

class HomeScreen extends StatefulWidget {
@override
_HomeScreenState createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
Completer<void> _refreshCompleter;
@override
void initState() {
super.initState();
_refreshCompleter = Completer<void>();
}

@override
Widget build(BuildContext context) {
BlocProvider.of<CaseBloc>(context).add(FetchCase());
final Function wp = Screen(context).wp;
final Function hp = Screen(context).hp;
return SingleChildScrollView(
padding: EdgeInsets.only(left: 20, right: 20, top: 20),
child: BlocBuilder<CaseBloc, CaseState>(
builder: (BuildContext context, CaseState state) {
print(state);
if (state is CaseLoading) {
return SizedBox(
width: wp(100),
height: hp(20),
child: Shimmer.fromColors(
baseColor: Colors.grey.withOpacity(0.3),
highlightColor: Colors.white38,
child: GlobalSituationCard(
cardTitle: 'loading',
caseTitle: 'loading',
currentData: 2344,
newData: null,
percentChange: calculateGrowthPercentage(234, 5678),
icon: showGrowthIcon(234, 5678),
color: showGrowthColor(234, 5678),
),
),
);
}
if (state is CaseLoaded) {
final currentData = state.currentData;
final firstData = state.firstData;
return RefreshIndicator(
child: Column(
children: <Widget>[
GlobalSituationCard(
cardTitle: 'TOTAL CASES',
caseTitle: 'Total',
currentData: currentData.totalCases,
newData: currentData.totalNewCasesToday,
percentChange: calculateGrowthPercentage(
currentData.totalCases,
currentData.totalNewCasesToday),
icon: showGrowthIcon(currentData.totalCases,
currentData.totalNewCasesToday),
color: Colors.red,
),
SizedBox(height: hp(3)),

GlobalSituationCard(
cardTitle: 'RECOVERED CASES',
caseTitle: 'Recovered',
currentData: currentData.totalRecovered,
newData:
currentData.totalRecovered - firstData.totalRecovered,
percentChange: calculateGrowthPercentage(
currentData.totalRecovered, firstData.totalRecovered),
icon: showGrowthIcon(
currentData.totalRecovered, firstData.totalRecovered),
color: Colors.red,
),
SizedBox(height: hp(3)),
GlobalSituationCard(
cardTitle: 'DEATH CASES',
caseTitle: 'Deaths',
currentData: currentData.totalDeaths,
newData: currentData.totalNewDeathsToday,
percentChange: calculateGrowthPercentage(
currentData.totalDeaths,
currentData.totalNewDeathsToday),
icon: showGrowthIcon(currentData.totalDeaths,
currentData.totalNewDeathsToday),
color: Colors.red,
),
SizedBox(height: hp(3)),
GlobalSituationCard(
cardTitle: 'SERIOUS CASES',
caseTitle: 'Serious',
currentData: currentData.totalSeriousCases,
newData: currentData.totalSeriousCases -
firstData.totalSeriousCases,
percentChange: calculateGrowthPercentage(
currentData.totalSeriousCases,
currentData.totalSeriousCases -
firstData.totalSeriousCases),
icon: showGrowthIcon(currentData.totalSeriousCases,
firstData.totalSeriousCases),
color: Colors.red,
),
// GlobalSituationCard(
// cardTitle: 'DEATH CASES',
// caseTitle: 'Deaths',
// currentData: currentData.deathCases,
// newData: 1457, percentChange: calculateGrowthPercentage(currentData.deathCases, firstData.deathCases),
// icon: showGrowthIcon(currentData.deathCases, firstData.deathCases), color: showGrowthColor(currentData.deathCases, firstData.deathCases),
// ),
// SizedBox(height: hp(3)),
// GlobalSituationCard(
// cardTitle: 'RECOVERED CASES',
// caseTitle: 'Recovered',
// currentData: currentData.recoveredCases,
// newData: 1457, percentChange: calculateGrowthPercentage(currentData.recoveredCases, firstData.recoveredCases),
// icon: showGrowthIcon(currentData.recoveredCases, firstData.recoveredCases),
// color: showGrowthColor(currentData.recoveredCases, firstData.recoveredCases),
// ),
SizedBox(height: hp(3)),
],
),
onRefresh: () {
BlocProvider.of<CaseBloc>(context).add(FetchCase());
return _refreshCompleter.future;
},
);
}
if (state is CaseError) {
return Text(
'Something went wrong!',
style: TextStyle(color: Colors.red),
);
}
return Center(
child: RefreshIndicator(
child: Text('Pull to refresh'),
onRefresh: () {
BlocProvider.of<CaseBloc>(context).add(FetchCase());
return _refreshCompleter.future;
},
));
},
// listener: (BuildContext context, CaseState state) {
// if (state is CaseLoaded) {
// BlocProvider.of<CaseBloc>(context).add(FetchCase());
// }
// }
));
}
}

0 comments on commit 5bd2ff2

Please sign in to comment.