6.4 Pin 组件封装

6.4 Pin 组件封装

image-20220715150208744

实现步骤:


第 1 步:安装插件 pinput

https://pub.flutter-io.cn/packages/pinput

1
flutter pub add pinput

第 2 步:定义参数

作为业务组件放在 lib/common/components

lib/common/components/pin.dart

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/// pin 输入框
class PinPutWidget extends StatelessWidget {
/// 提交事件
final Function(String)? onSubmit;

/// 焦点
final FocusNode? focusNode;

/// 文本编辑控制器
final TextEditingController? controller;

/// 验证函数
final String? Function(String?)? validator;

第 3 步:样式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
@override
Widget build(BuildContext context) {
// 默认
final defaultPinTheme = PinTheme(
width: 45,
height: 45,
textStyle: const TextStyle(
fontSize: 18,
// color: AppColors.surfaceVariant,
fontWeight: FontWeight.w600),
decoration: BoxDecoration(
border: Border.all(color: AppColors.surfaceVariant),
borderRadius: BorderRadius.circular(5),
),
);
// 编辑
final focusedPinTheme = defaultPinTheme.copyDecorationWith(
border: Border.all(color: AppColors.primary),
borderRadius: BorderRadius.circular(5),
);
// 完成
final submittedPinTheme = defaultPinTheme.copyWith(
decoration: defaultPinTheme.decoration?.copyWith(
color: AppColors.surfaceVariant,
),
);

最后:完整代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import 'package:flutter/material.dart';
import 'package:pinput/pinput.dart';

import '../index.dart';

/// pin 输入框
class PinPutWidget extends StatelessWidget {
/// 提交事件
final Function(String)? onSubmit;

/// 焦点
final FocusNode? focusNode;

/// 文本编辑控制器
final TextEditingController? controller;

/// 验证函数
final String? Function(String?)? validator;

const PinPutWidget({
Key? key,
this.onSubmit,
this.focusNode,
this.controller,
this.validator,
}) : super(key: key);

@override
Widget build(BuildContext context) {
// 默认
final defaultPinTheme = PinTheme(
width: 45,
height: 45,
textStyle: const TextStyle(
fontSize: 18,
// color: AppColors.surfaceVariant,
fontWeight: FontWeight.w600),
decoration: BoxDecoration(
border: Border.all(color: AppColors.surfaceVariant),
borderRadius: BorderRadius.circular(5),
),
);
// 编辑
final focusedPinTheme = defaultPinTheme.copyDecorationWith(
border: Border.all(color: AppColors.primary),
borderRadius: BorderRadius.circular(5),
);
// 完成
final submittedPinTheme = defaultPinTheme.copyWith(
decoration: defaultPinTheme.decoration?.copyWith(
color: AppColors.surfaceVariant,
),
);

return Pinput(
defaultPinTheme: defaultPinTheme,
focusedPinTheme: focusedPinTheme,
submittedPinTheme: submittedPinTheme,
length: 6, // 长度
validator: validator, // 验证函数
pinputAutovalidateMode: PinputAutovalidateMode.onSubmit,
showCursor: true, // 显示光标
autofocus: true, // 自动焦点
obscureText: true, // 密码显示
keyboardAppearance: Brightness.light,
focusNode: focusNode, // 焦点
controller: controller, // 本文控制器
onCompleted: onSubmit, // 提交
);
}
}

提交代码到 git