Flutter 第一篇 基本语法

108 阅读2分钟

介绍

flutter是google开源的一个跨平台开发平台。

安装

  • flutter
  • Android Studio

flutter安装

官方网站 flutter.dev/

由于打包iOS应用必须使用苹果系统,因此推荐使用苹果电脑进行开发。

如果是apple芯片需要sudo softwareupdate --install-rosetta --agree-to-license

下载安装包apple芯片 或者inter芯片

解压后保存到~/app目录,这个目录可以根据个人使用习惯进行修改。

修改环境变量

echo $SHELL查看当前的shell,默认是zsh。vi ~/.zshrc

#flutter 镜像
export PUB_HOSTED_URL=https://pub.flutter-io.cn
export FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cn

# flutter命令
export FLUTTER=~/app/flutter
export PATH=$PATH:$FLUTTER/bin

source ~/.zshrc刷新环境变量。或者新开一个终端,之前修改的环境变量也会生效。

检查安装条件 flutter doctor可以检查有哪些条件不满足。

安装Anddroid Studio

前往developer.android.google.cn/studio 进行下载。 双击安装包就可以完成,接着flutter doctor可以查看是否安装完成。

//问题1
cmdline-tools component is missing
Run `path/to/sdkmanager --install "cmdline-tools;latest"`
See https://developer.android.com/studio/command-line for more details.

// 处理方式
选择,设置,Appearance & Behavior, System Settings, Android SDK,选择SDK Tools,选择Android SDK Command-line Tools(latest)进行安装
//问题2Android license status unknown.
      Run `flutter doctor --android-licenses` to accept the SDK licenses.
      See https://flutter.dev/docs/get-started/install/macos#android-setup for
      more details.
      
// 处理方式
执行`flutter doctor --android-licenses`

Xcode安装 直接在app store搜索安装。

// 安装完成之后需要选择一个Xcode
sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer
//问题1
    ✗ CocoaPods not installed.
        CocoaPods is used to retrieve the iOS and macOS platform side's plugin
        code that responds to your plugin usage on the Dart side.
        Without CocoaPods, plugins will not work on iOS or macOS.
        For more info, see https://flutter.dev/platform-plugins
      To install see
      https://guides.cocoapods.org/using/getting-started.html#installation for
      instructions.
      
  
// 处理方法
sudo gem install cocoapods

安装插件

Plugins选择flutter,安装flutter的时候会提示我们是否安装dart,我们点击确认。因为flutter是使用dart语言进行开发的。

hello world

新建项目的时候,可以看到一个new flutter project,新建完成后,运行。可以看到一个计数器的项目。也可以选择一个empty项目,用于学习dart语法,此时需要配置dart sdk,在flutter的bin目录下有个cache,包含dart sdk。

变量

任意类型的变量var。

  // 变量 通过var声明变量
  var a ;// 声明变量但是不初始化,此时变量的值为null
  print(a);
  a = 10;
  print(a);
  
  a = 20;
  print(a);

  // 此时变量可以被赋值为字符串类型
  a = "Hello world";
  print(a);

固定类型的变量

  int a;
  a = 10;
  print(a);
  a = 20;
  print(a);

  // 此时,由于a的类型固定,不能赋值其它的类型
  // a = "Hello";
  // print(a);

最终变量final

最终变量只能被赋值一次,且必须被赋值才能使用。

最终变量的值不要求编译时确定,可以在程序运行的任意时候指定。

  final a;

  // 语法错误,不能直接使用,必须初始化
  // print(a);

  a = 20;
  print(a);

  // 报错,只能赋值一次
  // a = 30;

  int b = 1000;
  final c = b;
  print(c);

常量const

常量的值是不能被修改的。

  // 语法错误,常量必须在声明的时候初始化
  // const a ;
  // a = 20;


  // 语法错误:
  // 常量必须在编译期确定
  // int a = 20;
  // const b = a;
  // print(b);

  const a = 20;
  print(a);

数据类型

number

  // 整数
  int a = 10;

  // 浮点数
  double b = 1.03;

  // 运算符
  // + - * / %  ~/取整
  print(10~/3);// 3

string

  // 字符串 单引号或者双引号
  var s1 = 'hello world';
  var s2 = "Hello world";
  var s3 = """床前明月光,
  疑是地上霜,
  """;
  var s4 = '''床前明月光,
  疑是地上霜,
  ''';

  print(s1);
  print(s1[0]);
  
  // 运行错误
  // print(s1[100]);


  // 模板字符串
  print("$s1 + $s2 = ${s1 + s2}");

list

  //可变列表
  var list1 = [1, 2.2, "Hello"];

  // 不可变列表
  var list2 = const [1, 2.2 , "hello"];

  // 增
  list1.insert(0, "001");
  
  // 删
  list1.removeAt(1);
  // list1.removeLast();
  // list1.remove(1);
  // list1.removeRange(0, 2);

  // list1.clear();// 清空

  var list3 = [1, 3, 2, 4];
  list3.sort();
  print(list3);

map

  // 可变
  var json1 = {"one":"1", 2:"hello world"};
  // 不可变
  var json2 = const {"one":"1", 2:"hello world"};
  
  print(json1[2]);// hello world

  json1["china"] = "中国";

  // 获取所有的key
  print(json1.keys) ;

  // 获取所有的value
  print(json1.values);

运算符

var a ;
// 如果a为nil则赋值
a ??= 10;

// 如果a有值,则返回a,否则返回b
print(a ?? 20);

函数

普通函数

void main() {
  void hello() {
    print("hello world");
  }

  // 如果只有一句,则可以使用箭头
  void hello1() => print("hello world");

  hello();
  hello1();
}

可选参数函数

void main() {
  void sum1(int a, {int b = 0, int c = 0}) {
    print(a + b + c);
  }

  // 如果不给默认值,则必须为可选类型
  void sum2(int a, {int? b, int c = 0}) {
    b ??= 10;
    print(a + b + c);
  }

  sum1(10, b: 20);
  sum2(10, c: 20);
}

匿名函数

void main() {
  // 匿名函数
  var a = () {
    print("hello world");
  };
  a();
  a();
}

立即执行函数

void main() {
  // 立即执行函数
  () {
    print("hello world");
  }();
}

闭包

void main() {
  // 立即执行函数
  var add = myAdd();
  add();
  add();
}

myAdd() {
  int a = 0;

  return () {
    print(a++);
  };
}

类和对象

使用class创建类。

void main() {
  Student xiaoming = Student();
  xiaoming.name = "小明";
  xiaoming.age = 20;
  print(xiaoming.name);
  print(xiaoming.age);

}

class Student {
  String? name;
  int? age;
}

类的权限

使用下划线的属性和方法只能被当前文件访问,不能被其它文件引用。

构造函数

void main() {
  // 一旦定义了构造函数,则默认构造函数无法继续使用
  // Student xiaoming = Student();

  Student xiaoming = Student("xiaoming", 19);
  print(xiaoming.name);
  print(xiaoming.age);
  
  var xiaoming1 = Student.withNameAndAge("xiaoming", 20);
  print(xiaoming1.name);
  print(xiaoming1.age);
}

class Student {
  String? name;
  int? age;

  // 构造函数
  Student(this.name, this.age);

  // 命名构造函数
  Student.withNameAndAge(this.name, this.age);

}

单例

void main() {
  var s1 = Student();
  var s2 = Student();
  if (s1 == s2) {
    print("same object");
  }

}

class Student {
  // 静态变量
  static Student? _instance;
  
  // 使用factory才可以return
  factory Student() => _instance ??= Student._init();
  
  // 由于有Student构造函数,所以必须使用一个命名构造函数进行初始化
  Student._init();
}

对象方法和类方法

void main() {
  var s1 = Student();
  s1.say();

  Student.good_morning();


}

class Student {
  // 对象方法
  void say(){
    print("老师好");
  }
  
  // 类方法
  static void good_morning(){
    print("good morning");
  }
}

类方法只能访问静态变量。

void main() {
  var s1 = Student();
  Student.good_morning();
}

class Student {
  static int age = 100;

  // 类方法
  static void good_morning(){
    print("maxage is ${age}" );
  }
}