-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add input controller and keyboard service
- Loading branch information
1 parent
7ec5da9
commit 11f453b
Showing
5 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import 'package:flutter/foundation.dart'; | ||
import 'package:flutter_keyboard_visibility/flutter_keyboard_visibility.dart'; | ||
|
||
class KeyboardService { | ||
final KeyboardVisibilityController keyboardVisibilityController = | ||
KeyboardVisibilityController(); | ||
final ValueNotifier<bool> isKeyboardVisible = ValueNotifier<bool>(false); | ||
|
||
void init() { | ||
keyboardVisibilityController.onChange.listen((bool visible) { | ||
isKeyboardVisible.value = visible; | ||
}); | ||
} | ||
|
||
void dispose() { | ||
isKeyboardVisible.dispose(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:ice/app/services/keyboard_service.dart'; | ||
|
||
class InputFieldController { | ||
InputFieldController({ | ||
FocusNode? focusNode, | ||
this.enabled = true, | ||
required this.widgetKey, | ||
this.scrollToOnFocusKey, | ||
}) : focusNode = focusNode ?? FocusNode(); | ||
|
||
final FocusNode focusNode; | ||
final bool enabled; | ||
final GlobalKey widgetKey; | ||
final GlobalKey? scrollToOnFocusKey; | ||
|
||
double scrollPadding = 0.0; | ||
|
||
InputFieldState get _state { | ||
if (!enabled) { | ||
return InputFieldState.disabled; | ||
} else if (focusNode.hasFocus) { | ||
return InputFieldState.focused; | ||
} else { | ||
return InputFieldState.enabled; | ||
} | ||
} | ||
|
||
void _updateState() { | ||
state = _state; | ||
} | ||
|
||
InputFieldState state = InputFieldState.enabled; | ||
|
||
void onInit() { | ||
focusNode.addListener(_updateState); | ||
|
||
if (scrollToOnFocusKey != null) { | ||
final KeyboardService keyboardController = KeyboardService(); | ||
|
||
keyboardController.isKeyboardVisible.addListener(() async { | ||
if (keyboardController.isKeyboardVisible.value) { | ||
await Future<void>.delayed(const Duration(milliseconds: 50)); | ||
|
||
final RenderBox? box1 = | ||
widgetKey.currentContext?.findRenderObject() as RenderBox?; | ||
final RenderBox? box2 = scrollToOnFocusKey!.currentContext | ||
?.findRenderObject() as RenderBox?; | ||
|
||
if (box1 != null && box2 != null) { | ||
final double distance = _getYPosition(box1) - _getYPosition(box2); | ||
|
||
if (distance > 0) { | ||
scrollPadding = distance; | ||
} | ||
} | ||
} | ||
}); | ||
} | ||
} | ||
|
||
static double _getYPosition(RenderBox box) => | ||
box.globalToLocal(Offset.zero).dy - box.size.height; | ||
} | ||
|
||
enum InputFieldState { | ||
enabled, | ||
disabled, | ||
focused, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters