Flutter基礎知識積累

一 Dart基礎:

  1. Flutter使用Dart進行開發,Dart支持JIT與AOT兩種模式。app運行模式有三種:
  • debug :調試模式-JIT。
  • profile:與release模式類似-AOT,支持檢測幀率,可做內存優化時調試。
  • release :生產模式-AOT。
  1. [ .. ]該符號叫做級聯,與this.相當,[ _ ]代表私有變量,
    a ?? 1 :表示a如果為空則返回1; a ??= 1:表示a如果為空則a賦值為1;a ~/22 :表示a對于 22 整除,方法可選參數如下代碼,也可進行默認值賦值:
void start({int length = 0}){}。
  1. Dart是單線程模型,運行模式如下:
    main->microtask queue(微任務)-> event queue(事物),
    main先執行,循環執行microtask隊列,全部微任務執行完畢再執行event事物隊列的任務。一般用的Future屬于事物隊列??梢酝ㄟ^Future.microtask來創建微任務。
  2. 在Dart中,一切都是對象,所有的對象都是繼承自Object,Dart是強類型語言,但可以用var或 dynamic來聲明一個變量,Dart會自動推斷其數據類型,沒有賦初值的變量都會有默認值null。
  3. Dart 中的多構造方法,可以通過命名方法實現。默認構造方法只能有一個,而通過 CSDart.empty() 方法可以創建一個空參數的類,其實方法名稱隨你取,而變量初始化值時,只需要通過 this.length 在構造方法中指定即可:
class CSDart {
  String length;  
  //默認構造方法,賦值給length
  CSDart(this.length);
  //返回一個空的CSDart
  CSDart.empty();
  //返回一個設置了length的CSDart
  CSDart.forName(this.length);
}

二 Flutter基礎:

  1. Flutter生命周期相關的知識:
  • initState():Widget 初始化當前 State,在當前方法中是不能獲取到 Context 的,如想獲取,可以加延遲如:Future.delayed()。
  • didChangeDependencies():在 initState() 后調用,Widget對象依賴關系發生變化的時候也會調用。如語言,樣式。
  • build():畫出界面,當調用setState時執行。
  • deactivate():當 State 被暫時從視圖樹中移除時會調用這個方法,頁面切換時也會調用該方法,和Android里的 onPause 差不多。
  • dispose():Widget 銷毀時調用。
  • didUpdateWidget:Widget 狀態發生變化的時候調用。
  • reassemble()
    主要用于調試,熱重載時調用,release環境下不會調用
  • A頁面跳轉B頁面,并且B頁面回退A頁面會執行的什么周期方法如下:
//a到b頁面
I/flutter (29569):binitState
I/flutter (29569): bdidChangeDependencies
I/flutter (29569): bbuild
I/flutter (29569): adeactivate
I/flutter (29569): adidChangeDependencies
I/flutter (29569): abuild
//b退到a: 
I/flutter (29569): adeactivate
I/flutter (29569): adidChangeDependencies
I/flutter (29569): abuild
I/flutter (29569): bdeactivate
I/flutter (29569): bdispose
  • 可以通過with WidgetsBindingObserver混入次類并重新如下方法,進行監聽Home建。
 @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    switch (state) {
      case AppLifecycleState.inactive://前后臺切換都執行
        debugPrint('AppLifecycleState.inactive');
        break;
      case AppLifecycleState.paused://前臺到后臺
        debugPrint('AppLifecycleState.paused');
        break;
      case AppLifecycleState.resumed://后臺到前臺
        debugPrint('AppLifecycleState.resumed');
       if (Platform.isAndroid) {
          // 以下兩行 設置android狀態欄為透明的沉浸。寫在組件渲染之后,是為了在渲染后進行set賦值,覆蓋狀態欄,寫在渲染之前MaterialApp組件會覆蓋掉這個值。
          SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
        }
        break;
      case AppLifecycleState.suspending:
        debugPrint('AppLifecycleState.suspending');
        break;
    }
    super.didChangeAppLifecycleState(state);
  }
  • 通過MaterialApp->navigatorObservers關鍵字可對頁面進出進行監聽,(如防截屏操作可使用)該類提供的方法如下:
   /// The [Navigator] pushed `route`.
  ///
  /// The route immediately below that one, and thus the previously active
  /// route, is `previousRoute`.
  void didPush(Route<dynamic> route, Route<dynamic> previousRoute) { }

  /// The [Navigator] popped `route`.
  ///
  /// The route immediately below that one, and thus the newly active
  /// route, is `previousRoute`.
  void didPop(Route<dynamic> route, Route<dynamic> previousRoute) { }

  /// The [Navigator] removed `route`.
  ///
  /// If only one route is being removed, then the route immediately below
  /// that one, if any, is `previousRoute`.
  ///
  /// If multiple routes are being removed, then the route below the
  /// bottommost route being removed, if any, is `previousRoute`, and this
  /// method will be called once for each removed route, from the topmost route
  /// to the bottommost route.
  void didRemove(Route<dynamic> route, Route<dynamic> previousRoute) { }

  /// The [Navigator] replaced `oldRoute` with `newRoute`.
  void didReplace({ Route<dynamic> newRoute, Route<dynamic> oldRoute }) { }

  /// The [Navigator]'s route `route` is being moved by a user gesture.
  ///
  /// For example, this is called when an iOS back gesture starts.
  ///
  /// Paired with a call to [didStopUserGesture] when the route is no longer
  /// being manipulated via user gesture.
  ///
  /// If present, the route immediately below `route` is `previousRoute`.
  /// Though the gesture may not necessarily conclude at `previousRoute` if
  /// the gesture is canceled. In that case, [didStopUserGesture] is still
  /// called but a follow-up [didPop] is not.
  void didStartUserGesture(Route<dynamic> route, Route<dynamic> previousRoute) { }

  /// User gesture is no longer controlling the [Navigator].
  ///
  /// Paired with an earlier call to [didStartUserGesture].
  void didStopUserGesture() { }
  1. Flutter與Native通信方式:
  • Flutter 通過 PlatformChannel 與原生進行交互,其中 PlatformChannel 分為三種:
    a. BasicMessageChannel:用于傳遞字符串和半結構化的信息。
    b. MethodChannel:用于傳遞方法調用。Flutter主動調用Native的方法,并獲取相應的返回值。
    c. EventChannel:用于數據流(event streams)的通信。
    詳解可參考咸魚講解的PlatformChannel
  1. Stream的理解:Stream 和 Future 一樣,都是用來處理異步的工具。但是 Stream 和 Future 不同的地方是 Stream 可以接收多個異步結果,而Future 只有一個。Stream 的創建可以使用 Stream.fromFuture,也可以使用 StreamController 來創建和控制。Stream支持兩種模式,一種是單訂閱模式一種是多訂閱模式。
    由于Stream 默認處于單訂閱模式,所以同一個 stream 上的 listen 和其它大多數方法只能調用一次,調用第二次就會報錯。但 Stream 可以通過 transform() 方法(返回另一個 Stream)進行連續調用。通過 Stream.asBroadcastStream() 可以將一個單訂閱模式的 Stream 轉換成一個多訂閱模式的 Stream,isBroadcast 屬性可以判斷當前 Stream 所處的模式。
    可以通過使用await for是不斷獲取stream流中的數據,然后執行循環體中的操作。它一般用在直到stream什么時候完成,并且必須等待傳遞完成之后才能使用,不然就會一直阻塞。如下:
Stream<String> stream = new Stream<String>.fromIterable(['起床', '刷牙', '洗臉', '上班了']);
main() async{
  print('早上8:00了');
  await for(String s in stream){
    print(s);
  }
  print('開始上班');
}
  1. 后續繼續學習補充
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容