focus_detector 1.0.2

  • Readme
  • Changelog
  • Example
  • Installing
  • new75

FocusDetector #

https://pub.dev/packages/focus_detector

Wrapper around Google's VisibilityDetector for combining it with Flutter's WidgetsBindingObserver in order to achieve Android's onResume/onPause and iOS's viewDidAppear/viewDidDisappear on Flutter.

How Does it Work? #

A FocusDetector widget takes in a child and optional onFocusGained/onFocusLost callbacks and fires those as the widget's focus changes. Some cases that trigger the focus change:

  1. When the widget is first created;
  2. When its route is popped back to;
  3. When the app is sent to background/foreground while the widget was visible;

Article #

Bringing onResume/viewDidAppear onPause/viewDidDisappear to Flutter

Sample #

Check out the set-state-focus-detector or bloc-focus-detector branch of the Breaking Bapp repository.

[1.0.2] - 03/20/2020 #

  • Fix background/foreground transition on iOS.

[1.0.1] - 03/18/2020 #

  • Simplified demo project.

[1.0.0] - 03/18/2020 #

  • Added demo project.

[0.0.1] - 03/18/2020 #

  • First release.

example/lib/main.dart

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

void main() {
  runApp(
    MyApp(),
  );
}

class MyApp extends StatelessWidget {
  final _resumeDetectorKey = UniqueKey();

  @override
  Widget build(BuildContext context) => MaterialApp(
        title: 'FocusDetector Example',
        theme: ThemeData(
          primarySwatch: Colors.green,
        ),
        home: FocusDetector(
          key: _resumeDetectorKey,
          child: Scaffold(
            appBar: AppBar(
              title: Text('FocusDetector Example'),
            ),
          ),
          onFocusGained: () {
            print('Focus gained, equivalent to onResume or viewDidAppear');
          },
          onFocusLost: () {
            print('Focus lost, equivalent to onPause or viewDidDisappear');
          },
        ),
      );
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  focus_detector: ^1.0.2

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:focus_detector/focus_detector.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
50
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
75
Learn more about scoring.

We analyzed this package on Mar 31, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.6
  • Flutter: 1.12.13+hotfix.8

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
visibility_detector ^0.1.4 0.1.4
Transitive dependencies
collection 1.14.11 1.14.12
meta 1.1.8
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test