Skip to content

Commit

Permalink
support dynamic widget height
Browse files Browse the repository at this point in the history
  • Loading branch information
henryleunghk committed Aug 12, 2020
1 parent 21e7d30 commit 977e263
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 19 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.1.0

* support dynamic widget height with plugin documentation added

## 0.0.2

* update usage example
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@ Hope you find it useful! Enjoy coding 🎉🎉🎉

| Name | Type | Description | Default |
|:----------------|:--------------|:-------------------------------|:-------------------------|
| `controller` | TextEditingController | Controlling the text being edited (https://api.flutter.dev/flutter/material/TextField/controller.html) | null |
| `controller` | TextEditingController | Controlling the text being edited (https://api.flutter.dev/flutter/material/TextField/controller.html) | null |
| `placeholder` | String | Placeholder text when text entry is empty (https://api.flutter.dev/flutter/cupertino/CupertinoTextField/placeholder.html) | null |
| `textContentType` | TextContentType | To identify the semantic meaning expected for a text-entry area (https://developer.apple.com/documentation/uikit/uitextcontenttype) | null |
| `keyboardType` | KeyboardType | Type of keyboard to display for a given text-based view (https://developer.apple.com/documentation/uikit/uikeyboardtype) | KeyboardType.defaultType |
| `onChanged` | ValueChanged\<String> | Called when the user initiates a change to text entry (https://api.flutter.dev/flutter/material/TextField/onChanged.html) | null |
| `onSubmitted` | ValueChanged\<String> | Called when the user indicates that they are done editing the text in the field (https://api.flutter.dev/flutter/material/TextField/onSubmitted.html) | null |
| `focusNode` | FocusNode | Defines the keyboard focus for this widget (https://api.flutter.dev/flutter/material/TextField/focusNode.html) | null |
| `textAlign` | TextAlign | How the text should be aligned horizontally (https://api.flutter.dev/flutter/material/TextField/textAlign.html) | TextAlign.start |
| `minLines` | int | Minimum number of lines of text input widget | 1 |
| `maxLines` | int | Maximum number of lines of text input body, 0 for no limit | 1 |
| `height` | double | Height of this widget | 36.0 |

## More examples

Expand Down
4 changes: 2 additions & 2 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
PODS:
- Flutter (1.0.0)
- flutter_native_text_input (0.0.1):
- flutter_native_text_input (0.1.0):
- Flutter

DEPENDENCIES:
Expand All @@ -15,7 +15,7 @@ EXTERNAL SOURCES:

SPEC CHECKSUMS:
Flutter: 0e3d915762c693b495b44d77113d4970485de6ec
flutter_native_text_input: 23447c3f8cd67b956841cf744f2f993380355e8a
flutter_native_text_input: 10d9e348e752337f2027d073e719d1b9ba56f76e

PODFILE CHECKSUM: aff02bfeed411c636180d6812254b2daeea14d09

Expand Down
4 changes: 2 additions & 2 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ class _MyAppState extends State<MyApp> {
class HomePage extends StatelessWidget {
final FocusNode _focusNode = FocusNode();

_onChangeText(value) => debugPrint("inputValueChanged: $value");
_onSubmittedText(value) => debugPrint("onSubmitted: $value");
_onChangeText(value) => debugPrint("_onChangeText: $value");
_onSubmittedText(value) => debugPrint("_onSubmittedText: $value");

@override
Widget build(BuildContext context) {
Expand Down
2 changes: 1 addition & 1 deletion example/lib/more_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class _MorePageState extends State<MorePage> {
DemoItem(
title: "Multiline Text Input",
child: NativeTextInput(
height: 72,
minLines: 3,
maxLines: 0,
)),
DemoItem(
Expand Down
2 changes: 1 addition & 1 deletion ios/flutter_native_text_input.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#
Pod::Spec.new do |s|
s.name = 'flutter_native_text_input'
s.version = '0.0.2'
s.version = '0.1.0'
s.summary = 'Native text input for Flutter'
s.description = <<-DESC
Native text input for Flutter
Expand Down
40 changes: 30 additions & 10 deletions lib/flutter_native_text_input.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ class NativeTextInput extends StatefulWidget {
this.onSubmitted,
this.focusNode,
this.textAlign = TextAlign.start,
this.minLines = 1,
this.maxLines = 1,
this.height = 36,
}) : super(key: key);

/// Controls the text being edited.
Expand Down Expand Up @@ -112,7 +112,7 @@ class NativeTextInput extends StatefulWidget {

final int maxLines;

final double height;
final int minLines;

@override
State<StatefulWidget> createState() => _NativeTextInputState();
Expand All @@ -122,12 +122,14 @@ class _NativeTextInputState extends State<NativeTextInput> {
MethodChannel _channel;

TextEditingController _controller;
TextEditingController get _effectiveController => widget.controller ?? (_controller ??= TextEditingController());
TextEditingController get _effectiveController =>
widget.controller ?? (_controller ??= TextEditingController());

FocusNode _focusNode;
FocusNode get _effectiveFocusNode => widget.focusNode ?? (_focusNode ??= FocusNode());
FocusNode get _effectiveFocusNode =>
widget.focusNode ?? (_focusNode ??= FocusNode());

bool get _isMultiline => widget.maxLines > 1;
bool get _isMultiline => widget.maxLines == 0 || widget.maxLines > 1;
int _currentLineIndex = 1;

@override
Expand All @@ -146,15 +148,17 @@ class _NativeTextInputState extends State<NativeTextInput> {

if (widget.controller != null) {
widget.controller.addListener(() {
_channel.invokeMethod("setText", {"text": widget.controller.text ?? ""});
_channel
.invokeMethod("setText", {"text": widget.controller.text ?? ""});
});
}
}

@override
Widget build(BuildContext context) {
return ConstrainedBox(
constraints: BoxConstraints(minHeight: widget.height, maxHeight: widget.height * _currentLineIndex),
constraints:
BoxConstraints(minHeight: _minHeight(), maxHeight: _maxHeight()),
child: UiKitView(
viewType: "flutter_native_text_input",
creationParamsCodec: const StandardMessageCodec(),
Expand All @@ -164,7 +168,8 @@ class _NativeTextInputState extends State<NativeTextInput> {
}

void _createMethodChannel(int nativeViewId) {
_channel = MethodChannel("flutter_native_text_input$nativeViewId")..setMethodCallHandler(_onMethodCall);
_channel = MethodChannel("flutter_native_text_input$nativeViewId")
..setMethodCallHandler(_onMethodCall);
}

Map<String, dynamic> _buildCreationParams() {
Expand Down Expand Up @@ -196,7 +201,20 @@ class _NativeTextInputState extends State<NativeTextInput> {
return null;
}

throw MissingPluginException("NativeTextInput._onMethodCall: No handler for ${call.method}");
throw MissingPluginException(
"NativeTextInput._onMethodCall: No handler for ${call.method}");
}

double _minHeight() {
return widget.minLines * 22.0 > 36.0 ? widget.minLines * 22.0 : 36.0;
}

double _maxHeight() {
return _isMultiline
? (22.0 * _currentLineIndex > _minHeight()
? 22.0 * _currentLineIndex
: _minHeight())
: 36.0;
}

// input control methods
Expand All @@ -215,7 +233,9 @@ class _NativeTextInputState extends State<NativeTextInput> {

void _inputValueChanged(String text, int lineIndex) {
if (text != null) {
if (_isMultiline && _currentLineIndex != lineIndex && lineIndex <= widget.maxLines)
if (_isMultiline &&
_currentLineIndex != lineIndex &&
lineIndex <= widget.maxLines)
setState(() {
_currentLineIndex = lineIndex;
});
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: flutter_native_text_input
description: Native text input for Flutter. Currently iOS-only with the use of UITextView.

version: 0.0.2
version: 0.1.0
homepage: https://github.com/henryleunghk/flutter-native-text-input

environment:
Expand Down

0 comments on commit 977e263

Please sign in to comment.