14

I'm implementing a TextFormField with the maxLines attribute set to 3. How can I make the TextFormField scroll down once the user starts with his fourth line? At the moment the cursor is just not visible anymore until the user scrolls down by hand. Is there a way to do this automatically?

This behaviour is actually featured in the flutter_gallery app in the 'Text fields' example. Just type a long text to the 'Live story' input until it reaches the fourth line.
The important parts of my code actually look like this:

import 'package:flutter/material.dart';

class TextFormFieldDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      body: new Form(
        child: new TextFormField(
          maxLines: 3,
        ),
      ),
    );
  }
}

So far I have found no workaround for this issue.
This issue affects both iOS and android.

Rainer Wittmann
  • 7,528
  • 4
  • 20
  • 34
  • PS: This also affects single line TextFields. One would expect the text to scroll to the left once the end of the line is reached, but it doesn't. – Rainer Wittmann Apr 11 '17 at 14:06

8 Answers8

18

Our team accomplished this by nesting some existing widgets:

// create the illusion of a beautifully scrolling text box
return new Container(
    color: Colors.gray,
  padding: new EdgeInsets.all(7.0),

  child: new ConstrainedBox(
    constraints: new BoxConstraints(
      minWidth: _contextWidth(),
      maxWidth: _contextWidth(),
      minHeight: AppMeasurements.isLandscapePhone(context) ? 25.0 : 25.0,
      maxHeight: 55.0,
    ),

    child: new SingleChildScrollView(
      scrollDirection: Axis.vertical,
      reverse: true,

        // 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: ''Please enter a lot of text',
          ),
        ),
        // ends the actual text box

      ),
    ),
  );
}

We had help from Darky to get widget ordering and the correct widgets to make it work.

Deborah
  • 4,316
  • 8
  • 31
  • 45
  • 2
    maxLInes=null is awesome – Pushan Gupta Aug 13 '18 at 15:11
  • 1
    beautiful hack <3 – OMi Shah Dec 15 '19 at 18:36
  • 1
    Did anyone got this running for the CupertinoTextBox? Though MaxLines=null is also supposed to create a growing field, no scroll bar shows up – w461 Aug 31 '20 at 08:40
  • @w461: using a CupertinoTextField will add scrollbars automatically but maxLines can't be null. Scrollbars are added the moment the contents of the text field exceeds maxLines. E.g. if maxLines is 3 and the text occupies 4 lines, a scrollbar is added. – JohnV Mar 20 '22 at 00:12
7

This appears to be a missing feature in the Flutter Framework, I've filed a bug to get it resolved: https://github.com/flutter/flutter/issues/9365

Eric Seidel
  • 3,282
  • 1
  • 15
  • 22
5

You can use BoxConstraints and set maxHeight of your TextField

Container(
  constraints: BoxConstraints(maxHeight: 100),
  child: SingleChildScrollView(
    child: TextField(
      maxLines: null,
    ),
  ),
);
Abdurahman Popal
  • 2,859
  • 24
  • 18
5

With the latest flutter 1.20.4, this text field will scroll when it rich the max hight.

       Container(
          constraints: BoxConstraints(maxHeight: 200),
             child: TextField(
                maxLines: null,
                 .......
                )
            )

If you are using the Textfield inside Raw or column wrap it in Expanded widget

Sinshaw Demisse
  • 232
  • 1
  • 3
  • 12
2

I made it work like this. Hope it helps!

return new Card(
                shape: RoundedRectangleBorder(
                 side: BorderSide(
                   color: Colors.deepPurpleAccent,
                   width: 1
                 ),
                  borderRadius: BorderRadius.circular(3)
                ),
                child: Container(
                  height: 50,
                  child: SingleChildScrollView(
                    child: TextField(
                      maxLines: null,
                    ),
                  ),
                ),
              );
user187293
  • 71
  • 1
  • 5
2

I solve this with ScrollController.

ScrollController textFieldScrollController = ScrollController();

TextField(
  scrollController: textFieldScrollController,
  keyboardType: TextInputType.multiline,
  minLines: null,
  maxLines: null,
  onChanged: (value) {
    textFieldScrollController.jumpTo(textFieldScrollController.position.maxScrollExtent);
  },
),
1

Just Use TextFormField() widget & set minLines= and maxLines= . The missing thing is here Scroll indicator.

 TextFormField(
                    minLines: 3,
                    maxLines: 3,
                    key: _messageValueKey,
                    decoration: _inputDecoration,
                  ),

Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56
-3

Just add the 'reverse: true' to your Scrollable Widget, like so:

    Widget build(BuildContext context) {
      return Container(
        width: MediaQuery.of(context).size.width,
        height: 130.0,
        decoration: BoxDecoration(
          border: Border.all(color: Colors.purpleAccent),
        ),
        child: SingleChildScrollView(
          scrollDirection: Axis.horizontal,
          reverse: true,
          child: Text(
            '$message',
            maxLines: 2,
            style: TextStyle(fontSize: 30),
          ),
        ),
      );
    }
  }
Chukwuemeka Maduekwe
  • 6,687
  • 5
  • 44
  • 67