其实从18年底时就有接触过Flutter,但是一直到现在才有时间去系统的学习和实战.本篇和后续文章会从入门到实战讲述和每一步流程.
废话结束 -> GO
介绍
首先老规矩,先看一下Flutter到底是个什么东西,可以用来干什么.
Flutter 百度百科
- Flutter是一款移动应用程序SDK,一份代码可以同时生成iOS和Android两个高性能、高保真的应用程序。
- Flutter目标是使开发人员能够交付在不同平台上都感觉自然流畅的高性能应用程序。我们兼容滚动行为、排版、图标等方面的差异。
- 无需移动开发经验即可开始使用。应用程序是用Dart语言编写的,如果您使用过Java或JavaScript之类的语言,则该应用程序看起来很熟悉。
优势
Flutter的优势:
- 提高开发效率
- 同一份代码开发iOS和Android, 成本, 无Android/iOS不统一问题
- Hot Reload(大部分情况下)在应用程序运行时更改代码并重新加载(通过热重载)
- 修复崩溃并继续从应用程序停止的地方进行调试
- 创建美观,高度定制的用户体验
- 受益于使用Flutter框架提供的丰富的Material Design和-Cupertino(iOS风格)的widget
- 实现定制、美观、品牌驱动的设计,而不受原生控件的限制
- IDE限制少,Windows、MacOS、Linux操作系统都可以安装
环境搭建
(本人使用Android Studio,主要在于Android Studio提供了FLutter Inspector工具,可以实时审查元素,解决界面的显示适配问题。当然用VS Code也是可以的)
环境搭建对于各位开发者来说应该没有问题, Linux基础命令即可,
配置Flutter环境ForMac
总结一句话, 根据Doctor提示来,都说的明明白白的. 另外安装过Python的可能会有坑,不过网上也很多现成的解决方案.
得到下图==配置成功
配置完成
Hello Flutter
1.打开Android Studio,选择新建Flutter project
image.png2. 选择新建Application
image.png主界面1-应用, 2-插件, 3-三方包/库 4.Flutter包(供Android/iOS使用)
我们创建完进入编辑界面后,我们可以看到主要有4块区域:
1、工程文件目录.
2、构建区域在这里可以选择,运行IOS Android 模拟器。
选择模拟器
3、Flutter 程序当前页面的组件树,Flutter 程序的核心原则是一切都是组件(Widget),所以他的所有都是组件。
4、dart语法分析,我们通过前面的介绍知道,Flutter程序是通过dart语言来写的。
这样创建完成之后,我们会看到lib目录下有一个main.dart文件,这个就是源代码:
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@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 simply save your changes to "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'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
// This widget is the home page of your application. It is stateful, meaning
// that it has a State object (defined below) that contains fields that affect
// how it looks.
// This class is the configuration for the state. It holds the values (in this
// case the title) provided by the parent (in this case the App widget) and
// used by the build method of the State. Fields in a Widget subclass are
// always marked "final".
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
// This call to setState tells the Flutter framework that something has
// changed in this State, which causes it to rerun the build method below
// so that the display can reflect the updated values. If we changed
// _counter without calling setState(), then the build method would not be
// called again, and so nothing would appear to happen.
_counter++;
});
}
@override
Widget build(BuildContext context) {
// This method is rerun every time setState is called, for instance as done
// by the _incrementCounter method above.
//
// The Flutter framework has been optimized to make rerunning build methods
// fast, so that you can just rebuild anything that needs updating rather
// than having to individually change instances of widgets.
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(widget.title),
),
body: Center(
// Center is a layout widget. It takes a single child and positions it
// in the middle of the parent.
child: Column(
// Column is also layout widget. It takes a list of children and
// arranges them vertically. By default, it sizes itself to fit its
// children horizontally, and tries to be as tall as its parent.
//
// Invoke "debug painting" (press "p" in the console, choose the
// "Toggle Debug Paint" action from the Flutter Inspector in Android
// Studio, or the "Toggle Debug Paint" command in Visual Studio Code)
// to see the wireframe for each widget.
//
// Column has various properties to control how it sizes itself and
// how it positions its children. Here we use mainAxisAlignment to
// center the children vertically; the main axis here is the vertical
// axis because Columns are vertical (the cross axis would be
// horizontal).
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
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.
);
}
}
这段代码其实很简单,首先我们看到main()函数调用了runAPP(),这里简单说下
runApp函数接受指定的控件(Widget),并使其作为控件树(widget tree)的根控件。
runAPP的入参是下面定义的MyAPP,在MyAPP里面创建了_MyHomePageState的对象作为首页,我们可以看到下面_MyHomePageState里面定义了中间是两个Text ,底部是一个floatingActionButton。关于dart的语法后续文章详细说明,这里就简单了解一下这个demo:就是点击一次下面的悬浮按钮,中间的数字会加1.完了之后我们就可以运行,根据上面的介绍我们选择模拟器运行,如下图所示:
第一个Flutter程序
小结
总的来说dart语法多熟悉,多敲之后还是很人性化的一门极简风格语言,Flutter入门还是非常简单的.
网友评论