ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Flutter实战1 --- 写一个天气查询的APP,android游戏开发大全第二版代码

2021-11-12 16:03:06  阅读:131  来源: 互联网

标签:return WeatherData APP flutter --- 添加 new android class


),
home: MyHomePage(title: ‘Flutter Demo Home Page’),
);
}
}

其中home 就是要显示的界面,这里我们要把MyHomePage换成我们自己的。

4.1 创建WeatherWidget

通过 new -> Dart File 在lib目录下创建WeatherWidget

class WeatherWidget extends StatefulWidget{
@override
State createState() {
// TODO: implement createState
return new WeatherState();
}
}

class WeatherState extends State{
@override
Widget build(BuildContext context) {
// TODO: implement build
return Scaffold(
);
}
}

创建完后,在main.dart中将home改为WeatherWidget,如下:

class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: ‘Flutter Demo’,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: WeatherWidget(),
);
}

4.2 HotReload

在写UI的工程中,我们可以用到Flutter的hot reload的特性,写布局的时候,按ctrl+scmd+s就可以在手机上实时看到界面的变化。

这个功能很好用。

4.3添加图片资源

Flutter可以添加不同的资源,例如图片、文本、配置文件、静态数据等。

添加资源时,需要在pubspec.yaml文件中的flutter属性下添加assets,并标明要添加资源的路径,例如,我们要加入指定的图片时,可以这么写:

flutter:
assets:

  • assets/my_icon.png
  • assets/background.png

如果要添加的资源太多,也可以添加文件夹,例如:

flutter:
assets:

  • assets/

在本demo中,要添加一个背景图,我们在工程的根目录下创建images目录,将背景图放在images目录下,然后在pubspec.yaml中添加:

flutter:

The following line ensures that the Material Icons font is

included with your application, so that you can use the icons in

the material Icons class.

uses-material-design: true
assets:

  • images/

4.4 写WeatherWidget的UI布局

Scaffold中添加body的属性,来写UI的布局,如下:

class WeatherState extends State{
@override
Widget build(BuildContext context) {
// TODO: implement build
return Scaffold(
body: new Stack(
fit: StackFit.expand,
children: [
new Image.asset(“images/weather_bg.jpg”,fit: BoxFit.fitHeight,),
new Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
new Container(
width: double.infinity,
margin: EdgeInsets.only(top: 40.0),
child: new Text(
“广州市”,
textAlign: TextAlign.center,
style: new TextStyle(
color: Colors.white,
fontSize: 30.0,
),
),
),
new Container(
width: double.infinity,
margin: EdgeInsets.only(top: 100.0),
child: new Column(
children: [
new Text(
“20 °”,
style: new TextStyle(
color: Colors.white,
fontSize: 80.0
)),
new Text(
“晴”,
style: new TextStyle(
color: Colors.white,
fontSize: 45.0
)),
new Text(
“湿度 80%”,
style: new TextStyle(
color: Colors.white,
fontSize: 30.0
),
)
],
),
)
],
)
],
),
);
}

}

ctrl+s,在手机上就可以看到写好的UI,但这时候的数据是写死的,下来看如何通过http获取数据。

5.通过http获取数据

要通过http数据,我们首先要添加http的依赖库,在pubspec.yaml中的dependencies添加如下:

dependencies:
flutter:
sdk: flutter

The following adds the Cupertino Icons font to your application.

Use with the CupertinoIcons class for iOS style icons.

cupertino_icons: ^0.1.2
http: ^0.12.0

然后在当前工程目录下运行以下命令行:

$ flutter packages get

或者在Android Stuido 打开pubspec.yaml 文件,点击上面的packages get

这里操作的意义是,拉取http的库。

《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》

浏览器打开:qq.cn.hn/FTe 免费领取

5.1 创建WeatherData类

通过 new -> Dart File 在lib目录下创建WeatherData

class WeatherData{
String cond; //天气
String tmp; //温度
String hum; //湿度

WeatherData({this.cond, this.tmp, this.hum});

factory WeatherData.fromJson(Map<String, dynamic> json) {
return WeatherData(
cond: json[‘HeWeather6’][0][‘now’][‘cond_txt’],
tmp: json[‘HeWeather6’][0][‘now’][‘tmp’]+“°”,
hum: “湿度 “+json[‘HeWeather6’][0][‘now’][‘hum’]+”%”,
);
}

factory WeatherData.empty() {
return WeatherData(
cond: “”,
tmp: “”,
hum: “”,
);
}
}

5.2 数据获取

class WeatherState extends State{

WeatherData weather = WeatherData.empty();

WeatherState(){
_getWeather();
}

void _getWeather() async{
WeatherData data = await _fetchWeather();
setState((){
weather = data;
});
}

Future _fetchWeather() async{
final response = await http.get(‘https://free-api.heweather.com/s6/weather/now?location=广州&key=ebb698e9bb6844199e6fd23cbb9a77c5’);
if(response.statusCode == 200){
return WeatherData.fromJson(json.decode(response.body));
}else{
return WeatherData.empty();
}
}

@override

标签:return,WeatherData,APP,flutter,---,添加,new,android,class
来源: https://blog.csdn.net/m0_63935510/article/details/121289721

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有