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('');
List<List<CellModel>> _cellGroupList = [];
List<List<String>> _textGroupList = [];
int _currGroupIndex = 0;
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) { 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(), ); }, ), ], ), ), ), ); }
Widget _createGroup(int groupIndex, List<CellModel> cellModelList) { return ConstrainedBox( constraints: BoxConstraints( maxWidth: 100, ), child: GridView.builder( gridDelegate: SliverGridDelegateWithMaxCrossAxisExtent( maxCrossAxisExtent: ScreenUtil().setWidth(127), mainAxisSpacing: 0, crossAxisSpacing: 0, 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(); }); } }
|