Flutter TextField how to change the maximum and minimum lines

Changing the maximum and minimum lines in a TextField:

We can change the maximum and minimum lines in a TextField using two different props. In this post, we will learn how to do that with an example.

Properties to change the max and min lines in a TextField:

TextField class provides the following two properties that can be used to change the maximum and minimum lines in a TextField:

  • int minLines
  • int maxLines

These properties are used to define the minimum number of lines and maximum number of lines in a TextField. Both of these properties takes one integer value and sets that.

Example:

Let’s take a look at the below example:

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'Flutter Demo',
        theme: ThemeData(
          primarySwatch: Colors.blue,
        ),
        home: Scaffold(
            appBar: AppBar(
              title: Text('Example'),
            ),
            body: Container(
                margin: EdgeInsets.all(30),
                child: TextField(
                  minLines: 2,
                  maxLines: 4,
                  decoration: InputDecoration(
                      border: OutlineInputBorder(),
                      labelText: "Enter your Text"),
                ))));
  }
}

Here, we defined the minimum number or lines or minLines as 2 and maximum number of lines or maxLines as 4. If you run this app, it will create one screen with one TextField as like below:

Flutter minLines example

So, you can see here that the height of the TextField is bigger and it is showing two lines as this is the minimum lines value.

Now, let’s see how it works if I write more than two lines:

python textfield maxlines example

Here, I entered five lines and as you can see, the height of the textfield grows as I starts writing. It stops once four lines are reached and the content become scrollable.

Flutter makes it easy to build expandable TextField or auto grow TextField.

You might also like: