搜索插件、代码仓与 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")
    );
}

所属分类

源码仓库

使用此插件的项目

暂无相关数据。