Flutter has multiple widgets that automatically resize the text to fit perfectly within its boundaries.
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,
)
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.
You can install the package from the command line using the below code with Flutter as shown.
$ flutter pub add auto_size_text
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';
Create a new Flutter project and then copy and paste the below code into example/lib/main.dart file:
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,
),
),
),
),
);
}
}