0

Here is a widget that creates a text input in a scrolling area. I want the text widget to grow from minHeight to maxHeight, but it defaults to maxHeight.

How can I construct this to grow from minHeight to maxHeight, even though the text input has no height?

Here is my current code:

return new ConstrainedBox(
    constraints: new BoxConstraints(
      minHeight: 25.0,
      maxHeight: 60.0,
    ),
    child: new ListView(
      scrollDirection: Axis.vertical,
      reverse: true,
      children: <Widget>[

        new Container(
        decoration: new BoxDecoration(
          color: Colors.grey,
        ),
        padding: const EdgeInsets.all(7.0),

        // here's the actual text box
        child: new TextField(
          keyboardType: TextInputType.multiline,
          maxLines: null, //grow automatically
          focusNode: mrFocus,
          controller: _textController,
          onSubmitted: currentIsComposing ? _handleSubmitted : null,
          decoration: new InputDecoration.collapsed(
            hintText: ChqStrings.of(context).sendAMessage(),
          ),
        ),
        // ends the actual text box
      ),
    ],
  ),
);
Deborah
  • 4,316
  • 8
  • 31
  • 45
  • Sounds like you are looking for https://stackoverflow.com/questions/51205333/flutter-textfield-that-auto-expands-when-text-is-entered-and-then-starts-scrolli/51205467 – Günter Zöchbauer Jan 15 '19 at 14:06

1 Answers1

2

WARNING : That is most of the time a bad idea to measure the size of a widget. There are many alternatives which you should consider.

Flutter widgets are supposed to know neither where they are rendered on screen nor have access to the size of their siblings/children. There are some exceptions. But remember this.


That's a one-liner : context.size. Where context is the context of the widget you want to measure. That function is available only outside of the build method.

You may want to use a GlobalKey to get the context of a child.

Since size property is only available after a build, you may also want to use a Scheduler to calculate after the build call. This can be achieved with SchedulerBinding.instance.addPostFrameCallback(callback).

Rémi Rousselet
  • 256,336
  • 79
  • 519
  • 432
  • "It's a common functionality Flutter does not yet provide in Text widgets". That's simply because this is not the role of `Text` widget. You can achieve the same result with composition. – Rémi Rousselet Feb 08 '18 at 19:00
  • A `ConstraintBox` > `SingleChildScrollView` > `TextInput` should do the trick. (will take maxLines height or maxHeight) And if you want the textinput to hit minHeight, then instead of calculating it's size you should write your own TextInput (by looking at the source code) – Rémi Rousselet Feb 08 '18 at 19:11
  • In fact no need to write your own widget. maxlines set to null works – Rémi Rousselet Feb 08 '18 at 21:56
  • ConstraintBox > SingleChildScrollView > TextInput with "maxLines" to null will start at minHeight and grow until maxHeight – Rémi Rousselet Feb 08 '18 at 21:56
  • Your suggestion works, I had a container in between that was causing trouble. I have edited my question, if you will edit your answer I will mark it very gratefully with green check and many thanks! :) – Deborah Feb 12 '18 at 18:53