Flutter实战(一)写一个天气查询的APP

@override
Widget build(BuildContext context) {
return MaterialApp(
title: ‘Flutter Demo’,
theme: ThemeData(
// This is the theme of your application.
//
// Try running your application with “flutter run”. You’ll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// “hot reload” (press “r” in the console where you ran “flutter run”,
// or press Run > Flutter Hot Reload in a Flutter IDE). Notice that the
// counter didn’t reset back to zero; the application is not restarted.
primarySwatch: Colors.blue,
),
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的库。

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(){

最后

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助

因此我收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门**

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

  • 28
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值