Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions lib/core/api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,24 @@ Future<dynamic> get(String uri) async {
}
}

Future<dynamic> post(String uri, Map<String, dynamic> body) async {
try {
final headers = {'Content-Type': 'application/json'};
String jsonBody = json.encode(body);
final encoding = Encoding.getByName('utf-8');
var response =
await http.post(Uri.parse(uri), headers: headers, body: jsonBody, encoding: encoding).timeout(const Duration(seconds: 2));
print(response.statusCode);
if (response.statusCode == 200) {
return jsonDecode(response.body);
} else {
return false;
}
} catch (err) {
return false;
}
}

String formatNumber(int number) {
return number.toString().padLeft(2, '0');
}
Expand Down
33 changes: 19 additions & 14 deletions lib/view/earthquake.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class EarthquakePage extends StatefulWidget {
_EarthquakePage createState() => _EarthquakePage();
}

var data;

class _EarthquakePage extends State<EarthquakePage> {
String url = 'https://exptech.com.tw/api/v1/trem/rts-image';
late Widget _pic = Image.network(
Expand All @@ -27,7 +29,6 @@ class _EarthquakePage extends State<EarthquakePage> {
int _page = 0;
List<Widget> _List_children = <Widget>[];
String reports_url = "https://exptech.com.tw/api/v3/earthquake/reports";
var data;

@override
void initState() {
Expand Down Expand Up @@ -64,7 +65,7 @@ class _EarthquakePage extends State<EarthquakePage> {

_updateReportsWidget() async {
try {
data = await get(reports_url);
data ??= await post(reports_url, { "list" : {} });
} on TimeoutException catch (e) {
return;
} catch (e) {
Expand Down Expand Up @@ -111,18 +112,22 @@ class _EarthquakePage extends State<EarthquakePage> {
));
} else {
print(data);
_List_children.add(Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
"規模M " + data[0]["magnitudeValue"].toString(),
style: TextStyle(
fontSize: 32,
fontWeight: FontWeight.w100,
color: Colors.white),
)
],
));
if (data is! bool) {
for (var i = 0; i < data.length; i++) {
_List_children.add(Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
"規模M " + data[i]["magnitudeValue"].toStringAsFixed(1),
style: TextStyle(
fontSize: 32,
fontWeight: FontWeight.w100,
color: Colors.white),
)
],
));
}
}
}
}
});
Expand Down