From 405acf1eb4da21a624b244aaadc5ea9dd95ed918 Mon Sep 17 00:00:00 2001 From: laiiihz Date: Mon, 8 Feb 2021 11:48:07 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E9=A6=96=E9=A1=B5=E5=85=AC=E5=91=8A?= =?UTF-8?q?=E6=BB=9A=E5=8A=A8=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/ui/home/home_notification.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ui/home/home_notification.dart b/lib/ui/home/home_notification.dart index dd558691..218ceef5 100644 --- a/lib/ui/home/home_notification.dart +++ b/lib/ui/home/home_notification.dart @@ -70,7 +70,7 @@ class _HomeNotificationState extends State { duration: Duration(milliseconds: 1000), child: Align( alignment: Alignment.centerLeft, - key: ValueKey(randomItem?.id ?? 0), + key: ObjectKey(randomItem), child: Text(randomItem?.title ?? ''), ), ), From 44b3364c03b03cfd30f9ba7464cdbbcaba4f9750 Mon Sep 17 00:00:00 2001 From: laiiihz Date: Mon, 8 Feb 2021 13:34:06 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=A4=A9=E6=B0=94?= =?UTF-8?q?=E9=A2=84=E6=8A=A5=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/model/common/real_time_weather_model.dart | 379 ++++++++++++++++++ lib/pages/home/widget/animate_app_bar.dart | 8 +- lib/pages/splash/splash_page.dart | 4 +- lib/provider/app_provider.dart | 66 +++ lib/utils/weather/weather_util.dart | 18 + 5 files changed, 472 insertions(+), 3 deletions(-) create mode 100644 lib/model/common/real_time_weather_model.dart create mode 100644 lib/utils/weather/weather_util.dart diff --git a/lib/model/common/real_time_weather_model.dart b/lib/model/common/real_time_weather_model.dart new file mode 100644 index 00000000..97ac7810 --- /dev/null +++ b/lib/model/common/real_time_weather_model.dart @@ -0,0 +1,379 @@ +class RealTimeWeatherModel { + String status; + String apiVersion; + String apiStatus; + String lang; + String unit; + num tzshift; + String timezone; + num serverTime; + List location; + Result result; + + RealTimeWeatherModel( + {this.status, + this.apiVersion, + this.apiStatus, + this.lang, + this.unit, + this.tzshift, + this.timezone, + this.serverTime, + this.location, + this.result}); + + RealTimeWeatherModel.fromJson(Map json) { + status = json['status']; + apiVersion = json['api_version']; + apiStatus = json['api_status']; + lang = json['lang']; + unit = json['unit']; + tzshift = json['tzshift']; + timezone = json['timezone']; + serverTime = json['server_time']; + location = json['location'].cast(); + result = + json['result'] != null ? new Result.fromJson(json['result']) : null; + } + + Map toJson() { + final Map data = new Map(); + data['status'] = this.status; + data['api_version'] = this.apiVersion; + data['api_status'] = this.apiStatus; + data['lang'] = this.lang; + data['unit'] = this.unit; + data['tzshift'] = this.tzshift; + data['timezone'] = this.timezone; + data['server_time'] = this.serverTime; + data['location'] = this.location; + if (this.result != null) { + data['result'] = this.result.toJson(); + } + return data; + } +} + +class Result { + Realtime realtime; + num primary; + + Result({this.realtime, this.primary}); + + Result.fromJson(Map json) { + realtime = json['realtime'] != null + ? new Realtime.fromJson(json['realtime']) + : null; + primary = json['primary']; + } + + Map toJson() { + final Map data = new Map(); + if (this.realtime != null) { + data['realtime'] = this.realtime.toJson(); + } + data['primary'] = this.primary; + return data; + } +} + +class Realtime { + String status; + num temperature; + num humidity; + num cloudrate; + String skycon; + num visibility; + num dswrf; + Wind wind; + num pressure; + num apparentTemperature; + Precipitation precipitation; + AirQuality airQuality; + LifeIndex lifeIndex; + + Realtime( + {this.status, + this.temperature, + this.humidity, + this.cloudrate, + this.skycon, + this.visibility, + this.dswrf, + this.wind, + this.pressure, + this.apparentTemperature, + this.precipitation, + this.airQuality, + this.lifeIndex}); + + Realtime.fromJson(Map json) { + status = json['status']; + temperature = json['temperature']; + humidity = json['humidity']; + cloudrate = json['cloudrate']; + skycon = json['skycon']; + visibility = json['visibility']; + dswrf = json['dswrf']; + wind = json['wind'] != null ? new Wind.fromJson(json['wind']) : null; + pressure = json['pressure']; + apparentTemperature = json['apparent_temperature']; + precipitation = json['precipitation'] != null + ? new Precipitation.fromJson(json['precipitation']) + : null; + airQuality = json['air_quality'] != null + ? new AirQuality.fromJson(json['air_quality']) + : null; + lifeIndex = json['life_index'] != null + ? new LifeIndex.fromJson(json['life_index']) + : null; + } + + Map toJson() { + final Map data = new Map(); + data['status'] = this.status; + data['temperature'] = this.temperature; + data['humidity'] = this.humidity; + data['cloudrate'] = this.cloudrate; + data['skycon'] = this.skycon; + data['visibility'] = this.visibility; + data['dswrf'] = this.dswrf; + if (this.wind != null) { + data['wind'] = this.wind.toJson(); + } + data['pressure'] = this.pressure; + data['apparent_temperature'] = this.apparentTemperature; + if (this.precipitation != null) { + data['precipitation'] = this.precipitation.toJson(); + } + if (this.airQuality != null) { + data['air_quality'] = this.airQuality.toJson(); + } + if (this.lifeIndex != null) { + data['life_index'] = this.lifeIndex.toJson(); + } + return data; + } +} + +class Wind { + num speed; + num direction; + + Wind({this.speed, this.direction}); + + Wind.fromJson(Map json) { + speed = json['speed']; + direction = json['direction']; + } + + Map toJson() { + final Map data = new Map(); + data['speed'] = this.speed; + data['direction'] = this.direction; + return data; + } +} + +class Precipitation { + Local local; + Nearest nearest; + + Precipitation({this.local, this.nearest}); + + Precipitation.fromJson(Map json) { + local = json['local'] != null ? new Local.fromJson(json['local']) : null; + nearest = + json['nearest'] != null ? new Nearest.fromJson(json['nearest']) : null; + } + + Map toJson() { + final Map data = new Map(); + if (this.local != null) { + data['local'] = this.local.toJson(); + } + if (this.nearest != null) { + data['nearest'] = this.nearest.toJson(); + } + return data; + } +} + +class Local { + String status; + String datasource; + num numensity; + + Local({this.status, this.datasource, this.numensity}); + + Local.fromJson(Map json) { + status = json['status']; + datasource = json['datasource']; + numensity = json['numensity']; + } + + Map toJson() { + final Map data = new Map(); + data['status'] = this.status; + data['datasource'] = this.datasource; + data['numensity'] = this.numensity; + return data; + } +} + +class Nearest { + String status; + num distance; + num numensity; + + Nearest({this.status, this.distance, this.numensity}); + + Nearest.fromJson(Map json) { + status = json['status']; + distance = json['distance']; + numensity = json['numensity']; + } + + Map toJson() { + final Map data = new Map(); + data['status'] = this.status; + data['distance'] = this.distance; + data['numensity'] = this.numensity; + return data; + } +} + +class AirQuality { + num pm25; + num pm10; + num o3; + num so2; + num no2; + num co; + Aqi aqi; + Description description; + + AirQuality( + {this.pm25, + this.pm10, + this.o3, + this.so2, + this.no2, + this.co, + this.aqi, + this.description}); + + AirQuality.fromJson(Map json) { + pm25 = json['pm25']; + pm10 = json['pm10']; + o3 = json['o3']; + so2 = json['so2']; + no2 = json['no2']; + co = json['co']; + aqi = json['aqi'] != null ? new Aqi.fromJson(json['aqi']) : null; + description = json['description'] != null + ? new Description.fromJson(json['description']) + : null; + } + + Map toJson() { + final Map data = new Map(); + data['pm25'] = this.pm25; + data['pm10'] = this.pm10; + data['o3'] = this.o3; + data['so2'] = this.so2; + data['no2'] = this.no2; + data['co'] = this.co; + if (this.aqi != null) { + data['aqi'] = this.aqi.toJson(); + } + if (this.description != null) { + data['description'] = this.description.toJson(); + } + return data; + } +} + +class Aqi { + num chn; + num usa; + + Aqi({this.chn, this.usa}); + + Aqi.fromJson(Map json) { + chn = json['chn']; + usa = json['usa']; + } + + Map toJson() { + final Map data = new Map(); + data['chn'] = this.chn; + data['usa'] = this.usa; + return data; + } +} + +class Description { + String usa; + String chn; + + Description({this.usa, this.chn}); + + Description.fromJson(Map json) { + usa = json['usa']; + chn = json['chn']; + } + + Map toJson() { + final Map data = new Map(); + data['usa'] = this.usa; + data['chn'] = this.chn; + return data; + } +} + +class LifeIndex { + Ultraviolet ultraviolet; + Ultraviolet comfort; + + LifeIndex({this.ultraviolet, this.comfort}); + + LifeIndex.fromJson(Map json) { + ultraviolet = json['ultraviolet'] != null + ? new Ultraviolet.fromJson(json['ultraviolet']) + : null; + comfort = json['comfort'] != null + ? new Ultraviolet.fromJson(json['comfort']) + : null; + } + + Map toJson() { + final Map data = new Map(); + if (this.ultraviolet != null) { + data['ultraviolet'] = this.ultraviolet.toJson(); + } + if (this.comfort != null) { + data['comfort'] = this.comfort.toJson(); + } + return data; + } +} + +class Ultraviolet { + num index; + String desc; + + Ultraviolet({this.index, this.desc}); + + Ultraviolet.fromJson(Map json) { + index = json['index']; + desc = json['desc']; + } + + Map toJson() { + final Map data = new Map(); + data['index'] = this.index; + data['desc'] = this.desc; + return data; + } +} diff --git a/lib/pages/home/widget/animate_app_bar.dart b/lib/pages/home/widget/animate_app_bar.dart index 75ab1162..f3ebbd7e 100644 --- a/lib/pages/home/widget/animate_app_bar.dart +++ b/lib/pages/home/widget/animate_app_bar.dart @@ -1,8 +1,10 @@ // Flutter imports: +import 'package:akuCommunity/provider/app_provider.dart'; import 'package:flutter/material.dart'; // Package imports: import 'package:flutter_screenutil/flutter_screenutil.dart'; +import 'package:provider/provider.dart'; import 'package:velocity_x/velocity_x.dart'; class AnimateAppBar extends StatefulWidget with PreferredSizeWidget { @@ -37,6 +39,7 @@ class _AnimateAppBarState extends State { @override Widget build(BuildContext context) { + final appProvider = Provider.of(context); return AppBar( title: 'TEST'.text.make(), backgroundColor: _bgColor, @@ -49,7 +52,7 @@ class _AnimateAppBarState extends State { crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( - '深圳', + appProvider?.location?.city ?? '', style: TextStyle( fontWeight: FontWeight.w600, fontSize: 24.sp, @@ -57,7 +60,8 @@ class _AnimateAppBarState extends State { ), ), Text( - '阴 27℃', + '${appProvider.weatherType} ${appProvider.weatherTemp}℃', + // '阴 27℃', style: TextStyle( fontSize: 20.sp, color: Color(0xff333333), diff --git a/lib/pages/splash/splash_page.dart b/lib/pages/splash/splash_page.dart index 9f5287a9..12843717 100644 --- a/lib/pages/splash/splash_page.dart +++ b/lib/pages/splash/splash_page.dart @@ -1,4 +1,5 @@ // Flutter imports: +import 'package:akuCommunity/utils/weather/weather_util.dart'; import 'package:flutter/material.dart'; // Package imports: @@ -33,7 +34,7 @@ class _SplashPageState extends State { await HiveStore.init(); //初始化AMap - AmapLocation.instance.init(iosKey: 'ios key'); + await AmapLocation.instance.init(iosKey: 'ios key'); } Future _initOp() async { @@ -42,6 +43,7 @@ class _SplashPageState extends State { final userProvider = Provider.of(context, listen: false); final appProvider = Provider.of(context, listen: false); appProvider.initApplications(); + appProvider.getWeather(); //app init delay 2 second await Future.delayed(Duration(seconds: 2)); if (HiveStore.appBox.get('login') ?? false) { diff --git a/lib/provider/app_provider.dart b/lib/provider/app_provider.dart index 88b190be..6f7617e2 100644 --- a/lib/provider/app_provider.dart +++ b/lib/provider/app_provider.dart @@ -1,4 +1,7 @@ // Flutter imports: +import 'package:akuCommunity/model/common/real_time_weather_model.dart'; +import 'package:amap_location_fluttify/amap_location_fluttify.dart'; +import 'package:dio/dio.dart'; import 'package:flutter/material.dart'; // Project imports: @@ -70,4 +73,67 @@ class AppProvider extends ChangeNotifier { (model.data as List).map((e) => HotTopicModel.fromJson(e)).toList(); notifyListeners(); } + + RealTimeWeatherModel _weatherModel; + RealTimeWeatherModel get weatherModel => _weatherModel; + + String get weatherTemp => + _weatherModel?.result?.realtime?.temperature?.toStringAsFixed(0) ?? ''; + + String get weatherType { + if (_weatherModel?.result?.realtime?.skycon == null) return ''; + switch (_weatherModel.result.realtime.skycon) { + case 'CLEAR_DAY': + case 'CLEAR_NIGHT': + return '晴'; + case 'PARTLY_CLOUDY_DAY': + case 'PARTLY_CLOUDY_NIGHT': + return '多云'; + case 'CLOUDY': + return '阴'; + case 'LIGHT_HAZE': + return '轻度雾霾'; + case 'MODERATE_HAZE': + return '中度雾霾'; + case 'HEAVY_HAZE': + return '重度雾霾'; + case 'LIGHT_RAIN': + return '小雨'; + case 'MODERATE_RAIN': + return '中雨'; + case 'HEAVY_RAIN': + return '大雨'; + case 'STORM_RAIN': + return '暴雨'; + case 'FOG': + return '雾'; + case 'LIGHT_SNOW': + return '小雪'; + case 'MODERATE_SNOW': + return '中雪'; + case 'HEAVY_SNOW': + return '大雪'; + case 'STORM_SNOW': + return '暴雪'; + case 'DUST': + return '浮尘'; + case 'SAND': + return '沙尘'; + case 'WIND': + return '大风'; + default: + return ''; + } + } + + Location _location; + Location get location => _location; + getWeather() async { + _location = await AmapLocation.instance.fetchLocation(); + Response response = await Dio().get( + 'https://api.caiyunapp.com/v2.5/Rl2lmppO9q15q8W6/${_location.latLng.longitude},${_location.latLng.latitude}/realtime.json', + ); + _weatherModel = RealTimeWeatherModel.fromJson(response.data); + notifyListeners(); + } } diff --git a/lib/utils/weather/weather_util.dart b/lib/utils/weather/weather_util.dart new file mode 100644 index 00000000..8dae4300 --- /dev/null +++ b/lib/utils/weather/weather_util.dart @@ -0,0 +1,18 @@ +import 'package:akuCommunity/model/common/real_time_weather_model.dart'; +import 'package:amap_map_fluttify/amap_map_fluttify.dart'; +import 'package:dio/dio.dart'; + +class WeatherUtil { + static Location _location; + static String baseURL = 'https://api.caiyunapp.com/v2.5/Rl2lmppO9q15q8W6'; + + static Future getWeather() async { + _location = await AmapLocation.instance.fetchLocation(); + Response response = await Dio().get( + '$baseURL/${_location.latLng.latitude},${_location.latLng.longitude}/realtime.json'); + if (response.data == null) + return null; + else + return RealTimeWeatherModel.fromJson(response.data); + } +}