key_event_utils 0.0.4

  • Readme
  • Changelog
  • Example
  • Installing
  • 43

key_event_utils #

实现条码扫码器扫码事件监听

Pub

主要功能 #

  • 监听手机物理按钮的点击事件,可以自定义设置需要监听的键码值
  • 获取手机物理按钮键码值
  • 监听扫码结束事件
  • 自动清除条码扫码器追加的'\n',返回TextEditingValue可直接用于输入框更新

image

[0.0.4] 2019-12-06 #

  • 添加example、功能说明

[0.0.2] 2019-12-05 #

  • 添加gif效果展示

[0.0.1] 2019-12-04 #

  • 第一次提交
  • TODO: Describe initial release.

example/lib/main.dart

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

import 'package:flutter/services.dart';
import 'package:key_event_utils/key_event_utils.dart';

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

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  TextEditingController controller;
  FocusNode focusNode;

  List<int> keyCodes;
  List<String> barcodeList;

  @override
  void initState() {
    super.initState();
    controller = TextEditingController();
    focusNode = FocusNode();
    keyCodes = [];
    barcodeList = [];

    /// 入栈
    TceKeyEventUtils.instance.push(() {
      if (!focusNode.hasFocus) {
        // 聚焦
        focusNode.requestFocus();
      }
      // 清除
      controller.clear();
    });

    /// 在不清楚条码扫码器的扫码按钮键值时,可以通过setKeyEventListener获取键值
    TceKeyEventUtils.instance.setKeyEventListener((keyCode) {
      if (!keyCodes.contains(keyCode)) {
        setState(() {
          keyCodes.add(keyCode);
        });
      }
    });

    /// 在清楚知道条码扫码器的扫码按钮键值时,可以直接设置需要监听的键值
//    TceKeyEventUtils.instance.setKeyCode(ScanUtils.getDefaultKeyCodes());
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();

    /// 出栈
    TceKeyEventUtils.instance.pop();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Container(
          child: Center(
            child: Column(
              children: <Widget>[
                Text('获取扫码按钮键值:'),
                TextField(
                  controller: controller,
                  focusNode: focusNode,
                  autofocus: true,

                  /// 请注意这里必须要设置null 或者 设置 minLines:1,maxLines:2,
                  /// 如果不设置,条码扫码起追加的 回车符 \n 就不会出现
                  maxLines: null,
//                  minLines: 1,
//                  maxLines: 2,
                  onChanged: (text) {
                    print('onChanged :$text');
                    TextEditingValue value =
                        TceKeyEventUtils.instance.textChangedListener(text);
                    if (value != null) {
                      setState(() {
                        barcodeList.add(text);
                        controller.value = value;
                      });
                    }
                  },
                  decoration: InputDecoration(
                    hintText: '请使用条码扫码器扫码',
                  ),
                ),
                const Divider(),
                Text('条码扫码器-扫码按钮键值列表:'),
                Container(
                  height: 100,
                  child: ListView.builder(
                    itemBuilder: (context, index) {
                      return Container(
                          height: 20,
                          child: Row(
                            children: <Widget>[
                              Expanded(child: Text('${keyCodes[index]}')),
                              FlatButton(
                                onPressed: () {
                                  setState(() {
                                    if (ScanUtils.isOnScan(keyCodes[index])) {
                                      /// 取消监听
                                      ScanUtils.removeKeyCodes(keyCodes[index]);
                                    } else {
                                      /// 设置监听
                                      ScanUtils.setKeyCodes(keyCodes);
                                    }
                                  });
                                },
                                child: Text(
                                    '${ScanUtils.isOnScan(keyCodes[index]) ? '取消监听' : '添加到扫码监听'}'),
                              )
                            ],
                          ));
                    },
                    itemCount: keyCodes?.length ?? 0,
                  ),
                ),
                const Divider(),
                Text('已扫条码列表:'),
                Expanded(
                  child: ListView.builder(
                    itemBuilder: (context, index) {
                      return Container(child: Text('${barcodeList[index]}'));
                    },
                    itemCount: barcodeList?.length ?? 0,
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

Use this package as a library

1. Depend on it

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


dependencies:
  key_event_utils: ^0.0.4

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:key_event_utils/key_event_utils.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
0
Health:
Code health derived from static analysis. [more]
97
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
70
Overall:
Weighted score of the above. [more]
43
Learn more about scoring.

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

  • Dart: 2.8.4
  • pana: 0.13.13
  • Flutter: 1.17.5

Analysis suggestions

Package does not support Flutter platform linux

Because of import path [package:key_event_utils/key_event_utils.dart] that declares support for platforms: android, ios

Package does not support Flutter platform macos

Because of import path [package:key_event_utils/key_event_utils.dart] that declares support for platforms: android, ios

Package does not support Flutter platform web

Because of import path [package:key_event_utils/key_event_utils.dart] that declares support for platforms: android, ios

Package does not support Flutter platform windows

Because of import path [package:key_event_utils/key_event_utils.dart] that declares support for platforms: android, ios

Package not compatible with SDK dart

because of import path [key_event_utils] that is in a package requiring null.

Health suggestions

Fix lib/tce/scan_utils.dart. (-2.48 points)

Analysis of lib/tce/scan_utils.dart reported 5 hints:

line 9 col 20: Name non-constant identifiers using lowerCamelCase.

line 12 col 20: Name non-constant identifiers using lowerCamelCase.

line 15 col 20: Name non-constant identifiers using lowerCamelCase.

line 18 col 20: Name non-constant identifiers using lowerCamelCase.

line 23 col 20: Name non-constant identifiers using lowerCamelCase.

Fix lib/main.dart. (-0.50 points)

Analysis of lib/main.dart reported 1 hint:

line 99 col 50: 'display1' is deprecated and shouldn't be used. This is the term used in the 2014 version of material design. The modern term is headline4. This feature was deprecated after v1.13.8..

Format lib/key_event_utils.dart.

Run flutter format to format lib/key_event_utils.dart.

Maintenance suggestions

The package description is too short. (-20 points)

Add more detail to the description field of pubspec.yaml. Use 60 to 180 characters to describe the package, what it does, and its target use case.

Package is pre-v0.1 release. (-10 points)

While nothing is inherently wrong with versions of 0.0.*, it might mean that the author is still experimenting with the general direction of the API.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.12 1.14.13
meta 1.1.8 1.2.2
sky_engine 0.0.99
typed_data 1.1.6 1.2.0
vector_math 2.0.8 2.1.0-nullsafety
Dev dependencies
flutter_test