The Wayback Machine - https://web.archive.org/web/20240805164123/https://www.geeksforgeeks.org/flutter-auto-size-text/
Open In App

Flutter – Auto size text

Last Updated : 11 Apr, 2022
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report

An adaptive UI is an integral part of any application. As applications nowadays have to function on a wide range of devices ranging from tablets, PCs, and mobile with varying screen sizes. This is where the application needs to adjust itself according to the size of the screen irrespective of the size of the content of the application.

Flutter provides with an auto_size_text dependency that adapts the screen according to the screen size and manipulates itself to provide with adaptive user experience. The above dependency adjusts the size of the text in the application. In this article, we will explore the same dependency in detail by building a simple application.

To build the application follow the below steps:

  • Add the dependency to the pubspec.yaml file.
  • Import the dependency into the main.dart file.
  • Create a StatelessWidget to give the app a structure
  • Add a sample text to adapt

Now, let’s discuss the above steps in detail.

Adding the Dependency:

To add the dependency to the pubspec.yaml file,  add the auto_size_text to the flutter dependency as shown below:

dependency

Importing the Dependency:

The dependency can be imported into the main.dart file as follows:

import 'package:auto_size_text/auto_size_text.dart';

Creating the App structure:

Use the StatelessWidget to give a simple structure to the app as shown below:

Dart




class App extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar:AppBar(title: Text('GeeksForGeeks'),
          backgroundColor: Colors.green,),
        ),
      ),
    );
  }
}


Adding a Sample text:

A text inside the body of the application can be added inside a sizedBox as shown below:

Dart




child: SizedBox(
  width: 200.0,
  height: 140.0,
  child: AutoSizeText(
    'Hello Geeks!. We will break this line into 3 lines !!',
    style: TextStyle(fontSize: 30.0),
    maxLines: 3,
  ),
),


 
 

Complete Source Code:

 

Dart




import 'package:auto_size_text/auto_size_text.dart';
import 'package:flutter/material.dart';
 
void main() {
  runApp(App());
}
 
class App extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar:AppBar(title: Text('GeeksForGeeks'),
          backgroundColor: Colors.green,),
        body: Center(
          child: SizedBox(
            width: 200.0,
            height: 140.0,
            child: AutoSizeText(
              'Hello Geeks!. We will break this line into 3 lines !!',
              style: TextStyle(fontSize: 30.0),
              maxLines: 3,
            ),
          ),
        ),
      ),
    );
  }
}


Output:

3 lines adjust

If you wish to make the text fit in 2 lines instead of 3 line, make the below changes to the code:

Dart




import 'package:auto_size_text/auto_size_text.dart';
import 'package:flutter/material.dart';
 
void main() {
  runApp(App());
}
 
class App extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar:AppBar(title: Text('GeeksForGeeks'),
          backgroundColor: Colors.green,),
        body: Center(
          child: SizedBox(
            width: 200.0,
            height: 140.0,
            child: AutoSizeText(
              'Hello Geeks! We will break this line into 3 lines !!',
              style: TextStyle(fontSize: 30.0),
              maxLines: 2,
            ),
          ),
        ),
      ),
    );
  }
}


Output:

2 lines adjust



Similar Reads

Flutter Quill - Rich Text Editor in Flutter Application
This post will explain how to integrate a rich text editor into your Flutter application so that it may enable rich text editing. There are many more things we can do with Flutter Quill, but they can get a little involved. The example below is a pretty basic demonstration of what we mean. Let's first discuss what flutter quill is and why we need it
5 min read
Is Flutter Worth Learning? Top 7 Reasons to Learn Flutter
In today's era, the usage of smartphones has increased exponentially and so has the use of mobile applications and websites. Meanwhile, considering future career prospects, learning web development and mobile app development is strongly recommended for all individuals. And when we come to mobile app development, there are two most-popular open-sour
5 min read
Flutter - Sharing Data Among Flutter Pages
In this article, we are going to find the solution for the problem statement "Import/send data from one page to another in flutter". Before going into the topic, there are some pre-requisites. Pre-requisites:Basics of dart programming language.Setting up Flutter in VS code or Setting up Flutter in Android Studio.Creating first flutter app | Hello W
4 min read
Flutter - Animated AlertDialog in Flutter
Animating an AlertDialog in Flutter involves using the Flutter animations framework to create custom animations for showing and hiding the dialogue. In this article, we are going to add an animation to an AlertDialog. A sample video is given below to get an idea about what we are going to do in this article. [video mp4="https://media.geeksforgeeks.
4 min read
Flutter - Different Types of Loading using Flutter EasyLoading
Flutter applications involve incorporating loading indicators to provide users with feedback during background operations. Flutter has a package for simplifying the process named flutter_easyloading package. This package easiest way to integrate loading indicators with various styles. In this article, we will implement different types of loading in
5 min read
Animated Text in Flutter
Animations make the UI more interactive and enhance the user experience. There is no limitation of creativity when it comes to animations or making the application more interactive. In Flutter, we got an easy way to display Animated text. In this article, we will see how we can animate texts using animated_text_kit. Types of Text Animations: Follow
3 min read
Flutter - Skeleton Text
In Flutter, the skeleton_text library is used to easily implement skeleton text loading animation. Its main application in a flutter app is to assure its users that the servers are working but running slow, but the content will eventually load. It also enhances the UI if the user connection is slow too. in this article, we will be looking into the
3 min read
Flutter - Scrollable Text
In this article, we are going to make a Flutter application in which we will add a Text Widget that can be scrolled horizontally or vertically. These can have a wide range of applications depending upon the needs of the users. Here we will be implementing the simplest form of scrollable text. We can make the text scrollable in flutter using these 2
3 min read
Flutter - Change Text When Button Pressed
In this article, you will see how you can change the text in Flutter on a single screen after clicking the button. Let's start so you first start your vs code or android studio and you just delete the whole code and paste the code which we have given in below and run it then you can get the solution. A sample video is given below to get an idea abo
4 min read
Flutter - Load Text Assets
In Flutter, you can easily load text assets such as JSON files, configuration files, or plain text files into your app's memory at runtime. This allows you to access the data contained in these files and use it to populate your app's UI or perform other operations. To load text assets in Flutter, you first need to add the files to your project's as
3 min read
Article Tags :
three90RightbarBannerImg