zoukankan      html  css  js  c++  java
  • Flutter基础

    创建简单APP,主要是main.dart

    import 'package:flutter/material.dart';
    
    /*void main() {
      runApp(MyApp());
    }*/
    
    void main() => runApp(MyApp());
    
    class MyApp extends StatelessWidget {
      // This widget is the root of your application.
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          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,
            // This makes the visual density adapt to the platform that you run
            // the app on. For desktop platforms, the controls will be smaller and
            // closer together (more dense) than on mobile platforms.
            visualDensity: VisualDensity.adaptivePlatformDensity,
          ),
          home: new Scaffold(
            appBar: new AppBar(
              title: new Text("First Flutter Demo"),
            ),
            body: new Center(
              child: new Text("It is body"),
            ),
          ),
        );
      }
    }
    • 本示例创建一个Material APP。Material是一种标准的移动端和web端的视觉设计语言。 Flutter提供了一套丰富的Material widgets。
    • main函数使用了(=>)符号, 这是Dart中单行函数或方法的简写
    void main() => runApp(MyApp());   
    等同于
    void main() {
      runApp(MyApp());
    }
    •  ^符号意味着你可以使用此插件的最新版本(大于等于当前版本)慎用
    优点:能使用最新代码
    缺点:编译出错,前一天代码还能编译,今天编译出错情况
    •  下划线前缀标识
    final _suggestions = <WordPair>[];
    在Dart语言中使用下划线前缀标识符,会强制其变成私有的。
    •  赋值操作符
    AA ?? "999"  ///表示如果 AA 为空,返回999
    AA ??= "999" ///表示如果 AA 为空,给 AA 设置成 999
    AA ~/999 ///AA 对于 999 整除
    •  作用域
    Dart 没有关键词 public 、private 等修饰符,_ 下横向直接代表 private ,但是有 @protected 注解 。
    • 类、接口、继承
    Dart 中没有接口,类都可以作为接口,把某个类当做接口实现时,只需要使用 implements ,然后复写父类方法即可。
    
    Dart 中支持 mixins ,按照出现顺序应该为extends 、 mixins 、implements
    • 同步异步
    Dart 中可通过 async/await 或者 Future 定义异步操作,而事实上 async/await 也只是语法糖,最终还是通过编译器转为 Future
    Dart 中另外一种异步操作, async* / yield 或者 Stream 可定义 Stream 异步, async* / yield 也只是语法糖,最终还是通过编译器转为 StreamStream 还支持同步操作。

      1)、Stream 中主要有 StreamStreamControllerStreamSinkStreamSubscription 四个关键对象,大致可以总结为:

      •   StreamController :如类名描述,用于整个 Stream 过程的控制,提供各类接口用于创建各种事件流。

      •   StreamSink :一般作为事件的入口,提供如 addaddStream 等。

      •   Stream :事件源本身,一般可用于监听事件或者对事件进行转换,如 listenwhere

      •   StreamSubscription :事件订阅后的对象,表面上用于管理订阅过等各类操作,如 cacenlpause ,同时在内部也是事件的中转关键。

      2)、一般通过 StreamController 创建 Stream;通过 StreamSink 添加事件;通过 Stream 监听事件;通过 StreamSubscription 管理订阅。

      3)、Stream 中支持各种变化,比如mapexpandwheretake 等操作,同时支持转换为 Future

    • Flutter生命周期
    • initState() 表示当前 State 将和一个 BuildContext 产生关联,但是此时BuildContext 没有完全装载完成,如果你需要在该方法中获取 BuildContext ,可以 new Future.delayed(const Duration(seconds: 0, (){//context}); 一下。

    • didChangeDependencies()initState() 之后调用,当 State 对象的依赖关系发生变化时,该方法被调用,初始化时也会调用。

    • deactivate()State 被暂时从视图树中移除时,会调用这个方法,同时页面切换时,也会调用。

    • dispose() Widget 销毁了,在调用这个方法之前,总会先调用 deactivate()。

    • didUpdateWidgewidget 状态发生变化时,被调用。

     

    基本类型

    • var 可以定义变量,如 var tag = "666" ,这和 JS 、 Kotlin 等语言类似,同时 Dart 也算半个动态类型语言,同时支持闭包。

    • Dart 属于是强类型语言 ,但可以用 var 来声明变量,Dart自推导出数据类型,所以 var 实际上是编译期的“语法糖”。dynamic 表示动态类型, 被编译后,实际是一个 object 类型,在编译期间不进行任何的类型检查,而是在运行期进行类型检查。

    • Dart 中 number 类型分为 intdouble ,其中 java 中的 long 对应的也是 Dart 中的 int 类型,Dart 中没有 float 类型。

    • Dart 下只有 bool 型可以用于 if 等判断,不同于 JS 这种使用方式是不合法的 var g = "null"; if(g){}

    • Dart 中,switch 支持 String 类型。

    变量

    • Dart 不需要给变量设置 setter getter 方法, 这和 kotlin 等语言类似。Dart 中所有的基础类型、类等都继承 Object ,默认值是 NULL, 自带 getter 和 setter ,而如果是 final 或者 const 的话,那么它只有一个 getter 方法。

    • Dart 中 final 和 const 表示常量,比如 final name = 'GSY'; const value= 1000000; 同时 static const 组合代表了静态常量,其中 const 的值在编译期确定,final 的值要到运行时才确定。

    • Dart 下的数值,在作为字符串使用时,是需要显式指定的。比如:int i = 0; print("aaaa" + i); 这样并不支持,需要 print("aaaa" + i.toString()); 这样使用,这和 Java 与 JS 存在差异,所以在使用动态类型时,需要注意不要把 number 类型当做 String 使用。

    • Dart 中数组等于列表,所以 var list = [];List list = new List() 可以简单看做一样。

  • 相关阅读:
    Openstack 通过 SQLAlchemy-ORM 访问数据库
    ulimit -c unlimited
    ajax 调用后台接口示例
    读书有什么用——北漂18年(番外篇三)
    zTree点击文字勾选复选框
    深度剖析 | 基于大数据架构的BI应用
    深度剖析 | 基于大数据架构的BI应用
    AngularJS之对话框
    AngularJS之依赖注入(实例一)
    AngularJS之$watch方法(监控动作)
  • 原文地址:https://www.cnblogs.com/zgz345/p/12930281.html
Copyright © 2011-2022 走看看