Created
February 13, 2019 11:15
-
-
Save fishyer/0abb449bddf7f0e696d0952e2d79d649 to your computer and use it in GitHub Desktop.
ScopedModel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'package:scoped_model/scoped_model.dart'; | |
import 'package:dio/dio.dart'; | |
import 'dart:convert'; | |
class HttpPage extends StatefulWidget { | |
@override | |
_HttpPageState createState() => _HttpPageState(); | |
} | |
class HttpModel extends Model { | |
static HttpModel of(BuildContext context) => | |
ScopedModel.of<HttpModel>(context, rebuildOnChange: true); | |
String url = | |
"http://co-api.51wnl.com/calendar/details?token=CD78D9012F1C063E54C640EA27952F80×tamp=1462377600&client=ceshi"; | |
BaseResponse baseResponse = BaseResponse.empty(); | |
CalendarData weather = CalendarData.empty(); | |
void fetchData() async { | |
Response response = await Dio().get(url); | |
if (response.statusCode == 200) { | |
String data = response.data; | |
baseResponse = BaseResponse.fromJson(json.decode(data)); | |
weather = baseResponse.data[0]; | |
} else { | |
baseResponse = BaseResponse.empty(); | |
} | |
notifyListeners(); | |
} | |
} | |
class _HttpPageState extends State<HttpPage> { | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar(title: Text("HttpPage")), | |
body: new Column( | |
mainAxisAlignment: MainAxisAlignment.start, | |
crossAxisAlignment: CrossAxisAlignment.center, | |
children: <Widget>[ | |
new Container( | |
width: double.infinity, | |
margin: EdgeInsets.only(top: 40.0), | |
child: new Text( | |
"万年历", | |
textAlign: TextAlign.center, | |
style: new TextStyle( | |
color: Colors.black, | |
fontSize: 30.0, | |
), | |
), | |
), | |
new Container( | |
width: double.infinity, | |
margin: EdgeInsets.only(top: 100.0), | |
child: ScopedModel<HttpModel>( | |
model: HttpModel(), | |
child: ScopedModelDescendant<HttpModel>(builder: (context, child, model) { | |
model.fetchData(); | |
return new Column( | |
children: <Widget>[ | |
new Text(model.weather?.datekey, | |
style: new TextStyle( | |
color: Colors.black, fontSize: 30.0)), | |
new Text(model.weather?.weeks, | |
style: new TextStyle( | |
color: Colors.black, fontSize: 20.0)), | |
new Text(model.weather?.tgdz, | |
style: new TextStyle( | |
color: Colors.black, fontSize: 25.0)), | |
new Text( | |
model.weather?.constellation, | |
style: new TextStyle(color: Colors.black, fontSize: 30.0), | |
) | |
], | |
); | |
}), | |
), | |
) | |
], | |
), | |
); | |
} | |
} | |
class BaseResponse { | |
int status; | |
String errMsg; | |
List<CalendarData> data; | |
BaseResponse({this.status, this.errMsg, this.data}); | |
factory BaseResponse.fromJson(Map<String, dynamic> json) { | |
var list = json['data'] as List; | |
print(list.runtimeType); | |
List<CalendarData> calendarList = | |
list.map((i) => CalendarData.fromJson(i)).toList(); | |
return BaseResponse( | |
status: json['status'], errMsg: json['errMsg'], data: calendarList); | |
} | |
static BaseResponse empty() { | |
return BaseResponse(); | |
} | |
} | |
class CalendarData { | |
String datekey; //2016/5/5 0:00:00 | |
String tgdz; //丙申年 [猴] 癸巳月 丁亥日 | |
String weekDay; //Thu | |
String lunar; //三月廿九 | |
String weeks; //周四 第19周 | |
String constellation; //金牛座 | |
CalendarData(this.datekey, this.tgdz, this.weekDay, this.lunar, this.weeks, | |
this.constellation); | |
CalendarData.fromJson(Map<String, dynamic> json) | |
: datekey = json['datekey'], | |
tgdz = json['tgdz'], | |
weekDay = json['weekDay'], | |
lunar = json['lunar'], | |
weeks = json['weeks'], | |
constellation = json['constellation']; | |
Map<String, dynamic> toJson() => { | |
'datekey': datekey, | |
'tgdz': tgdz, | |
'weekDay': weekDay, | |
'lunar': lunar, | |
'weeks': weeks, | |
'constellation': constellation, | |
}; | |
static CalendarData empty() { | |
return new CalendarData("日期", "天干地支", "星期", "农历日期", "周数", "星座"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment