Flutter 颤振条件背景图像叠加
我正在开发一个带有颤振的天气应用程序。我正在考虑设计一个背景,它会随着天气的变化而变化。但我不知道怎么做。我看到了类似的问题,但我没有看到任何好处 这是我所有的代码Flutter 颤振条件背景图像叠加,flutter,dart,mobile,Flutter,Dart,Mobile,我正在开发一个带有颤振的天气应用程序。我正在考虑设计一个背景,它会随着天气的变化而变化。但我不知道怎么做。我看到了类似的问题,但我没有看到任何好处 这是我所有的代码 import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:http/http.dart' as http; import 'package:location/location.dart'; import 'package:flutte
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:location/location.dart';
import 'package:flutter/services.dart';
import 'package:uygulama1/Weather.dart';
import 'package:uygulama1/WeatherItem.dart';
import 'package:uygulama1/WeatherData.dart';
import 'package:uygulama1/ForecastData.dart';
//PROJECT'S ROOT
void main() {
runApp(MaterialApp(
title: "WeatherApp",
home: MyApp(),
));
}
//PROJECTS MAIN CLASS
class MyApp extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return new MyAppState();
}
}
class MyAppState extends State<MyApp> {
bool isLoading = false;
WeatherData weatherData;
ForecastData forecastData;
Location _location = new Location();
String error;
@override
void initState() {
super.initState();
loadWeather();
}
Future<LocationData> getLocationData() async {
return await _location.getLocation();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Weather App',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Scaffold(
backgroundColor: Colors.tealAccent,
appBar: AppBar(
title: Text('Flutter Weather App'),
),
body: Center(
child: Column(mainAxisSize: MainAxisSize.min, children: <Widget>[
//BACKGROUND IMAGE
Container(
decoration: BoxDecoration(
image: new DecorationImage(
image: new AssetImage("assets/rain.jpg"),fit: BoxFit.cover
),
),
),
//END
Expanded(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Padding(
padding: const EdgeInsets.all(8.0),
child: weatherData != null
? Weather(weather: weatherData)
: Container(),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: isLoading
? CircularProgressIndicator(
strokeWidth: 2.0,
valueColor:
new AlwaysStoppedAnimation(Colors.black),
)
: IconButton(
icon: new Icon(Icons.refresh),
tooltip: 'Refresh',
onPressed: loadWeather,
color: Colors.black,
),
),
],
),
),
SafeArea(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
height: 200.0,
child: forecastData != null
? ListView.builder(
itemCount: forecastData.list.length,
scrollDirection: Axis.horizontal,
itemBuilder: (context, index) => WeatherItem(
weather: forecastData.list.elementAt(index)))
: Container(),
),
),
)
]))),
);
}
loadWeather() async {
setState(() {
isLoading = true;
});
LocationData location;
try {
location = await getLocationData();
error = null;
} on PlatformException catch (e) {
if (e.code == 'PERMISSION_DENIED') {
error = 'Permission denied';
} else if (e.code == 'PERMISSION_DENIED_NEVER_ASK') {
error =
'Permission denied - please ask the user to enable it from the app settings';
}
location = null;
}
if (location != null) {
final lat = location.latitude;
final lon = location.longitude;
final weatherResponse = await http.get(
'https://api.openweathermap.org/data/2.5/weather?APPID=d89de3f0b2dedfe4f923f1e7f709953a&lat=${lat.toString()}&lon=${lon.toString()}');
final forecastResponse = await http.get(
'https://api.openweathermap.org/data/2.5/forecast?APPID=d89de3f0b2dedfe4f923f1e7f709953a&lat=${lat.toString()}&lon=${lon.toString()}');
if (weatherResponse.statusCode == 200 &&
forecastResponse.statusCode == 200) {
return setState(() {
weatherData =
new WeatherData.fromJson(jsonDecode(weatherResponse.body));
forecastData =
new ForecastData.fromJson(jsonDecode(forecastResponse.body));
isLoading = false;
});
}
}
setState(() {
isLoading = false;
});
}
}
导入'dart:convert';
进口“包装:颤振/材料.省道”;
将“package:http/http.dart”导入为http;
导入“package:location/location.dart”;
导入“包:flifter/services.dart”;
进口“包装:维库拉玛1/Weather.dart”;
进口“包装:维库拉玛1/WeatherItem.dart”;
导入“package:uigulama1/WeatherData.dart”;
导入“package:uigulama1/ForecastData.dart”;
//项目的根
void main(){
runApp(材料应用程序)(
标题:“WeatherApp”,
主页:MyApp(),
));
}
//项目主类
类MyApp扩展了StatefulWidget{
@凌驾
状态createState(){
返回新的MyAppState();
}
}
类MyAppState扩展了状态{
bool isLoading=false;
天气数据;
预报数据预报数据;
位置_Location=新位置();
字符串错误;
@凌驾
void initState(){
super.initState();
loadWeather();
}
Future getLocationData()异步{
返回wait_location.getLocation();
}
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“颤振天气应用程序”,
主题:主题数据(
主样本:颜色。蓝色,
),
家:脚手架(
背景颜色:Colors.tealacent,
appBar:appBar(
标题:文本(“颤振天气应用程序”),
),
正文:中(
子项:列(mainAxisSize:mainAxisSize.min,子项:[
//背景图像
容器(
装饰:盒子装饰(
图片:新装饰图片(
图片:新资产图像(“assets/rain.jpg”),适合:BoxFit.cover
),
),
),
//结束
扩大(
子:列(
mainAxisAlignment:mainAxisAlignment.center,
儿童:[
填充物(
填充:常数边集全部(8.0),
子项:weatherData!=null
?天气(天气:天气数据)
:Container(),
),
填充物(
填充:常数边集全部(8.0),
孩子:孤岛
?循环压缩机指示器(
冲程宽度:2.0,
valueColor:
全新AlwaysStoppedAnimation(颜色:黑色),
)
:图标按钮(
图标:新图标(图标。刷新),
工具提示:“刷新”,
按:loadWeather,
颜色:颜色,黑色,
),
),
],
),
),
安全区(
孩子:填充(
填充:常数边集全部(8.0),
子:容器(
高度:200.0,
子:forecastData!=null
?ListView.builder(
itemCount:forecastData.list.length,
滚动方向:轴水平,
itemBuilder:(上下文,索引)=>WeatherItem(
天气:forecastData.list.elementAt(索引)))
:Container(),
),
),
)
]))),
);
}
loadWeather()异步{
设置状态(){
isLoading=true;
});
位置数据位置;
试一试{
位置=等待getLocationData();
错误=null;
}平台上异常捕获(e){
如果(e.code=='权限被拒绝'){
错误='权限被拒绝';
}else if(e.code=='PERMISSION\u DENIED\u NEVER\u ASK'){
错误=
'权限被拒绝-请要求用户从应用程序设置启用它';
}
位置=空;
}
如果(位置!=null){
最终纬度=位置纬度;
最终lon=位置。经度;
final weatherResponse=等待http.get(
'https://api.openweathermap.org/data/2.5/weather?APPID=d89de3f0b2dedfe4f923f1e7f709953a&lat=${lat.toString()}&lon=${lon.toString()}');
final forecastResponse=等待http.get(
'https://api.openweathermap.org/data/2.5/forecast?APPID=d89de3f0b2dedfe4f923f1e7f709953a&lat=${lat.toString()}&lon=${lon.toString()}');
如果(weatherResponse.statusCode==200&&
forecastResponse.statusCode==200){
返回设置状态(){
气象数据=
新的WeatherData.fromJson(jsonDecode(weatherResponse.body));
预报数据=
新的ForecastData.fromJson(jsonDecode(forecastResponse.body));
isLoading=false;
});
}
}
设置状态(){
isLoading=false;
});
}
}
天气预报
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:uygulama1/WeatherData.dart';
class Weather extends StatelessWidget {
final WeatherData weather;
Weather({Key key, @required this.weather}) : super(key: key);
@override
Widget build(BuildContext context) {
var temperature = (weather.temp - 273.15).round();
return Column(
children: <Widget>[
Text(weather.name, style: new TextStyle(color: Colors.black)),
Text("\n" + weather.main,
style: new TextStyle(color: Colors.black, fontSize: 32.0)),
Text("Temp: " + '${temperature.toString()}°C',
style: new TextStyle(color: Colors.black)),
Image.network('https://openweathermap.org/img/w/${weather.icon}.png'),
Text("Date: " + new DateFormat.yMMMd().format(weather.date),
style: new TextStyle(color: Colors.black)),
Text("Hour: " + new DateFormat.Hm().format(weather.date),
style: new TextStyle(color: Colors.black)),
],
);
}
}
导入“包装:颤振/材料.省道”;
导入“包:intl/intl.dart”;
导入“package:uigulama1/WeatherData.dart”;
类天气小部件{
最终天气数据;
天气({Key-Key,@required this.Weather}):超级(Key:Key);
@凌驾
小部件构建(构建上下文){
变量温度=(weather.temp-273.15).round();
返回列(
儿童:[
文本(weather.name,style:newtextstyle(颜色:Colors.black)),
文本(“\n”+weather.main,
样式:新文本样式(颜色:Colors.black,fontSize:32.0)),
文本(“温度:”+'${temperature.toString()}°C',
样式:新文本样式(颜色:Colors.black)),
Image.network('https://openweathermap.org/img/w/${weather.icon}.png'),
文本(“日期:”+新的日期形式
Container(
decoration: BoxDecoration(
image: new DecorationImage(
image:
fit: BoxFit.cover,
(isRaining)? new AssetImage("assets/rain.jpg"):new AssetImage("assets/sunny.jpg")
),
),
Container(
decoration: BoxDecoration(
image: new DecorationImage(
image:
fit: BoxFit.cover,
if(isRaining)
new AssetImage("assets/rain.jpg")
else if(isSunny)
new AssetImage("assets/sunny.jpg")
else
new AssetImage("assets/cold.jpg")
),
),
final Map<String, AssetImage> images = {"rain": AssetImage("assets/rain.jpg"),
"wind": AssetImage("assets/wind.jpg"),
"snow": AssetImage("assets/snow.jpg")};
Container(
decoration: BoxDecoration(
image: new DecorationImage(
image: weatherData == null ? images["wind"] : images[weatherData.name],
fit: BoxFit.cover
),
),
),