密码键盘

Posted by アライさん on 2020年10月20日

Flutter实现密码键盘。

难点:不要遮挡输入区域,像真正的键盘一样把输入区域顶上去。

使用keyboard_actions。

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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166

class MyPage extends StatefulWidget {
static const routeName = '/myPage';

@override
_MyPageState createState() => _MyPageState();
}

class _MyPageState extends State<MyPage> {

final _node = FocusNode();
final ValueNotifier<String> _valueNotifier = ValueNotifier('');

///CellModel分组
List<List<CellModel>> _cellGroupList = [];

///文字分组
List<List<String>> _textGroupList = [];

///当前cell所在的group中的位置
int _currGroupIndex = 0;

///当前cell所在list中的位置
int _currListIndex = -1;

@override
void initState() {
super.initState();
_initAllCell();
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: SimpleAppbar(
title: '键盘',
),
body: KeyboardActions(
tapOutsideToDismiss: false,
config: KeyboardActionsConfig(
actions: [
KeyboardActionsItem(
focusNode: _node,
displayActionBar: false,
displayDoneButton: false,
displayArrows: false,
toolbarButtons: [],
footerBuilder: (BuildContext context) {
//自定义的键盘,需要implements PreferredSizeWidget,设置高度
return CustomKeyboard(
showNextGroup: true,
tap: (String action) {
_inputText(action);
},
);
}),
],
),
autoScroll: true,
bottomAvoiderScrollPhysics: BouncingScrollPhysics(),
child: Container(
padding: EdgeInsets.symmetric(
horizontal: ScreenUtil().setWidth(26),
),
child: Column(
mainAxisSize: MainAxisSize.max,
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.start,
children: [
//方格组
KeyboardInput<String>(
focusNode: _node,
notifier: _valueNotifier,
builder: (context, text, hasFocus) {
int i = -1;
return Wrap(
runSpacing: ScreenUtil().setHeight(20),
spacing: ScreenUtil().setWidth(13),
runAlignment: WrapAlignment.start,
crossAxisAlignment: WrapCrossAlignment.start,
children: _cellGroupList.map((list) {
i++;
return _createGroup(i, list);
}).toList(),
);
},
),
],
),
),
),
);
}

///创建group视图
Widget _createGroup(int groupIndex, List<CellModel> cellModelList) {
return ConstrainedBox(
constraints: BoxConstraints(
maxWidth: 100,
),
child: GridView.builder(
gridDelegate: SliverGridDelegateWithMaxCrossAxisExtent(
//单个widget的水平最大宽度
maxCrossAxisExtent: ScreenUtil().setWidth(127),
//垂直单个子widget间距
mainAxisSpacing: 0,
//水平单个子widget间距
crossAxisSpacing: 0,
//item宽高比例
childAspectRatio:
ScreenUtil().setWidth(127) / ScreenUtil().setHeight(77),
),
itemBuilder: (context, final index) {
return SquareCell(
statusIndex: cellModelList[index].statusIndex,
text: _textGroupList[groupIndex][index] == ''
? 'P${index + 1}'
: _textGroupList[groupIndex][index],
hasFocus:
(groupIndex == _currGroupIndex && index == _currListIndex),
tap: () {
setState(() {
_currGroupIndex = groupIndex;
_currListIndex = index;
_node.requestFocus();
});
},
);
},
padding: EdgeInsets.zero,
itemCount: cellModelList.length,
physics: NeverScrollableScrollPhysics(),
cacheExtent: ScreenUtil().setHeight(77),
shrinkWrap: true,
),
);
}

///监听键盘输入
_inputText(String action) {

_notifyValueChange();
setState(() {});
}


///初始化所有点
_initAllCell({bool notify = false}) {
_cellGroupList.clear();
_textGroupList.clear();
_cellGroupList.add([CellModel(), CellModel()]);
_textGroupList.add(['', '']);
_currGroupIndex = 0;
_currListIndex = -1;
if (notify) {
setState(() {});
_notifyValueChange();
}

///延迟回收键盘
Future.delayed(Duration(milliseconds: 300), () {
_node.unfocus();
});
}
}