搜尋套件、儲存庫與 APP 分類

套件詳情v2.0.0

flutter_focus_watcher

一個新的 Flutter 套件。

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
290
喜歡數
23
Pub 分數
140

安裝套件

flutter pub add flutter_focus_watcher

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

Flutter Focus Watcher 是一個專為監控小部件焦點變更而設計的 Flutter 套件,特別適用於表單處理。它讓開發者能夠偵測小部件何時取得或失去焦點,進而實現回應式 UI 行為,例如強調聚焦欄位或觸發驗證。此套件支援所有主要平台,包括 Android、iOS、Web、macOS、Windows 和 Linux,非常適合跨平台應用。透過 pub add 進行最少設定,即可順暢整合至現有的表單與輸入工作流程中。

適用情境

  • 監控表單中的焦點狀態
  • 在焦點變更時觸發驗證
  • 透過焦點反饋增強可訪問性
  • 基於焦點實現動態UI更新
  • 追蹤使用者互動模式
  • 改進複雜輸入中的表單可用性

主要特色

  • 即時焦點變更檢測
  • 跨平台相容性
  • 與現有組件的簡單整合
  • 支援獲得與失去焦點事件
  • 輕量且高效
  • 適用於文字欄位和自訂可聚焦元件

適合對象

  • 表單密集型應用
  • 面向可存取性的開發
  • 互動式 UI 組件
  • 跨平台行動與Web應用
  • 尋求即時輸入反饋的開發者
  • 需要進階表單狀態管理的應用程式

使用建議

  • 在複雜場景中需要手動處理焦點狀態
  • 可能需要額外的邏輯來處理巢狀或動態表單
  • 不適用於非表單使用案例
  • 僅限聚焦相關事件
  • 最好與其他表單驗證套件一起使用
  • 性能影響可忽略不計,但應在大型表單中進行測試

原始 README

以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。

Flutter Focus Watcher

This widget is used to remove TextField focus if a tap occured somewhere else In order for this to work properly, it must be placed inside MaterialApp or WidgetsApp. This is necessary because this widget requires MediaQuery, which is supplied by those two.

The widget also keeps track of a keyboard space and moves the whole application up if a focused TextField gets obscured by the keyboard The only additional thing you need to do for it is to set your Scaffold's resizeToAvoidBottomInset to false (in case you use scaffold) and that's it. If you want to change the height of application lift, simply set your preferred value to the FocusWatcher's liftOffset variable. The default value is 15.0. This means the number of points above the keyboard's upper bound

Example

It's very easy to use. You may add it as a library or simply copy flutter_focus_watcher.dart into your project. It doesn't have any external dependencies

Then simply do this. Notice that FocusWatcher is inside MaterialApp. That's because it inherits MediaQuery from that

IMPORTANT! You must wrap with the FocusWatcher every scaffold where you want this functionality

import 'package:flutter/material.dart';
import 'package:flutter_focus_watcher/flutter_focus_watcher.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
   Widget build(BuildContext context) {
      return MaterialApp(
        title: 'Demo app',
        theme: ThemeData(
          primarySwatch: Colors.blue,
        ),
        home: MyHomePage(title: 'Flutter Demo Home Page')
      );
    }
}

 @override
  Widget build(BuildContext context) {
    return FocusWatcher(
        child: Scaffold(
         // don't forget about this
         resizeToAvoidBottomInset: false,
         ...
       )
    );
     
  }

And it will do the rest.

alt watcher

And here is how it handles the keyboard

alt keyboard

In case you want to exclude some widget from this workflow, simply wrap that widget with

@override
Widget build(BuildContext context) {
    return IgnoreFocusWatcher(
      child: TextField("I will be ignored by FocusWatcher")
    );
}

And this widget will be ignored by FocusWatcher

There may also be some cases when a TextField (like material text field) does not apply a RenderEditable or RenderParagraph thus making this plugin useless. You can simply fix it by wrapping the TextField in a ForceFocusWatcher() widget. That's it

@override
Widget build(BuildContext context) {
    return ForceFocusWatcher(
      child: TextField("I'm forced to activate focus watcher")
    );
}

所屬分類

原始碼儲存庫

使用此套件的專案

暫無相關資料。