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.
Steps to implement auto_size_text in Flutter
Step 1 : Adding the Dependency
To add the dependency to the pubspec.yaml file, add the auto_size_text to the flutter dependency as shown below:
dependencies:
auto_size_text: ^3.0.0
Now run the below command in terminal.
flutter pub get
Step 2 : Importing the Dependency
The dependency can be imported into the main.dart file as follows:
import 'package:auto_size_text/auto_size_text.dart';
Step 3 : 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,), ), ), ); } }
Step 4 : 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, ), ),
To know more about SizedBox in flutter refer this article: Flutter – SizedBox Widget
Complete Source Code(main.dart):
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:
If you wish to make the text fit in 2 lines instead of 3 line, make the below changes to the code.
main.dart:
Dart import 'package:auto_size_text/auto_size_text.dart'; import 'package:flutter/material.dart'; // Main function to run the app void main() { runApp(MyApp()); } // MyApp is a stateless widget class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( debugShowCheckedModeBanner: false, home: Scaffold( appBar: AppBar( title: Text('GeeksForGeeks'), backgroundColor: Colors.green, foregroundColor: Colors.white, ), 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:
Similar Reads
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 a
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 User Interface if the user connect
3 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, yo
3 min read
Flutter - Create 3D Text Flutter, developed by Google, has gained immense popularity among developers due to its flexibility and ease of use. With Flutter, we can create stunning user interfaces, including 3D elements. This tutorial will explore how to create 3D text in Flutter step by step using the text_3d library. A samp
5 min read
Flutter - Custom Widgets We create Custom Widgets when we want a custom look and feel to our app, and we know that there will be a repetition of a particular widget. We can create the custom widget in a new dart file with all the codes and defining the parameters that we need in the constructor. For more on how to split wid
8 min read