Skip to content

Commit

Permalink
Merge pull request #23 from NKUST-ITC/develop
Browse files Browse the repository at this point in the history
Fix flutter error
  • Loading branch information
abc873693 authored Jan 22, 2019
2 parents 2b1bc0e + 4c6c6ef commit 67119c1
Showing 1 changed file with 70 additions and 59 deletions.
129 changes: 70 additions & 59 deletions lib/pages/home/bus/bus_reserve_page.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:nkust_ap/models/models.dart';
import 'package:nkust_ap/res/resource.dart' as Resource;
import 'package:nkust_ap/utils/global.dart';
import 'package:nkust_ap/widgets/flutter_calendar.dart';
import 'package:flutter/cupertino.dart';
import 'package:nkust_ap/res/resource.dart' as Resource;
import 'package:nkust_ap/models/models.dart';
import 'package:nkust_ap/widgets/hint_content.dart';
import 'package:nkust_ap/widgets/progress_dialog.dart';

Expand Down Expand Up @@ -34,7 +34,6 @@ class BusReservePageState extends State<BusReservePage> {

_State state = _State.loading;
BusData busData;
List<Widget> busTimeWeights = [];

Station selectStartStation = Station.janGong;
DateTime dateTime = DateTime.now();
Expand Down Expand Up @@ -75,11 +74,24 @@ class BusReservePageState extends State<BusReservePage> {
default:
return ListView(
physics: const NeverScrollableScrollPhysics(),
children: busTimeWeights,
children: _renderBusTimeWidgets(),
);
}
}

_renderBusTimeWidgets() {
List<Widget> list = [];
if (busData != null) {
for (var i in busData.timetable) {
if (selectStartStation == Station.janGong && i.endStation == "燕巢")
list.add(_busTimeWidget(i));
else if (selectStartStation == Station.yanchao && i.endStation == "建工")
list.add(_busTimeWidget(i));
}
}
return list;
}

_busTimeWidget(BusTime busTime) => Column(
children: <Widget>[
FlatButton(
Expand Down Expand Up @@ -251,11 +263,11 @@ class BusReservePageState extends State<BusReservePage> {
)
},
onValueChanged: (Station text) {
selectStartStation = text;
if (state == _State.finish)
_updateBusTimeTables();
else
setState(() {});
if (mounted) {
setState(() {
selectStartStation = text;
});
}
},
),
),
Expand All @@ -273,49 +285,63 @@ class BusReservePageState extends State<BusReservePage> {
_getBusTimeTables() {
Helper.cancelToken.cancel("");
Helper.cancelToken = CancelToken();
state = _State.loading;
setState(() {});
if (mounted) {
setState(() {
state = _State.loading;
});
}
Helper.instance.getBusTimeTables(dateTime).then((response) {
busData = response;
_updateBusTimeTables();
if (mounted) {
setState(() {
if (busData.timetable.length != 0)
state = _State.finish;
else
state = _State.empty;
});
}
}).catchError((e) {
assert(e is DioError);
DioError dioError = e as DioError;
//if bus can't connection:
// dioError.message = HttpException: Connection closed before full header was received
switch (dioError.type) {
case DioErrorType.RESPONSE:
Utils.showToast(app.tokenExpiredContent);
Navigator.popUntil(
context, ModalRoute.withName(Navigator.defaultRouteName));
break;
case DioErrorType.DEFAULT:
if (dioError.message.contains("HttpException")) {
if (e is DioError) {
DioError dioError = e;
//if bus can't connection:
// dioError.message = HttpException: Connection closed before full header was received
switch (dioError.type) {
case DioErrorType.RESPONSE:
Utils.showToast(app.tokenExpiredContent);
Navigator.popUntil(
context, ModalRoute.withName(Navigator.defaultRouteName));
break;
case DioErrorType.DEFAULT:
if (dioError.message.contains("HttpException")) {
if (mounted) {
setState(() {
state = _State.error;
Utils.showToast(app.busFailInfinity);
});
}
}
break;
case DioErrorType.CANCEL:
break;
default:
if (mounted) {
setState(() {
state = _State.error;
Utils.showToast(app.busFailInfinity);
Utils.handleDioError(dioError, app);
});
}
}
break;
case DioErrorType.CANCEL:
break;
default:
setState(() {
state = _State.error;
Utils.handleDioError(dioError, app);
});
break;
break;
}
} else {
throw (e);
}
});
}

_bookingBus(BusTime busTime) {
showDialog(
context: context,
builder: (BuildContext context) =>
ProgressDialog(app.reserving),
builder: (BuildContext context) => ProgressDialog(app.reserving),
barrierDismissible: true);
Helper.instance.bookingBusReservation(busTime.busId).then((response) {
//TODO 優化成物件
Expand Down Expand Up @@ -344,10 +370,12 @@ class BusReservePageState extends State<BusReservePage> {
break;
case DioErrorType.DEFAULT:
if (dioError.message.contains("HttpException")) {
setState(() {
state = _State.error;
Utils.showToast(app.busFailInfinity);
});
if (mounted) {
setState(() {
state = _State.error;
Utils.showToast(app.busFailInfinity);
});
}
}
break;
case DioErrorType.CANCEL:
Expand All @@ -358,21 +386,4 @@ class BusReservePageState extends State<BusReservePage> {
}
});
}

_updateBusTimeTables() {
busTimeWeights = [];
if (busData != null) {
for (var i in busData.timetable) {
if (selectStartStation == Station.janGong && i.endStation == "燕巢")
busTimeWeights.add(_busTimeWidget(i));
else if (selectStartStation == Station.yanchao && i.endStation == "建工")
busTimeWeights.add(_busTimeWidget(i));
}
if (busData.timetable.length != 0)
state = _State.finish;
else
state = _State.empty;
setState(() {});
}
}
}

0 comments on commit 67119c1

Please sign in to comment.