Flutter 填坑整理

Cheney2006發表於2020-04-05

  Flutter是一個全新的跨平臺開發的語言,我在實際專案開發中也有幸使用到 Flutter 進行完整開發,在這個開發過程中也是一步步的實驗,在這其中也遇到了各種各樣的坑, 網上通過各種谷歌,stackoverflow,Flutter官網等查資料,有些問題是解決,但還是有些問題不能及時解決,於是就自己慢慢的嘗試著摸索除了一些解決方案,所以在這裡就整理一下分享給大家,跟大家一起學習、討論。

1、Waiting for another flutter command to release the startup lock...

問題描述

在專案中使用Flutter Packages get時或命令列執行flutter build 時 會 出現: Waiting for another flutter command to release the startup lock...

解決方案:

找到\bin\cache中的lockfile檔案刪除,如果還是不行則重啟IDE重新試下。

2、The context used to push or pop routes from the Navigator must be that of a widget that is a descendant of a Navigator widget.

問題描述

在頁面 initState、build、或者頁面返回沒辦法直接使用 context 或 setState

解決辦法

使用

Future.delayed(Duration.zero).then((e) {
      ....
    });
複製程式碼

3、PageView儲存狀態報錯

問題描述

Build functions must never return null. To return an empty space that causes the building widget to fill available room, return "new Container()". To return an empty space that takes as little room as possible, return "new Container(width: 0.0, height: 0.0)"

解決辦法

在 build 方法必須加上super.build

 @override
  Widget build(BuildContext context) {
    super.build(context); //儲存狀態必須加上
    [...]
  }
複製程式碼

4、EventManager使用問題

post                        : 傳送訊息
on                          : 監聽訊息
destroy                     : 銷燬

複製程式碼

問題描述

錯誤的使用destory 使得整個應用沒辦法監聽訊息

解決辦法

在 BaseWidget 的 dispose不能 destory EventManager。EventManager是單例,基類取消就全部取消了。

5、IOS 調起相機/相簿點選事件穿透

問題描述

在調起相機、相簿選擇圖片時,點選相機區域、圖片選擇區域會響應下層 flutter 介面的點選事件

具體參與:github.com/flutter/flu…

解決辦法

目前處理的辦法是調起相機、相簿時顯示一個帶蒙層的彈窗,收起相機、機冊時取消蒙層彈窗。

6、官方 Webview_flutter 使用問題

問題描述

在 android 中輸入框獲焦點,點選兩次會閃退。

在IOS需要 配置了io.flutter.embedded_views_preview屬性,會導致鍵盤卡頓。

解決辦法

待官方解決,暫時使用第三方flutter_webview_plugin 外掛

7、flutter_webview_plugin 外掛使用問題

問題描述

flutter_webview_plugin是呼叫原生 webview.並加到原生主頁面上。所以該 webview 是最頂層的,無法在其上顯示flutter的檢視

解決辦法

要顯示彈窗等檢視時先判斷當前是否有webview頁面,並且在進入webview頁面時呼叫FlutterWebviewPlugin().show();退出 webviewd頁面時呼叫FlutterWebviewPlugin().hide();

8、命名路由無法關閉指定頁面

問題描述

flutter 關閉彈窗、頁面都會都是呼叫 Navigator.pop(context, result),在這裡沒辦法指定特定的頁面路由或彈窗

解決辦法

修改原始碼把底層的history釋放出來,或者儘量避免這種需要關閉指定頁面的做法。

9、點選控制元件區域沒事件響應

問題描述

正確的設定了控制元件的點選事件,點選卻沒有反應

解決辦法

  • 給控制元件設定背景
  • 設定behavior屬性
return GestureDetector(
                    behavior: HitTestBehavior.translucent,
                    child: Text("測試${index}"),
                    onTap: () {},
                  );


enum HitTestBehavior {
  /// Targets that defer to their children receive events within their bounds
  /// only if one of their children is hit by the hit test.
  deferToChild, //只生效在child的區域比如文字

  /// Opaque targets can be hit by hit tests, causing them to both receive
  /// events within their bounds and prevent targets visually behind them from
  /// also receiving events.
  opaque,//GestureDetector的整個區域,不包括它下面的區域

  /// Translucent targets both receive events within their bounds and permit
  /// targets visually behind them to also receive events.
  translucent,// GestureDetector的整個區域以及它下面的區域
}

複製程式碼

10、Appbar/Tabbar/的高度設定問題

問題描述

Appbar、Tabbar 預設有固定的高度,如何改變其高度

解決辦法

使用PreferredSize。

最後

  如果在使用過程遇到問題,歡迎下方留言交流。

學習資料

請大家不吝點贊!因為您的點贊是對我最大的鼓勵,謝謝!

相關文章