Flutter helloWorld

作为一个Android开发者,学习flutter的过程中,看看这篇文章,会有不小的收获
flutter for android developer

国际惯例,先来一个helloWorld
Androidstudio创建一个Flutter project

1. flutter项目结构认识

一个flutter项目以下目录结构(如图)
1. lib 文件夹存放我们的dart源文件
2. pubspec.yaml文件 

2. pubspec.yaml 文件

配置文件,声明了我们项目的依赖,第三方库,assets资源 图片等
点击packages get 下载依赖,然后使用

3. 第一个flutter项目

构件一个material风格的app -> MaterialApp
home: 即显示的主内容,MyHomePage
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
	void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(

primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
主界面展示: 
1. Scaffold 是flutter提供给我们的一个widget,它包含了appbar,floatActionButton,drawer,等一些列功能
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
	
class MyHomePage extends StatelessWidget {
MyHomePage({Key key, this.title}) : super(key: key);

final String title;

@override
Widget build(BuildContext context) {
// TODO: implement build
return Scaffold(
appBar: AppBar(
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text("title"),
),
body: Center(
// Center is a layout widget. It takes a single child and positions it
// in the middle of the parent.
child: Column(

mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'times',
style: Theme.of(context).textTheme.display1,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}

void _incrementCounter() {
}
}

运行效果:

4. 项目剖析

在flutter中,入口函数为main ,我们通过runapp构件一个material风格的app,并通过scaffold设置了 appBar等
flutter中万物皆widget,我们常用的widget分为有状态和无状态两种,
StatelessWidget,StateFulWidget 
其中,stateFulWidget可以让我们更新widget显示,比如图片,文字,等

项目源码已上传至
github lib/helloWorld.dart