Skip to content

flutter学习

>

Flutter usage:

https://docs.flutter.dev/get-started

Dart lanuage

https://dart.dev/docs

Github

https://github.com/flutter/flutter

中文资料

https://book.flutterchina.club/

1. 安装

1.1 安装flutter sdk

https://docs.flutter.dev/get-started/install/windows

windows版本

https://docs.flutter.dev/get-started/install/windows/desktop

将文件解压到一个目录(自己定)

但是这个目录有个要求:

1、不要有特殊字符(中文 ,空格之类的)

2、不需要特殊权限(不要放c盘)

1.2 加入环境变量

将“D:\flutter\sdk\flutter\bin”加入path环境变量

1.3 检查是否安装成功了

yaml
flutter doctor

2. 常用命令

2.1 运行

yaml
Flutter run main.dart

2.2 检查工程

yaml
flutter analyze

3. fllutter的包

地址:https://pub.dev/packages

3.1 国内加速

yaml
eport PUB_HOSTED_URL=https://pub.flutter-io.cn
export FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cn

包管理文件

pubspec.yaml

安装dependence

yaml
flutter packages get

添加一个包

yaml
flutter pub add provider

4. 一些特殊的语法

4.1 _internal

_internal is just a name for a private constructor.

4.2 并行

dart中的异常是单线程的

4.3 构造函数

4.3.1 基本的

java
class Point {
  // Instance variables to hold the coordinates of the point.
  double x;
  double y;

  // Generative constructor with initializing formal parameters:
  Point(this.x, this.y);
}

4.3.2 命名构造函数

cpp
const double xOrigin = 0;
const double yOrigin = 0;

class Point {
  final double x;
  final double y;

  // Sets the x and y instance variables
  // before the constructor body runs.
  Point(this.x, this.y);

  // Named constructor
  Point.origin() : x = xOrigin, y = yOrigin;
}

4.3.3 重定向

java
class Point {
  double x, y;

  // The main constructor for this class.
  Point(this.x, this.y);

  // Delegates to the main constructor.
  Point.alongXAxis(double x) : this(x, 0);
}

4.3.4 工厂构造函数

js
class Logger {
  final String name;
  bool mute = false;

  // _cache is library-private, thanks to
  // the _ in front of its name.
  static final Map<String, Logger> _cache = <String, Logger>{};

  factory Logger(String name) {
    return _cache.putIfAbsent(name, () => Logger._internal(name));
  }

  factory Logger.fromJson(Map<String, Object> json) {
    return Logger(json['name'].toString());
  }

  Logger._internal(this.name);

  void log(String msg) {
    if (!mute) print(msg);
  }
}

5. widgets

https://docs.flutter.dev/ui/widgets

Widgets类中build 是入口

widgets分statelesswidgest(不变的),statefullwidget(可变的)

statefullwidget没有build method.是通过state object的build

5.1 常用的widgetts

widgets列表

https://docs.flutter.dev/reference/widgets

5.2 layout

5.3 boxtypes

6. 夸组件状态管理

6.1 inheritedWidget

https://juejin.cn/post/6844904066607218701

inheritedWidget 是一个特殊的 Widget,它将作为另一个子树的父节点放置在 Widget 树中。该子树的所有 widget 都必须能够与该 InheritedWidget 暴露的数据进行交互。

本质是一个widget,实现了updateShouldNotify方法

将return turn 表示通过子节点。需要重现build

6.2 provider

6.3 ChangeNotifier