Skip to content

Commit

Permalink
Update NumberBox (#1063)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdlukaa authored May 21, 2024
2 parents 52c0b0e + 4acac81 commit f60c41d
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/flutter_analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
steps:
- uses: actions/checkout@v2
- name: Install Flutter
uses: subosito/flutter-action@master
uses: subosito/flutter-action@main
with:
channel: stable

Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## NEXT
* fix: Scroll issue in `DatePicker`. ([#1054](https://github.com/bdlukaa/fluent_ui/issues/1054))
* feat: Add `NumberBox.textInputAction` and `NumberBox.onEditingComplete` ([#1063](https://github.com/bdlukaa/fluent_ui/pull/1063))

## 4.8.7

Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ dependencies:
git:
url: https://github.com/YehudaKremer/flutter_syntax_view.git
go_router: ^10.0.0
intl: ^0.18.0
intl: ^0.19.0
google_fonts: ^6.1.0

dev_dependencies:
Expand Down
43 changes: 31 additions & 12 deletions lib/src/controls/form/number_box.dart
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,15 @@ class NumberBox<T extends num> extends StatefulWidget {
/// {@macro flutter.widgets.editableText.textDirection}
final TextDirection? textDirection;

/// The type of action button to use for the keyboard.
///
/// Defaults to [TextInputAction.newline] if [keyboardType] is
/// [TextInputType.multiline] and [TextInputAction.done] otherwise.
final TextInputAction? textInputAction;

/// {@macro flutter.widgets.editableText.onEditingComplete}
final VoidCallback? onEditingComplete;

/// Creates a number box.
const NumberBox({
super.key,
Expand Down Expand Up @@ -264,6 +273,8 @@ class NumberBox<T extends num> extends StatefulWidget {
this.selectionHeightStyle = ui.BoxHeightStyle.tight,
this.selectionWidthStyle = ui.BoxWidthStyle.tight,
this.textDirection,
this.textInputAction,
this.onEditingComplete,
});

@override
Expand All @@ -273,7 +284,7 @@ class NumberBox<T extends num> extends StatefulWidget {
class NumberBoxState<T extends num> extends State<NumberBox<T>> {
FocusNode? _internalNode;

FocusNode? get focusNode => widget.focusNode ?? _internalNode;
FocusNode get focusNode => (widget.focusNode ?? _internalNode)!;

OverlayEntry? _entry;

Expand All @@ -295,28 +306,28 @@ class NumberBoxState<T extends num> extends State<NumberBox<T>> {

@override
void initState() {
super.initState();
if (widget.focusNode == null) {
_internalNode ??= _createFocusNode();
}
focusNode!.addListener(_handleFocusChanged);
focusNode.addListener(_handleFocusChanged);

controller.text = widget.value?.toString() ?? '';
super.initState();
}

@override
void dispose() {
_dismissOverlay();
focusNode!.removeListener(_handleFocusChanged);
focusNode.removeListener(_handleFocusChanged);
_internalNode?.dispose();
controller.dispose();
super.dispose();
}

void _handleFocusChanged() {
if (_hasPrimaryFocus != focusNode!.hasPrimaryFocus) {
if (_hasPrimaryFocus != focusNode.hasPrimaryFocus) {
setState(() {
_hasPrimaryFocus = focusNode!.hasPrimaryFocus;
_hasPrimaryFocus = focusNode.hasPrimaryFocus;
});

if (widget.mode == SpinButtonPlacementMode.compact) {
Expand All @@ -342,8 +353,8 @@ class NumberBoxState<T extends num> extends State<NumberBox<T>> {
if (widget.focusNode == null) {
_internalNode ??= _createFocusNode();
}
_hasPrimaryFocus = focusNode!.hasPrimaryFocus;
focusNode!.addListener(_handleFocusChanged);
_hasPrimaryFocus = focusNode.hasPrimaryFocus;
focusNode.addListener(_handleFocusChanged);
}

if (oldWidget.value != widget.value) {
Expand Down Expand Up @@ -471,10 +482,16 @@ class NumberBoxState<T extends num> extends State<NumberBox<T>> {
selectionWidthStyle: widget.selectionWidthStyle,
textDirection: widget.textDirection,
onSubmitted: (_) => updateValue(),
onEditingComplete: updateValue,
onTap: updateValue,
onTapOutside: (_) => updateValue(),
onEditingComplete: widget.onEditingComplete != null
? () {
updateValue();
widget.onEditingComplete!();
}
: null,
onChanged: widget.onTextChange,
textInputAction: widget.textInputAction,
);

return CompositedTransformTarget(
Expand All @@ -487,17 +504,19 @@ class NumberBoxState<T extends num> extends State<NumberBox<T>> {

if (event.logicalKey == LogicalKeyboardKey.pageUp) {
incrementLarge();
return KeyEventResult.handled;
} else if (event.logicalKey == LogicalKeyboardKey.pageDown) {
decrementLarge();
return KeyEventResult.handled;
} else if (event.logicalKey == LogicalKeyboardKey.arrowUp) {
incrementSmall();
return KeyEventResult.handled;
} else if (event.logicalKey == LogicalKeyboardKey.arrowDown) {
decrementSmall();
} else {
return KeyEventResult.ignored;
return KeyEventResult.handled;
}

return KeyEventResult.handled;
return KeyEventResult.ignored;
},
child: Listener(
onPointerSignal: (event) {
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ dependencies:
sdk: flutter

# Used on date and time pickers
intl: '>=0.18.0 <= 1.0.0'
intl: '>=0.19.0 <= 1.0.0'

# Used on TabView
scroll_pos: '>=0.5.0 < 1.0.0'
Expand Down

0 comments on commit f60c41d

Please sign in to comment.