mirror of
https://github.com/nisrulz/flutter-examples.git
synced 2026-04-07 16:22:38 +00:00
Added covid-19 example app and Updated the readme (#73)
This commit is contained in:
94
covid19_mobile_app/lib/screens/countrylist.dart
Normal file
94
covid19_mobile_app/lib/screens/countrylist.dart
Normal file
@@ -0,0 +1,94 @@
|
||||
import 'package:covid19_mobile_app/resources/fetch_data_functions.dart';
|
||||
import 'package:covid19_mobile_app/widgets/foldable_cell_widgets.dart';
|
||||
|
||||
import '../screens/searchCountry.dart';
|
||||
import '../widgets/drawer.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'dart:convert';
|
||||
import 'package:folding_cell/folding_cell.dart';
|
||||
|
||||
class CountryList extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
List<dynamic> results;
|
||||
return Scaffold(
|
||||
drawer: DrawerWidget(),
|
||||
appBar: AppBar(
|
||||
title: Text("Affected Countries"),
|
||||
centerTitle: true,
|
||||
actions: <Widget>[
|
||||
IconButton(
|
||||
icon: Icon(Icons.search),
|
||||
onPressed: () {
|
||||
showSearch(
|
||||
context: context,
|
||||
delegate: CountrySearchDelegate(results),
|
||||
);
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
body: FutureBuilder(
|
||||
future: getAllCountriesData(),
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.connectionState == ConnectionState.waiting)
|
||||
return Center(
|
||||
child: CircularProgressIndicator(),
|
||||
);
|
||||
if (snapshot.hasError)
|
||||
return Center(
|
||||
child: Text("Error! please check you wifi connection"),
|
||||
);
|
||||
results = jsonDecode(snapshot.data);
|
||||
return ListView.builder(
|
||||
physics: BouncingScrollPhysics(),
|
||||
itemCount: results.length,
|
||||
itemBuilder: (context, index) {
|
||||
var _foldingCellKey = GlobalKey<SimpleFoldingCellState>();
|
||||
return Container(
|
||||
color: Color(0xFF2e282a),
|
||||
alignment: Alignment.topCenter,
|
||||
child: SimpleFoldingCell(
|
||||
key: _foldingCellKey,
|
||||
frontWidget: Builder(
|
||||
builder: (BuildContext context) {
|
||||
return GestureDetector(
|
||||
onTap: () {
|
||||
SimpleFoldingCellState foldingCellState =
|
||||
context.findAncestorStateOfType();
|
||||
foldingCellState?.toggleFold();
|
||||
},
|
||||
child: Container(
|
||||
color: Color(0xff000000),
|
||||
child: buildFrontWidget(
|
||||
context,
|
||||
results[index]['countryInfo']['flag'],
|
||||
results[index]['country'],
|
||||
results[index]['cases'].toString())),
|
||||
);
|
||||
},
|
||||
),
|
||||
innerTopWidget: buildInnerTopWidget(
|
||||
results[index]['country'],
|
||||
results[index]['todayCases'].toString(),
|
||||
results[index]['deaths'].toString(),
|
||||
results[index]['todayDeaths'].toString(),
|
||||
results[index]['recovered'].toString(),
|
||||
results[index]['critical'].toString(),
|
||||
results[index]['casesPerOneMillion'].toString(),
|
||||
),
|
||||
innerBottomWidget: buildInnerBottomWidget(
|
||||
results[index]['cases'].toString()),
|
||||
cellSize: Size(MediaQuery.of(context).size.width, 125),
|
||||
padding: EdgeInsets.all(15),
|
||||
animationDuration: Duration(milliseconds: 300),
|
||||
borderRadius: 10,
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
138
covid19_mobile_app/lib/screens/home.dart
Normal file
138
covid19_mobile_app/lib/screens/home.dart
Normal file
@@ -0,0 +1,138 @@
|
||||
import 'dart:convert';
|
||||
import 'package:covid19_mobile_app/resources/fetch_data_functions.dart';
|
||||
import 'package:covid19_mobile_app/widgets/graph.dart';
|
||||
import 'package:covid19_mobile_app/widgets/info_card.dart';
|
||||
import '../widgets/drawer.dart';
|
||||
import 'package:number_display/number_display.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class Home extends StatelessWidget {
|
||||
final refreshKey = GlobalKey<RefreshIndicatorState>();
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
drawer: DrawerWidget(),
|
||||
appBar: AppBar(
|
||||
title: Text(
|
||||
"World Wide Cases",
|
||||
),
|
||||
centerTitle: true,
|
||||
),
|
||||
body: RefreshIndicator(
|
||||
// pull to refresh
|
||||
key: refreshKey,
|
||||
onRefresh: () => Navigator.pushReplacement(context,
|
||||
PageRouteBuilder(pageBuilder: (context, a1, a2) => Home())),
|
||||
child: FutureBuilder(
|
||||
future: getAllData(),
|
||||
builder: (context, snapshot) {
|
||||
// Display CircularProgressIndicator if data isn't fetched yet
|
||||
if (snapshot.connectionState == ConnectionState.waiting)
|
||||
return Center(child: CircularProgressIndicator());
|
||||
// If in case an error occurs
|
||||
if (snapshot.hasError) {
|
||||
return Container(
|
||||
child: Center(
|
||||
child: Text(
|
||||
"Failed to load data. Please check you wifi connection!",
|
||||
style: TextStyle(color: Colors.red),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// results of all countries(taken together)
|
||||
Map<String, dynamic> globalResults =
|
||||
json.decode(snapshot.data['all']);
|
||||
|
||||
// daily cases results
|
||||
List<Map<String, dynamic>> dailyCasesResults =
|
||||
(snapshot.data['country']);
|
||||
|
||||
// daily deaths results
|
||||
List<Map<String, dynamic>> dailyDeaths =
|
||||
(snapshot.data['deaths']);
|
||||
|
||||
// daily recoveries results
|
||||
List<Map<String, dynamic>> dailyRecoveries =
|
||||
(snapshot.data['recovered']);
|
||||
|
||||
// To display data in a width-limited component
|
||||
// Eg: converts 2,000,000 to 2M
|
||||
final updateNumberDisplay = createDisplay(length: 4);
|
||||
|
||||
return Padding(
|
||||
padding:
|
||||
EdgeInsets.symmetric(horizontal: 10.0, vertical: 5.0),
|
||||
child: ListView(
|
||||
physics: BouncingScrollPhysics(),
|
||||
children: <Widget>[
|
||||
GridView.count(
|
||||
shrinkWrap: true,
|
||||
physics: NeverScrollableScrollPhysics(),
|
||||
crossAxisCount: 2,
|
||||
children: <Widget>[
|
||||
infoCard(context, "Total Cases",
|
||||
globalResults['cases'].toString()),
|
||||
infoCard(context, "Deaths",
|
||||
globalResults['deaths'].toString()),
|
||||
infoCard(context, "Recovered",
|
||||
globalResults['recovered'].toString()),
|
||||
infoCard(context, "Active",
|
||||
globalResults['active'].toString()),
|
||||
infoCard(context, "Updated",
|
||||
updateNumberDisplay(globalResults['updated'])),
|
||||
infoCard(context, "Affected Countries",
|
||||
globalResults['affectedCountries'].toString()),
|
||||
],
|
||||
),
|
||||
SizedBox(
|
||||
height: 25.0,
|
||||
),
|
||||
Text(
|
||||
"Daily Cases in India",
|
||||
textAlign: TextAlign.center,
|
||||
style: TextStyle(
|
||||
fontSize: MediaQuery.of(context).size.height * 0.03,
|
||||
),
|
||||
),
|
||||
SizedBox(
|
||||
height: 5.0,
|
||||
),
|
||||
buildGraph(context, dailyCasesResults),
|
||||
SizedBox(
|
||||
height: 25.0,
|
||||
),
|
||||
Text(
|
||||
"Daily Deaths in India",
|
||||
textAlign: TextAlign.center,
|
||||
style: TextStyle(
|
||||
fontSize: MediaQuery.of(context).size.height * 0.03,
|
||||
),
|
||||
),
|
||||
SizedBox(
|
||||
height: 5.0,
|
||||
),
|
||||
buildGraph(context, dailyDeaths),
|
||||
SizedBox(
|
||||
height: 25.0,
|
||||
),
|
||||
Text(
|
||||
"Daily Recoveries in India",
|
||||
textAlign: TextAlign.center,
|
||||
style: TextStyle(
|
||||
fontSize: MediaQuery.of(context).size.height * 0.03,
|
||||
),
|
||||
),
|
||||
SizedBox(
|
||||
height: 5.0,
|
||||
),
|
||||
buildGraph(context, dailyRecoveries),
|
||||
],
|
||||
),
|
||||
);
|
||||
}),
|
||||
));
|
||||
}
|
||||
}
|
||||
165
covid19_mobile_app/lib/screens/searchCountry.dart
Normal file
165
covid19_mobile_app/lib/screens/searchCountry.dart
Normal file
@@ -0,0 +1,165 @@
|
||||
import 'package:covid19_mobile_app/widgets/foldable_cell_widgets.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:folding_cell/folding_cell.dart';
|
||||
|
||||
class CountrySearchDelegate extends SearchDelegate {
|
||||
List<dynamic> results;
|
||||
CountrySearchDelegate(this.results);
|
||||
|
||||
// to put the 'Clear Icon(X)' in the traling part of the search text field
|
||||
// to clear the contents of the text field
|
||||
@override
|
||||
List<Widget> buildActions(BuildContext context) {
|
||||
return [
|
||||
IconButton(
|
||||
icon: Icon(Icons.clear),
|
||||
onPressed: () {
|
||||
query = '';
|
||||
},
|
||||
)
|
||||
];
|
||||
}
|
||||
|
||||
// to put the 'Back icon(<-)' in the leading part of the search text field
|
||||
// to go back
|
||||
@override
|
||||
Widget buildLeading(BuildContext context) {
|
||||
return IconButton(
|
||||
icon: Icon(Icons.arrow_back),
|
||||
onPressed: () {
|
||||
Navigator.pop(context);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
// to build results according to the query
|
||||
// it is when the user types something and presses enter
|
||||
@override
|
||||
Widget buildResults(BuildContext context) {
|
||||
final suggestionsList = results
|
||||
.where((element) => element['country']
|
||||
.toString()
|
||||
.toLowerCase()
|
||||
.contains(query.toLowerCase()))
|
||||
.toList();
|
||||
return ListView.builder(
|
||||
physics: BouncingScrollPhysics(),
|
||||
itemCount: suggestionsList.length,
|
||||
itemBuilder: (context, index) {
|
||||
var _foldingCellKey = GlobalKey<SimpleFoldingCellState>();
|
||||
return Container(
|
||||
color: Color(0xFF2e282a),
|
||||
alignment: Alignment.topCenter,
|
||||
child: SimpleFoldingCell(
|
||||
key: _foldingCellKey,
|
||||
frontWidget: Builder(
|
||||
builder: (BuildContext context) {
|
||||
return GestureDetector(
|
||||
onTap: () {
|
||||
SimpleFoldingCellState foldingCellState =
|
||||
context.findAncestorStateOfType();
|
||||
foldingCellState?.toggleFold();
|
||||
},
|
||||
child: Container(
|
||||
color: Color(0xff000000),
|
||||
child: buildFrontWidget(
|
||||
context,
|
||||
suggestionsList[index]['countryInfo']['flag'],
|
||||
suggestionsList[index]['country'],
|
||||
suggestionsList[index]['cases'].toString())),
|
||||
);
|
||||
},
|
||||
),
|
||||
innerTopWidget: buildInnerTopWidget(
|
||||
suggestionsList[index]['country'],
|
||||
suggestionsList[index]['todayCases'].toString(),
|
||||
suggestionsList[index]['deaths'].toString(),
|
||||
suggestionsList[index]['todayDeaths'].toString(),
|
||||
suggestionsList[index]['recovered'].toString(),
|
||||
suggestionsList[index]['critical'].toString(),
|
||||
suggestionsList[index]['casesPerOneMillion'].toString(),
|
||||
),
|
||||
innerBottomWidget: buildInnerBottomWidget(
|
||||
suggestionsList[index]['cases'].toString()),
|
||||
cellSize: Size(MediaQuery.of(context).size.width, 125),
|
||||
padding: EdgeInsets.all(15),
|
||||
animationDuration: Duration(milliseconds: 300),
|
||||
borderRadius: 10,
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
// to set a placeholder for the search text field
|
||||
@override
|
||||
String get searchFieldLabel => 'Search Country';
|
||||
|
||||
// to set the theme for search bar
|
||||
@override
|
||||
ThemeData appBarTheme(BuildContext context) {
|
||||
return ThemeData(
|
||||
primaryColor: Colors.black,
|
||||
textTheme: Theme.of(context).textTheme.apply(),
|
||||
);
|
||||
}
|
||||
|
||||
// to build suggestions according to the query
|
||||
// it is when the user types something but does not press enter
|
||||
@override
|
||||
Widget buildSuggestions(BuildContext context) {
|
||||
final suggestionsList = results
|
||||
.where((element) => element['country']
|
||||
.toString()
|
||||
.toLowerCase()
|
||||
.contains(query.toLowerCase()))
|
||||
.toList();
|
||||
return ListView.builder(
|
||||
physics: BouncingScrollPhysics(),
|
||||
itemCount: suggestionsList.length,
|
||||
itemBuilder: (context, index) {
|
||||
var _foldingCellKey = GlobalKey<SimpleFoldingCellState>();
|
||||
return Container(
|
||||
color: Color(0xFF2e282a),
|
||||
alignment: Alignment.topCenter,
|
||||
child: SimpleFoldingCell(
|
||||
key: _foldingCellKey,
|
||||
frontWidget: Builder(
|
||||
builder: (BuildContext context) {
|
||||
return GestureDetector(
|
||||
onTap: () {
|
||||
SimpleFoldingCellState foldingCellState =
|
||||
context.findAncestorStateOfType();
|
||||
foldingCellState?.toggleFold();
|
||||
},
|
||||
child: Container(
|
||||
color: Color(0xff000000),
|
||||
child: buildFrontWidget(
|
||||
context,
|
||||
suggestionsList[index]['countryInfo']['flag'],
|
||||
suggestionsList[index]['country'],
|
||||
suggestionsList[index]['cases'].toString())),
|
||||
);
|
||||
},
|
||||
),
|
||||
innerTopWidget: buildInnerTopWidget(
|
||||
suggestionsList[index]['country'],
|
||||
suggestionsList[index]['todayCases'].toString(),
|
||||
suggestionsList[index]['deaths'].toString(),
|
||||
suggestionsList[index]['todayDeaths'].toString(),
|
||||
suggestionsList[index]['recovered'].toString(),
|
||||
suggestionsList[index]['critical'].toString(),
|
||||
suggestionsList[index]['casesPerOneMillion'].toString(),
|
||||
),
|
||||
innerBottomWidget: buildInnerBottomWidget(
|
||||
suggestionsList[index]['cases'].toString()),
|
||||
cellSize: Size(MediaQuery.of(context).size.width, 125),
|
||||
padding: EdgeInsets.all(15),
|
||||
animationDuration: Duration(milliseconds: 300),
|
||||
borderRadius: 10,
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user