|
| 1 | +import 'dart:math'; |
| 2 | +import 'dart:ui'; |
| 3 | + |
| 4 | +import 'package:flutter/material.dart'; |
| 5 | +import 'package:flutter_dynamic_weather/app/res/analytics_constant.dart'; |
| 6 | +import 'package:flutter_dynamic_weather/app/utils/ota_utils.dart'; |
| 7 | +import 'package:flutter_dynamic_weather/app/utils/print_utils.dart'; |
| 8 | +import 'package:flutter_dynamic_weather/views/common/wave_view.dart'; |
| 9 | +import 'package:flutter_weather_bg/bg/weather_bg.dart'; |
| 10 | +import 'package:flutter_weather_bg/flutter_weather_bg.dart'; |
| 11 | +import 'package:ota_update/ota_update.dart'; |
| 12 | +import 'package:umeng_analytics_plugin/umeng_analytics_plugin.dart'; |
| 13 | + |
| 14 | +var radius = 20.0; |
| 15 | +var width = 250.0; |
| 16 | +var height = 350.0; |
| 17 | +var weatherType = WeatherType.sunny; |
| 18 | + |
| 19 | +class OTADialog extends Dialog { |
| 20 | + final desc; |
| 21 | + final versionName; |
| 22 | + final apkUrl; |
| 23 | + |
| 24 | + OTADialog(this.desc, this.versionName, this.apkUrl, {Key key}) |
| 25 | + : super(key: key); |
| 26 | + |
| 27 | + @override |
| 28 | + Widget build(BuildContext context) { |
| 29 | + weatherType = |
| 30 | + WeatherType.values[Random().nextInt(WeatherType.values.length)]; |
| 31 | + return new Material( |
| 32 | + type: MaterialType.transparency, |
| 33 | + child: new Center( |
| 34 | + child: new SizedBox( |
| 35 | + width: width, |
| 36 | + height: height, |
| 37 | + child: ClipPath( |
| 38 | + clipper: ShapeBorderClipper( |
| 39 | + shape: RoundedRectangleBorder( |
| 40 | + borderRadius: BorderRadius.circular(radius))), |
| 41 | + child: Stack( |
| 42 | + children: [ |
| 43 | + WeatherBg( |
| 44 | + weatherType: weatherType, |
| 45 | + width: width, |
| 46 | + height: height, |
| 47 | + ), |
| 48 | + BackdropFilter( |
| 49 | + filter: ImageFilter.blur( |
| 50 | + sigmaX: 1, |
| 51 | + sigmaY: 1, |
| 52 | + ), |
| 53 | + child: Container( |
| 54 | + width: width, |
| 55 | + height: height, |
| 56 | + color: Colors.white.withAlpha(60), |
| 57 | + ), |
| 58 | + ), |
| 59 | + OTAContentWidget(desc, versionName, apkUrl), |
| 60 | + ], |
| 61 | + ), |
| 62 | + ), |
| 63 | + ), |
| 64 | + ), |
| 65 | + ); |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +class OTAContentWidget extends StatefulWidget { |
| 70 | + |
| 71 | + final desc; |
| 72 | + final versionName; |
| 73 | + final apkUrl; |
| 74 | + |
| 75 | + OTAContentWidget(this.desc, this.versionName, this.apkUrl, {Key key}) |
| 76 | + : super(key: key); |
| 77 | + |
| 78 | + @override |
| 79 | + _OTAContentWidgetState createState() => _OTAContentWidgetState(); |
| 80 | +} |
| 81 | + |
| 82 | +class _OTAContentWidgetState extends State<OTAContentWidget> { |
| 83 | + double progress = 0.0; |
| 84 | + OtaStatus otaStatus; |
| 85 | + |
| 86 | + Widget _buildWaveBg() { |
| 87 | + weatherPrint("_buildWaveBg progress: $progress, status: $otaStatus"); |
| 88 | + if (otaStatus == OtaStatus.DOWNLOADING) { |
| 89 | + return WaveProgress( |
| 90 | + Size(width, height), WeatherUtil.getColor(weatherType)[0], progress); |
| 91 | + } |
| 92 | + return Container(); |
| 93 | + } |
| 94 | + |
| 95 | + Widget _buildUpdateButton() { |
| 96 | + String buttonText = "立即更新"; |
| 97 | + VoidCallback action = startOta; |
| 98 | + if (otaStatus == OtaStatus.DOWNLOADING) { |
| 99 | + buttonText = "正在下载..."; |
| 100 | + action = null; |
| 101 | + } else if (otaStatus == OtaStatus.PERMISSION_NOT_GRANTED_ERROR) { |
| 102 | + buttonText = "请打开权限"; |
| 103 | + action = null; |
| 104 | + } |
| 105 | + return Container( |
| 106 | + margin: EdgeInsets.only(left: 40, right: 40, bottom: 25, top: 10), |
| 107 | + width: width, |
| 108 | + child: MaterialButton( |
| 109 | + onPressed: action, |
| 110 | + disabledColor: WeatherUtil.getColor(weatherType)[1].withOpacity(0.5), |
| 111 | + shape: RoundedRectangleBorder( |
| 112 | + side: BorderSide.none, |
| 113 | + borderRadius: BorderRadius.circular(25), |
| 114 | + ), |
| 115 | + child: Text( |
| 116 | + buttonText, |
| 117 | + style: TextStyle( |
| 118 | + color: Colors.white, fontSize: 13, fontWeight: FontWeight.w500), |
| 119 | + ), |
| 120 | + color: WeatherUtil.getColor(weatherType)[1], |
| 121 | + ), |
| 122 | + ); |
| 123 | + } |
| 124 | + |
| 125 | + void startOta() async { |
| 126 | + UmengAnalyticsPlugin.event(AnalyticsConstant.ota, label: "start"); |
| 127 | + OTAUtils.startOTA( |
| 128 | + widget.apkUrl, |
| 129 | + (event) { |
| 130 | + otaStatus = event.status; |
| 131 | + if (event.status == OtaStatus.DOWNLOADING) { |
| 132 | + progress = int.parse(event.value).toDouble(); |
| 133 | + } else if (event.status == OtaStatus.INSTALLING) { |
| 134 | + Navigator.of(context).pop(); |
| 135 | + } |
| 136 | + setState(() {}); |
| 137 | + }); |
| 138 | + } |
| 139 | + |
| 140 | + @override |
| 141 | + Widget build(BuildContext context) { |
| 142 | + return Container( |
| 143 | + width: width, |
| 144 | + height: height, |
| 145 | + child: Stack( |
| 146 | + children: [ |
| 147 | + _buildWaveBg(), |
| 148 | + Positioned( |
| 149 | + right: 10, |
| 150 | + top: 10, |
| 151 | + child: IconButton( |
| 152 | + icon: Icon( |
| 153 | + Icons.close, |
| 154 | + color: Colors.white.withAlpha(188), |
| 155 | + ), |
| 156 | + onPressed: () { |
| 157 | + UmengAnalyticsPlugin.event(AnalyticsConstant.ota, |
| 158 | + label: "cancel"); |
| 159 | + Navigator.of(context).pop(); |
| 160 | + }, |
| 161 | + ), |
| 162 | + ), |
| 163 | + Column( |
| 164 | + crossAxisAlignment: CrossAxisAlignment.start, |
| 165 | + children: [ |
| 166 | + Container( |
| 167 | + margin: EdgeInsets.only(top: 20, left: 30), |
| 168 | + child: Row( |
| 169 | + crossAxisAlignment: CrossAxisAlignment.end, |
| 170 | + children: [ |
| 171 | + Text( |
| 172 | + "版本更新", |
| 173 | + style: TextStyle( |
| 174 | + color: Colors.white, |
| 175 | + fontStyle: FontStyle.italic, |
| 176 | + fontWeight: FontWeight.bold, |
| 177 | + fontSize: 20, |
| 178 | + shadows: [ |
| 179 | + Shadow( |
| 180 | + color: Colors.black.withAlpha(100), |
| 181 | + offset: Offset(6, 3), |
| 182 | + blurRadius: 4) |
| 183 | + ], |
| 184 | + ), |
| 185 | + ), |
| 186 | + SizedBox( |
| 187 | + width: 20, |
| 188 | + ), |
| 189 | + Text( |
| 190 | + widget.versionName, |
| 191 | + style: TextStyle( |
| 192 | + color: Colors.white, |
| 193 | + fontStyle: FontStyle.italic, |
| 194 | + fontWeight: FontWeight.bold, |
| 195 | + fontSize: 15, |
| 196 | + shadows: [ |
| 197 | + Shadow( |
| 198 | + color: Colors.black.withAlpha(80), |
| 199 | + offset: Offset(2, 1), |
| 200 | + blurRadius: 4) |
| 201 | + ], |
| 202 | + ), |
| 203 | + ) |
| 204 | + ], |
| 205 | + ), |
| 206 | + ), |
| 207 | + Container( |
| 208 | + margin: EdgeInsets.only(left: 15, top: 30), |
| 209 | + child: Text( |
| 210 | + "更新内容", |
| 211 | + style: TextStyle( |
| 212 | + fontWeight: FontWeight.bold, |
| 213 | + fontSize: 15, |
| 214 | + color: Colors.white), |
| 215 | + ), |
| 216 | + ), |
| 217 | + Expanded( |
| 218 | + child: SingleChildScrollView( |
| 219 | + physics: BouncingScrollPhysics(), |
| 220 | + child: Container( |
| 221 | + margin: EdgeInsets.only(left: 15, top: 15, right: 12), |
| 222 | + child: Text( |
| 223 | + widget.desc, |
| 224 | + style: TextStyle(color: Colors.white, height: 1.8), |
| 225 | + ), |
| 226 | + ), |
| 227 | + ), |
| 228 | + ), |
| 229 | + _buildUpdateButton(), |
| 230 | + ], |
| 231 | + ), |
| 232 | + ], |
| 233 | + ), |
| 234 | + ); |
| 235 | + } |
| 236 | +} |
0 commit comments