Flutter 从零开始 002 创建第一个flutter项目
Flutter 从零开始 002 创建第一个flutter项目
本文将引导你从零开始创建第一个Flutter项目。通过本教程,你将学习如何在Android Studio中创建Flutter工程,理解项目的基本文件结构,以及掌握Flutter应用的核心组件和状态管理机制。
创建并运行一个Flutter App工程
打开Android Studio(简称AS)>File>New>New Flutter Project
选择一个模拟器,点击旁边的run按钮
第一次编译项目时间比较长,耐心等待即可,运行完成 如下图
项目基本文件目录
Flutter项目主要分为Android,iOS,lib,pubspec.yaml这四个,其中lib下就是用来flutter开发的,我们的dart文件就放在这里,pubspec.yaml文件就是flutter的构建管理工具,就和Android 的gradle 一样
name: flutter001
description: A new Flutter project.
# The following line prevents the package from being accidentally published to
# pub.dev using `flutter pub publish`. This is preferred for private packages.
publish_to: 'none' # Remove this line if you wish to publish to pub.dev
# The following defines the version and build number for your application.
# A version number is three numbers separated by dots, like 1.2.43
# followed by an optional build number separated by a +.
# Both the version and the builder number may be overridden in flutter
# build by specifying --build-name and --build-number, respectively.
# In Android, build-name is used as versionName while build-number used as versionCode.
# Read more about Android versioning at https://developer.android.com/studio/publish/versioning
# In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion.
# Read more about iOS versioning at
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
version: 1.0.0+1
environment:
sdk: ">=2.16.1 <3.0.0"
# Dependencies specify other packages that your package needs in order to work.
# To automatically upgrade your package dependencies to the latest versions
# consider running `flutter pub upgrade --major-versions`. Alternatively,
# dependencies can be manually updated by changing the version numbers below to
# the latest version available on pub.dev. To see which dependencies have newer
# versions available, run `flutter pub outdated`.
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: ^1.0.2
dev_dependencies:
flutter_test:
sdk: flutter
# The "flutter_lints" package below contains a set of recommended lints to
# encourage good coding practices. The lint set provided by the package is
# activated in the `analysis_options.yaml` file located at the root of your
# package. See that file for information about deactivating specific lint
# rules and activating additional ones.
flutter_lints: ^1.0.0
# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec
# The following section is specific to Flutter.
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
# To add assets to your application, add an assets section, like this:
# assets:
# - images/a_dot_burr.jpeg
# - images/a_dot_ham.jpeg
# An image asset can refer to one or more resolution-specific "variants", see
# https://flutter.dev/assets-and-images/#resolution-aware.
# For details regarding adding assets from package dependencies, see
# https://flutter.dev/assets-and-images/#from-packages
# To add custom fonts to your application, add a fonts section here,
# in this "flutter" section. Each entry in this list should have a
# "family" key with the font family name, and a "fonts" key with a
# list giving the asset and other descriptors for the font. For
# example:
# fonts:
# - family: Schyler
# fonts:
# - asset: fonts/Schyler-Regular.ttf
# - asset: fonts/Schyler-Italic.ttf
# style: italic
# - family: Trajan Pro
# fonts:
# - asset: fonts/TrajanPro.ttf
# - asset: fonts/TrajanPro_Bold.ttf
# weight: 700
#
# For details regarding fonts from package dependencies,
# see https://flutter.dev/custom-fonts/#from-packages
这里面就是对工程的配置,库的依赖,资源的依赖等
项目简介
这是Flutter的入口函数,runApp启动的Widget
void main() => runApp(MyApp());
MyApp类代表Flutter应用,它继承了StatelessWidget类,这也就意味着应用本身也是一个widget。在Flutter中,大多数东西都是widget(后同“组件”或“部件”),包括对齐(Align)、填充(Padding)、手势处理(GestureDetector)等,它们都是以widget的形式提供。
Flutter在构建页面时,会调用组件的build方法,widget的主要工作是提供一个build()方法来描述如何构建 UI 界面(通常是通过组合、拼装其它基础widget)。
MaterialApp是Material库中提供的Flutter APP框架,通过它可以设置应用的名称、主题、语言、首页及路由列表等。MaterialApp也是一个widget。
home为Flutter应用的首页,它也是一个widget。
MyHomePage是应用的首页,它继承自StatefulWidget类,表示它是一个有状态的组件(Statefulwidget)。关于有状态组件和无状态组件,我们在后面会详细讲解,现在只需要知道这是一个有状态组件就可以了。
StatefulWidget可以拥有状态,这些状态在widget生命周期中是可以变的,而StatelessWidget是不可变的。
Stateful widget至少由两个类组成:
- 一个StatefulWidget类。
- 一个State类;StatefulWidget类本身是不变的,但是State类中持有的状态在widget生命周期中可能会发生变化。
_MyHomePageState类是MyHomePage类对应的状态类。看到这里,读者可能已经发现:和MyApp类不同,MyHomePage类中并没有build方法,取而代之的是,build方法被挪到了_MyHomePageState方法中。
State类
接下来,我们看看_MyHomePageState中都包含哪些东西:
该组件的状态。由于我们只需要维护一个点击次数计数器,所以定义一个_counter状态:
int _counter = 0; //用于记录按钮点击的总次数
_counter为保存屏幕右下角带“+”号按钮点击次数的状态。
设置状态的自增函数。
void _incrementCounter() {
setState(() {
_counter++;
});
}
当按钮点击时,会调用此函数,该函数的作用是先自增_counter,然后调用setState方法。setState方法的作用是通知Flutter框架,有状态发生了改变,Flutter框架收到通知后,会执行build方法来根据新的状态重新构建界面,Flutter对此方法做了优化,使重新执行变的很快,所以你可以重新构建任何需要更新的东西,而无需分别去修改各个widget。
构建UI界面
构建UI界面的逻辑在 build 方法中,当MyHomePage第一次创建时,_MyHomePageState类会被创建,当初始化完成后,Flutter框架会调用widget的build方法来构建 widget 树,最终将widget树渲染到设备屏幕上。所以,我们看看_MyHomePageState的build方法中都干了什么事:
@Override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(