Skip to content

Commit

Permalink
Main widget
Browse files Browse the repository at this point in the history
Co-authored-by: Simone Sestito <[email protected]>
  • Loading branch information
m-i-n-a-r and Simone Sestito committed Oct 21, 2018
1 parent 8747b2a commit 76c90c7
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 12 deletions.
Binary file added assets/img/ic_exos_logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added flutter_01.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
104 changes: 101 additions & 3 deletions lib/route/home.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import 'package:escape_earth/bottom_home.dart';
import 'package:escape_earth/route/collection.dart';
import 'package:escape_earth/route/qa.dart';
import 'package:escape_earth/service/RocketService.dart';
import 'package:escape_earth/view/NewsView.dart';
import 'package:escape_earth/view/RocketHero.dart';
import 'package:escape_earth/view/RocketView.dart';
import 'package:flutter/material.dart';

class Home extends StatefulWidget {
Expand All @@ -26,7 +28,7 @@ class HomeState extends State<Home> {
children: <Widget>[
Padding(
padding: EdgeInsets.only(bottom: BOTTOM_BAR_HEIGHT),
child: currentFragment,
child: SafeArea(child: currentFragment),
),
Align(
alignment: Alignment.bottomCenter,
Expand Down Expand Up @@ -75,7 +77,103 @@ class HomeState extends State<Home> {
class HomeBody extends StatelessWidget {
@override
Widget build(BuildContext context) {
// TODO: implement HomeBody
return Container();
return FutureBuilder(
future: RocketService.getNextLaunch(),
builder: (context, snap) {
if (snap.data == null) {
return Center(
child: CircularProgressIndicator(),
);
}

if (snap.error != null) {
print(snap.error);
return Center(child: Icon(Icons.portable_wifi_off));
}

return Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Padding(
padding: EdgeInsets.only(top: 30.0),
child: Image.asset("assets/img/ic_exos_logo.png", width: 250.0),
),
Expanded(
child: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Text("NEXT LAUNCH IN:", style: TextStyle(fontSize: 18.0)),
Padding(padding: EdgeInsets.all(10.0)),
Counter(
until: DateTime.parse(snap.data.date),
),
],
))),
Padding(
padding: EdgeInsets.only(
left: 16.0, right: 16.0, bottom: 70.0, top: 16.0),
child: RocketView(launch: snap.data),
),
],
);
},
);
}
}

class Counter extends StatefulWidget {
final DateTime until;

Counter({
Key key,
@required this.until,
}) : super(key: key);

@override
CounterState createState() => CounterState();
}

class CounterState extends State<Counter> {
bool isRunning;

@override
void initState() {
super.initState();
isRunning = true;
_run();
}

void _run() async {
while (isRunning) {
await Future.delayed(const Duration(seconds: 1));
setState(() {});
}
}

@override
Widget build(BuildContext context) {
return Text(
_calculateString(),
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 43.0),
);
}

String _calculateString() {
final now = DateTime.now().millisecondsSinceEpoch;
final diff = widget.until.millisecondsSinceEpoch - now;

final day = diff ~/ 86400000;
final hours = diff ~/ 3600000 % 24;
final minutes = diff ~/ 60000 % 60;
final seconds = diff ~/ 1000 % 60;

return "${day}d ${hours}h ${minutes}m ${seconds}s";
}

@override
void dispose() {
isRunning = false;
super.dispose();
}
}
15 changes: 14 additions & 1 deletion lib/service/RocketService.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,19 @@ import 'package:escape_earth/model/RocketLaunch.dart';
import 'package:http/http.dart' as http;

class RocketService {
static Future<RocketLaunch> getNextLaunch() async {
final response = await http.get("https://launchlibrary.net/1.4/launch?next=1");
final content = json.decode(response.body)["launches"][0];
final agency = await getAgencyById(content["lsp"]);
return RocketLaunch(
country: agency.countryCode,
launchCompany: agency,
name: content["name"],
date: content["windowstart"],
videoUrl: (content["vidURLs"] ?? [null])[0],
);
}

static Future<List<RocketLaunch>> getLaunches({ String query }) async {
http.Response rawLaunchesResponse;
if (query == null || query == "") {
Expand All @@ -29,7 +42,7 @@ class RocketService {
launchCompany: allAgencies[i],
name: launchesContent[i]["name"],
date: launchesContent[i]["windowstart"],
videoUrl: launchesContent[i]["vidURLs"] == null ? null : launchesContent[i]["vidURLs"][0],
videoUrl: (launchesContent[i]["vidURLs"] ?? [null])[0],
));
}
return result;
Expand Down
16 changes: 8 additions & 8 deletions lib/view/NewsView.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ import 'package:escape_earth/localdata.dart';
class NewsList extends StatelessWidget {
@override
Widget build(BuildContext context) {
return SafeArea(
child: ListView.builder(
itemCount: latestNews.length,
padding: EdgeInsets.only(left: 16.0, right: 16.0),
itemBuilder: (context, i) => NewsItem(news: latestNews[i])),
);
return ListView.builder(
itemCount: latestNews.length,
padding: EdgeInsets.only(left: 16.0, right: 16.0),
itemBuilder: (context, i) => NewsItem(news: latestNews[i]));
}
}

Expand Down Expand Up @@ -72,7 +70,8 @@ class NewsItem extends StatelessWidget {
children: <Widget>[
Icon(Icons.calendar_today, color: Colors.black),
Padding(padding: EdgeInsets.all(2.5)),
Text(_dateToCalendarString(news.date), style: TextStyle(color: Colors.black)),
Text(_dateToCalendarString(news.date),
style: TextStyle(color: Colors.black)),
]),
),
],
Expand All @@ -83,5 +82,6 @@ class NewsItem extends StatelessWidget {
);
}

String _dateToCalendarString(DateTime date) => "${date.month}/${date.day}/${date.year}";
String _dateToCalendarString(DateTime date) =>
"${date.month}/${date.day}/${date.year}";
}

0 comments on commit 76c90c7

Please sign in to comment.