Skip to main content

Flutter Set Max Lines on Text

To set max lines validation in flutter on text we would use maxLines property of Text widget. maxLines allows us to define maximum number of lines Text widget will span. It's basically used when we have a large text defined but we only want to display a fixed number of lines like 2 lines or 3 lines or 4 lines. maxLines is used with TextOverflow to show how text overflows like ellipsis. If you want to read more about TextOverflow then I have already make a full tutorial on this topic. You can read it from HERE.

Flutter Set Max Lines on Text:

1. Defining Text widget with maxLines. I am showing only 2 lines of text.
Text(
        'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industrys standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.',
        style: TextStyle(fontSize: 28),
        maxLines: 2,
        overflow: TextOverflow.ellipsis,
      )
2. Complete source code for main.dart file:
import 'package:flutter/material.dart';

void main() => runApp(const App());

class App extends StatelessWidget {
  const App({super.key});

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: Scaffold(
          body: Center(
              child: Text(
        'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industrys standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.',
        style: TextStyle(fontSize: 28),
        maxLines: 2,
        overflow: TextOverflow.ellipsis,
      ))),
    );
  }
}
Screenshot of App:
Flutter Set Max Lines on Text

Comments