preface
Flutter is Google’s UI toolkit for building beautiful, natively compiled applications for mobile, web, desktop, and embedded devices from a single codebase.
1. 文件结构
- myapp
- android
- ios
- web
- build
- lib
- main.dart
- (需要编写的代码一般都在这)
- test
- .gitignore
- .metadata
- .packages
- 项目名.iml
- pubspec.lock
- (管理依赖包名及版本)
- pucspec.yaml
- (构建发布时的配置文件,name,version)
- (管理 assets)
- External Libraries
- Dart Packages
- Dart SDK
2. demo 代码
2.1 hello world
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
Widget build(BuildContext context) {
return MaterialApp(
title: 'Welcome to Flutter',
home: Scaffold(
appBar: AppBar(
title: const Text('Welcome to Flutter'),
),
body: const Center(
child: Text('Hello World'),
),
),
);
}
}
2.2 按钮点击计数
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
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: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required 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;
State<MyHomePage> 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++;
});
}
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 a 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>[
const Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}
一些记录:
- web 测试时,会启用一个新的端口来显示网页
- android 测试时,会生成一个 40mb 的 apk 包
- android 构建时,使用指令:
生成的ARM架构的 APK 大小 5.3 mbflutter build apk --split-per-abi
3. 其它说明
1:热重载
支持应用程序在运行状态下重载代码而无需重新启动应用程序或者丢失程序运行状态。
快捷键:ctrl + s
或 ctrl + \
2. 模式说明
- debug 模式
- 更大的性能开销实现了更快速的开发效率
- profile 模式
- release 模式
3. 环境说明
按照官网教程配置 flutter 环境,别忘了修改镜像源。
#添加bin到系统的环境变量,并运行
flutter --version
flutter doctor
出现结果如下即可
C:\Users\User>flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel stable, 2.5.3, on Microsoft Windows [Version 10.0.22000.282], locale zh-CN)
[√] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
[√] Chrome - develop for the web
[√] Android Studio (version 2020.3)
[√] VS Code (version 1.61.2)
[√] Connected device (3 available)
• No issues found!
other
uses-material-design: true
cupertino_icons: ^1.0.2
CupertinoIcons class for iOS style icons
注意手机的架构,默认的架构是 X86,生成的 APK 无法安装在 大部分ARM 架构的手机上
构建打包后ARM架构的才能安装,ARM 架构的无法运行在电脑的模拟器上。
4. dart 例子
4.1 类的初始化及类继承
void main(){
User userOne = User('luigi', 25);
print(userOne.username);
SuperUser userTwo = SuperUser("alicia", 30);
userTwo.login();
userTwo.publish();
}
class User{
String username;
int age;
User(this.username, this.age){} //注意这里的构建
void login(){
print("user logged in");
}
}
class SuperUser extends User{
SuperUser(String name, int age): super(name,age);
void publish(){
print("published update");
}
}
5. widgets
设计理念:所有界面都是一棵 widget树,每个widget 具有不同属性
- Scaffold
- AppBar
- title
- centerTitle: true
- backgroundColor: Colors.red 100
- Center
- Text
- Button
- FloatingActionButton
- child
- onPressed: () {}
- Row
- Column
- Image
6. 做个番茄
做个可以升级的番茄时钟管理
构建发布时,一般应用商店都需要企业认证(如安卓小米商店)。
这时,可以使用 应用分发平台,它会给你的应用提供一个下载的网页。
other
flutter 132k⭐
参考项目:
- flutter-picgo,较简易的入门项目
- gsy_github_app_flutter,近期,12.8k⭐
- gsy_flutter_demo,两月前,2.1k⭐
- FlutterTodo, 2年前,516⭐
- flutter-go,2年前,22.9k⭐
- Best-Flutter-UI-Templates, 13.1k
- FlutterExampleApps
推荐学习资源: