-
Notifications
You must be signed in to change notification settings - Fork 166
/
Copy pathkeyboard_actions.dart
626 lines (559 loc) · 20.2 KB
/
keyboard_actions.dart
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:keyboard_actions/external/keyboard_avoider/bottom_area_avoider.dart';
import 'package:keyboard_actions/external/platform_check/platform_check.dart';
import 'keyboard_actions_config.dart';
import 'keyboard_actions_item.dart';
export 'keyboard_actions_config.dart';
export 'keyboard_actions_item.dart';
export 'keyboard_custom.dart';
const double _kBarSize = 45.0;
const Duration _timeToDismiss = Duration(milliseconds: 110);
enum KeyboardActionsPlatform {
ANDROID,
IOS,
ALL,
}
/// The behavior when tapped outside the keyboard.
///
/// none: no overlay is added;
///
/// opaqueDismiss: an overlay is added which blocks the underneath widgets from
/// gestures. Once tapped, the keyboard will be dismissed;
///
/// translucentDismiss: an overlay is added which permits the underneath widgets
/// to receive gestures. Once tapped, the keyboard will be dismissed;
enum TapOutsideBehavior {
none,
opaqueDismiss,
translucentDismiss,
}
/// A widget that shows a bar of actions above the keyboard, to help customize input.
///
/// To use this class, add it somewhere higher up in your widget hierarchy. Then, from any child
/// widgets, add [KeyboardActionsConfig] to configure it with the [KeyboardAction]s you'd
/// like to use. These will be displayed whenever the wrapped focus nodes are selected.
///
/// This widget wraps a [KeyboardAvoider], which takes over functionality from [Scaffold]: when the
/// focus changes, this class re-sizes [child]'s focused object to still be visible, and scrolls to the
/// focused node. **As such, set [Scaffold.resizeToAvoidBottomInset] to _false_ when using this Widget.**
///
/// We manage resizing ourselves so that:
///
/// 1. using scaffold is not required
/// 2. content is only shrunk as needed (a problem with scaffold)
/// 3. we shrink an additional [_kBarSize] so the keyboard action bar doesn't cover content either.
class KeyboardActions extends StatefulWidget {
/// Any content you want to resize/scroll when the keyboard comes up
final Widget? child;
/// Keyboard configuration
final KeyboardActionsConfig config;
/// If you want the content to auto-scroll when focused; see [KeyboardAvoider.autoScroll]
final bool autoScroll;
/// In case you don't want to enable keyboard_action bar (e.g. You are running your app on iPad)
final bool enable;
/// If you are using keyboard_actions inside a Dialog it must be true
final bool isDialog;
/// Tap outside the keyboard will dismiss this
@Deprecated('Use tapOutsideBehavior instead.')
final bool tapOutsideToDismiss;
/// Tap outside behavior
final TapOutsideBehavior tapOutsideBehavior;
/// If you want to add overscroll. Eg: In some cases you have a [TextField] with an error text below that.
final double overscroll;
/// If you want to control the scroll physics of [BottomAreaAvoider] which uses a [SingleChildScrollView] to contain the child.
final ScrollPhysics? bottomAvoiderScrollPhysics;
/// If you are using [KeyboardActions] for just one textfield and don't need to scroll the content set this to `true`
final bool disableScroll;
/// Does not clear the focus if you tap on the node focused, useful for keeping the text cursor selection working. Usually used with tapOutsideBehavior as translucent
final bool keepFocusOnTappingNode;
/// Override default height of the bar. `null` is dynamic height
final double? barSize;
const KeyboardActions(
{this.child,
this.bottomAvoiderScrollPhysics,
this.enable = true,
this.autoScroll = true,
this.isDialog = false,
@Deprecated('Use tapOutsideBehavior instead.')
this.tapOutsideToDismiss = false,
this.tapOutsideBehavior = TapOutsideBehavior.none,
required this.config,
this.overscroll = 12.0,
this.disableScroll = false,
this.keepFocusOnTappingNode = false,
this.barSize = _kBarSize})
: assert(child != null);
@override
KeyboardActionstate createState() => KeyboardActionstate();
}
/// State class for [KeyboardActions].
class KeyboardActionstate extends State<KeyboardActions>
with WidgetsBindingObserver {
/// The currently configured keyboard actions
KeyboardActionsConfig? config;
/// private state
Map<int, KeyboardActionsItem> _map = Map();
KeyboardActionsItem? _currentAction;
int? _currentIndex = 0;
OverlayEntry? _overlayEntry;
double _offset = 0;
PreferredSizeWidget? _currentFooter;
bool _dismissAnimationNeeded = true;
final _keyParent = GlobalKey();
Completer<void>? _dismissAnimation;
/// If the keyboard bar is on for the current platform
bool get _isAvailable {
return config!.keyboardActionsPlatform == KeyboardActionsPlatform.ALL ||
(config!.keyboardActionsPlatform == KeyboardActionsPlatform.IOS &&
PlatformCheck.isIOS) ||
(config!.keyboardActionsPlatform == KeyboardActionsPlatform.ANDROID &&
PlatformCheck.isAndroid);
}
/// If we are currently showing the keyboard bar
bool get _isShowing {
return _overlayEntry != null;
}
/// The current previous index, or null.
int? get _previousIndex {
final nextIndex = _currentIndex! - 1;
return nextIndex >= 0 ? nextIndex : null;
}
/// The current next index, or null.
int? get _nextIndex {
final nextIndex = _currentIndex! + 1;
return nextIndex < _map.length ? nextIndex : null;
}
/// The distance from the bottom of the KeyboardActions widget to the
/// bottom of the view port.
///
/// Used to correctly calculate the offset to "avoid" with BottomAreaAvoider.
double get _distanceBelowWidget {
if (_keyParent.currentContext != null) {
final widgetRenderBox =
_keyParent.currentContext!.findRenderObject() as RenderBox;
final fullHeight = MediaQuery.of(context).size.height;
final widgetHeight = widgetRenderBox.size.height;
final widgetTop = widgetRenderBox.localToGlobal(Offset.zero).dy;
final widgetBottom = widgetTop + widgetHeight;
final distanceBelowWidget = fullHeight - widgetBottom;
return distanceBelowWidget;
}
return 0;
}
/// Set the config for the keyboard action bar.
void setConfig(KeyboardActionsConfig newConfig) {
clearConfig();
config = newConfig;
for (int i = 0; i < config!.actions!.length; i++) {
_addAction(i, config!.actions![i]);
}
_startListeningFocus();
}
/// Clear any existing configuration. Unsubscribe from focus listeners.
void clearConfig() {
_dismissListeningFocus();
_clearAllFocusNode();
config = null;
}
void _addAction(int index, KeyboardActionsItem action) {
_map[index] = action;
}
void _clearAllFocusNode() {
_map = Map();
}
void _clearFocus() {
_currentAction?.focusNode.unfocus();
}
Future<Null> _focusNodeListener() async {
bool hasFocusFound = false;
_map.keys.forEach((key) {
final currentAction = _map[key]!;
if (currentAction.focusNode.hasFocus) {
hasFocusFound = true;
_currentAction = currentAction;
_currentIndex = key;
return;
}
});
_focusChanged(hasFocusFound);
}
void _shouldGoToNextFocus(KeyboardActionsItem action, int? nextIndex) async {
_dismissAnimationNeeded = true;
_currentAction = action;
_currentIndex = nextIndex;
//remove focus for unselected fields
_map.keys.forEach((key) {
final currentAction = _map[key]!;
if (currentAction == _currentAction &&
currentAction.footerBuilder != null) {
_dismissAnimationNeeded = false;
}
if (currentAction != _currentAction) {
currentAction.focusNode.unfocus();
}
});
//if it is a custom keyboard then wait until the focus was dismissed from the others
if (_currentAction!.footerBuilder != null) {
await Future.delayed(
Duration(milliseconds: _timeToDismiss.inMilliseconds),
);
}
FocusScope.of(context).requestFocus(_currentAction!.focusNode);
await Future.delayed(const Duration(milliseconds: 100));
bottomAreaAvoiderKey.currentState?.scrollToOverscroll();
}
void _onTapUp() {
if (_previousIndex != null) {
final currentAction = _map[_previousIndex!]!;
if (currentAction.enabled) {
_shouldGoToNextFocus(currentAction, _previousIndex);
} else {
_currentIndex = _previousIndex;
_onTapUp();
}
}
}
void _onTapDown() {
if (_nextIndex != null) {
final currentAction = _map[_nextIndex!]!;
if (currentAction.enabled) {
_shouldGoToNextFocus(currentAction, _nextIndex);
} else {
_currentIndex = _nextIndex;
_onTapDown();
}
}
}
/// Shows or hides the keyboard bar as needed, and re-calculates the overlay offset.
///
/// Called every time the focus changes, and when the app is resumed on Android.
void _focusChanged(bool showBar) async {
if (_isAvailable) {
if (_dismissAnimation != null) {
// wait for the previous animation to complete
await _dismissAnimation?.future;
}
if (showBar && !_isShowing) {
_insertOverlay();
} else if (!showBar && _isShowing) {
_removeOverlay();
} else if (showBar && _isShowing) {
if (PlatformCheck.isAndroid) {
_updateOffset();
}
_overlayEntry!.markNeedsBuild();
}
if (_currentAction != null && _currentAction!.footerBuilder != null) {
WidgetsBinding.instance.addPostFrameCallback((_) {
_updateOffset();
});
}
}
}
@override
void didChangeMetrics() {
if (PlatformCheck.isAndroid) {
final value = WidgetsBinding.instance.window.viewInsets.bottom;
bool keyboardIsOpen = value > 0;
_onKeyboardChanged(keyboardIsOpen);
isKeyboardOpen = keyboardIsOpen;
}
// Need to wait a frame to get the new size
WidgetsBinding.instance.addPostFrameCallback((_) {
_updateOffset();
});
}
void _startListeningFocus() {
_map.values
.forEach((action) => action.focusNode.addListener(_focusNodeListener));
}
void _dismissListeningFocus() {
_map.values.forEach(
(action) => action.focusNode.removeListener(_focusNodeListener));
}
bool _inserted = false;
/// Insert the keyboard bar as an Overlay.
///
/// This will be inserted above everything else in the MaterialApp, including dialog modals.
///
/// Position the overlay based on the current [MediaQuery] to land above the keyboard.
void _insertOverlay() {
OverlayState os = Overlay.of(context);
_inserted = true;
_overlayEntry = OverlayEntry(builder: (context) {
// Update and build footer, if any
_currentFooter = (_currentAction!.footerBuilder != null)
? _currentAction!.footerBuilder!(context)
: null;
final queryData = MediaQuery.of(context);
return Stack(
children: [
if (widget.tapOutsideBehavior != TapOutsideBehavior.none ||
// ignore: deprecated_member_use_from_same_package
widget.tapOutsideToDismiss)
Positioned.fill(
child: Listener(
onPointerDown: (event) {
if (!widget.keepFocusOnTappingNode ||
_currentAction?.focusNode.rect.contains(event.position) !=
true) {
_clearFocus();
}
},
behavior: widget.tapOutsideBehavior ==
TapOutsideBehavior.translucentDismiss
? HitTestBehavior.translucent
: HitTestBehavior.opaque,
),
),
Positioned(
left: 0,
right: 0,
bottom: queryData.viewInsets.bottom,
child: Material(
color: config!.keyboardBarColor ?? Colors.grey[200],
elevation: config!.keyboardBarElevation ?? 20,
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
if (_currentAction!.displayActionBar)
_buildBar(_currentAction!.displayArrows),
if (_currentFooter != null)
AnimatedContainer(
duration: _timeToDismiss,
child: _currentFooter,
height:
_inserted ? _currentFooter!.preferredSize.height : 0,
),
],
),
),
),
],
);
});
os.insert(_overlayEntry!);
}
/// Remove the overlay bar. Call when losing focus or being dismissed.
void _removeOverlay({bool fromDispose = false}) async {
_inserted = false;
if (_currentFooter != null && _dismissAnimationNeeded) {
if (mounted && !fromDispose) {
_overlayEntry?.markNeedsBuild();
// add a completer to indicate the completion of dismiss animation.
_dismissAnimation = Completer<void>();
await Future.delayed(_timeToDismiss);
_dismissAnimation?.complete();
_dismissAnimation = null;
}
}
_overlayEntry?.remove();
_overlayEntry = null;
_currentFooter = null;
if (!fromDispose && _dismissAnimationNeeded) _updateOffset();
_dismissAnimationNeeded = true;
}
void _updateOffset() {
if (!mounted) {
return;
}
if (!_isShowing || !_isAvailable) {
setState(() {
_offset = 0.0;
});
return;
}
double newOffset = _currentAction!.displayActionBar
? _kBarSize
: 0; // offset for the actions bar
final keyboardHeight = EdgeInsets.fromWindowPadding(
WidgetsBinding.instance.window.viewInsets,
WidgetsBinding.instance.window.devicePixelRatio)
.bottom;
newOffset += keyboardHeight; // + offset for the system keyboard
if (_currentFooter != null) {
newOffset +=
_currentFooter!.preferredSize.height; // + offset for the footer
}
newOffset -= _localMargin + _distanceBelowWidget;
if (newOffset < 0) newOffset = 0;
// Update state if changed
if (_offset != newOffset) {
setState(() {
_offset = newOffset;
});
}
}
double _localMargin = 0.0;
void _onLayout() {
if (widget.isDialog) {
final render =
_keyParent.currentContext?.findRenderObject() as RenderBox?;
final fullHeight = MediaQuery.of(context).size.height;
final localHeight = render?.size.height ?? 0;
_localMargin = (fullHeight - localHeight) / 2;
}
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
if (defaultTargetPlatform == TargetPlatform.android) {
if (state == AppLifecycleState.paused) {
FocusScope.of(context).requestFocus(FocusNode());
_focusChanged(false);
}
}
super.didChangeAppLifecycleState(state);
}
@override
void didUpdateWidget(KeyboardActions oldWidget) {
if (widget.enable) setConfig(widget.config);
super.didUpdateWidget(oldWidget);
}
@override
void dispose() {
clearConfig();
_removeOverlay(fromDispose: true);
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
@override
void initState() {
WidgetsBinding.instance.addObserver(this);
if (widget.enable) {
setConfig(widget.config);
WidgetsBinding.instance.addPostFrameCallback((_) {
_onLayout();
_updateOffset();
});
}
super.initState();
}
var isKeyboardOpen = false;
void _onKeyboardChanged(bool isVisible) {
bool footerHasSize = _checkIfFooterHasSize();
if (!isVisible && isKeyboardOpen && !footerHasSize) {
_clearFocus();
}
}
bool _checkIfFooterHasSize() {
return _currentFooter != null &&
(_currentFooter?.preferredSize.height ?? 0) > 0;
}
/// Build the keyboard action bar based on the current [config].
Widget _buildBar(bool displayArrows) {
return AnimatedCrossFade(
duration: _timeToDismiss,
crossFadeState:
_isShowing ? CrossFadeState.showFirst : CrossFadeState.showSecond,
firstChild: Container(
height: widget.barSize,
width: MediaQuery.of(context).size.width,
decoration: BoxDecoration(
border: Border(
top: BorderSide(
color: widget.config.keyboardSeparatorColor,
width: 1.0,
),
),
),
child: SafeArea(
top: false,
bottom: false,
child: Row(
mainAxisAlignment:
_currentAction?.toolbarAlignment ?? MainAxisAlignment.end,
children: [
if (config!.nextFocus && displayArrows) ...[
IconButton(
icon: Icon(Icons.keyboard_arrow_up),
tooltip: 'Previous',
iconSize: IconTheme.of(context).size!,
color: IconTheme.of(context).color,
disabledColor: Theme.of(context).disabledColor,
onPressed: _previousIndex != null ? _onTapUp : null,
),
IconButton(
icon: Icon(Icons.keyboard_arrow_down),
tooltip: 'Next',
iconSize: IconTheme.of(context).size!,
color: IconTheme.of(context).color,
disabledColor: Theme.of(context).disabledColor,
onPressed: _nextIndex != null ? _onTapDown : null,
),
const Spacer(),
],
if (_currentAction?.displayDoneButton != null &&
_currentAction!.displayDoneButton &&
(_currentAction!.toolbarButtons == null ||
_currentAction!.toolbarButtons!.isEmpty))
Padding(
padding: const EdgeInsets.all(5.0),
child: InkWell(
onTap: () {
if (_currentAction?.onTapAction != null) {
_currentAction!.onTapAction!();
}
_clearFocus();
},
child: Container(
padding:
EdgeInsets.symmetric(vertical: 8.0, horizontal: 12.0),
child: config?.defaultDoneWidget ??
Text(
"Done",
style: TextStyle(
fontSize: 16.0,
fontWeight: FontWeight.w500,
),
),
),
),
),
if (_currentAction?.toolbarButtons != null)
..._currentAction!.toolbarButtons!
.map((item) => item(_currentAction!.focusNode))
.toList()
],
),
),
),
secondChild: const SizedBox.shrink(),
);
}
final GlobalKey<BottomAreaAvoiderState> bottomAreaAvoiderKey =
GlobalKey<BottomAreaAvoiderState>();
@override
Widget build(BuildContext context) {
// Return the given child wrapped in a [KeyboardAvoider].
// We will call [_buildBar] and insert it via overlay on demand.
// Add [_kBarSize] padding to ensure we scroll past the action bar.
// We need to add this sized box to support embedding in IntrinsicWidth
// areas, like AlertDialog. This is because of the LayoutBuilder KeyboardAvoider uses
// if it has no child ScrollView.
// If we don't, we get "LayoutBuilder does not support returning intrinsic dimensions".
// See https://github.com/flutter/flutter/issues/18108.
// The SizedBox can be removed when thats fixed.
return widget.enable && !widget.disableScroll
? Material(
color: Colors.transparent,
child: SizedBox(
width: double.maxFinite,
key: _keyParent,
child: BottomAreaAvoider(
key: bottomAreaAvoiderKey,
areaToAvoid: _offset,
overscroll: widget.overscroll,
duration: Duration(
milliseconds:
(_timeToDismiss.inMilliseconds * 1.8).toInt()),
autoScroll: widget.autoScroll,
physics: widget.bottomAvoiderScrollPhysics,
child: widget.child,
),
),
)
: widget.child!;
}
}