For electricity suppliers Amoy Department of APP APP or other similar terms, scouring get the password to the clipboard, and then parse out the specific details of the goods, the entire APP's core business operations, such as the following picture shows.
Here we achieve this function with Flutter, first and foremost front part of the import-related references and general page layout code directly copied just fine.
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
Then there is the main code, we need to implement component is bound observer ( WidgetsBindingObserver
) abstract class, use components ( Widgets
) Layers binding registration class interface, we will cover (its App lifecycle state changes didChangeAppLifecycleState
) method, which returns value type is the application life cycle state ( AppLifecycleState
) class.
Application lifecycle state ( AppLifecycleState
) class has several constants are: wait AppLifecycleState.inactive
( ), pause ( AppLifecycleState. paused
) and resume AppLifecycleState. resumed
( ).
class _MyHomePageState extends State<MyHomePage> with WidgetsBindingObserver {
@override
void initState() {
// 在当前页面放一个观察者。
WidgetsBinding.instance.addObserver(this);
super.initState();
}
@override
void dispose() {
// 移除当前页面的观察者。
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
// 当App生命周期状态为恢复时。
if (state == AppLifecycleState.resumed) {
getClipboardContents();
}
}
/// 使用异步调用获取系统剪贴板的返回值。
getClipboardContents() async {
// 访问剪贴板的内容。
ClipboardData clipboardData = await Clipboard.getData(Clipboard.kTextPlain);
// 剪贴板不为空时。
if (clipboardData != null && clipboardData.text.trim() != '') {
String _name = clipboardData.text.trim();
// 淘口令的正则表达式,能判断类似“¥123456¥”的文本。
if (RegExp(r'[\uffe5]+.+[\uffe5]').hasMatch(_name)) {
// 处理淘口令的业务逻辑。
showDialog<Null>(
context: context,
barrierDismissible: true,
builder: (BuildContext context) {
return CupertinoAlertDialog(
title: Text('淘口令'),
content: Text(_name),
);
},
);
}
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Flutter Demo 主页'),
),
);
}
}
If the above two pieces of code into one file, commissioning, then the APP in the background, and then copy a Amoy password, and then when back to APP, will be something like the following pop-up window.