Flutter Auto Size Text

Flutter has multiple widgets that automatically resize the text to fit perfectly within its boundaries.

In flutter,  AutoSizeText behaves exactly like a Text but the difference is that it resizes text to fit within its boundries.

Code item sample contentAutoSizeText(
'The text to display',
style: TextStyle(fontSize: 20),
maxLines: 2,
)

Follow the below steps to create auto test size in Flutter

 

Add the dependency package:

adding the dependency package to pubspec.yaml file. use the below code to add the dependency package. After adding the dependency package run the get package method to import all the required files to the app. 

Install the package:

You can install the package from the command line using the below code with Flutter as shown.

$ flutter pub add auto_size_text

Importing the Package:

After Adding the dependency package to the pubspec.yaml file , you can now import the package into the dart code by using the below code. without adding the dependency package to the pubspec.yaml file if you import it will show package not found an error.

import 'package:auto_size_text/auto_size_text.dart';

Complete example code for Flutter auto size text:

Create a new Flutter project and then copy and paste the below code into example/lib/main.dart file:

example/main.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(
body: Center(
child: SizedBox(
width: 200,
height: 140,
child: AutoSizeText(
'This string will be automatically resized to fit in two lines.',
style: TextStyle(fontSize: 30),
maxLines: 2,
),
),
),
),
);
}
}

Congratulations You Have Learned Flutter auto-size text….!!!

If It Is Helpful Share It With Your Friends….!!!

Leave a Reply

Categories