Flutter Tutorials

How to Dynamically Adjust Font Sizes in Flutter?

· · Updated April 12, 2024
Adjust Flutter Font Size on Different Devices

In this guide, we’ll explore three dynamic methods to adjust font sizes in Flutter, ensuring your app looks great on devices of all sizes.

Are you a Flutter developer looking to enhance the responsiveness and adaptability of your app’s UI? Font size adjustment is a crucial aspect of creating a visually appealing and user-friendly interface.

Table of Contents

Dynamically Adjust Font Sizes in Flutter

Why Dynamic Font Size Adjustment?

Before diving into the methods, let’s understand why dynamic font size adjustment is essential:

Device Diversity

With a plethora of devices available, ranging from smartphones to tablets, ensuring your app’s font sizes adapt to different screen sizes is crucial for a consistent user experience.

Accessibility

Dynamic font sizing helps accommodate users with varying visual impairments or preferences, ensuring readability for everyone.

Responsive Design

Embracing dynamic font sizes aligns with the principles of responsive design, making your app more versatile and user-friendly.

Without further ado, let’s explore the three methods to achieve dynamic font size adjustment in Flutter

1. Using MediaQuery -Adjust Font Sizes in Flutter

Flutter’s MediaQuery class provides information about the current app context, including the device’s size. We can utilize this information to dynamically adjust font sizes.

import 'package:flutter/material.dart';

class DynamicFontSizeApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    double screenWidth = MediaQuery.of(context).size.width;
    double screenHeight = MediaQuery.of(context).size.height;

    // Define font size based on screen width
    double fontSize = screenWidth * 0.05; // Adjust this factor as needed

    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Dynamic Font Size Example'),
        ),
        body: Center(
          child: Text(
            'Dynamic Font Size',
            style: TextStyle(fontSize: fontSize),
          ),
        ),
      ),
    );
  }
}

void main() {
  runApp(DynamicFontSizeApp());
}

Output:

Using MediaQuery -Adjust Font Sizes in Flutter

2. Utilizing LayoutBuilder – Adjust Font Sizes in Flutter

The LayoutBuilder widget provides information about the parent widget’s size constraints, allowing us to adjust font sizes dynamically.

import 'package:flutter/material.dart';

class DynamicFontSizeApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Dynamic Font Size Example'),
        ),
        body: Center(
          child: LayoutBuilder(
            builder: (context, constraints) {
              double fontSize = constraints.maxWidth * 0.05; // Adjust as needed
              return Text(
                'Dynamic Font Size',
                style: TextStyle(fontSize: fontSize),
              );
            },
          ),
        ),
      ),
    );
  }
}

void main() {
  runApp(DynamicFontSizeApp());
}

Output:

Utilizing LayoutBuilder - Adjust Font Sizes in Flutter

3. Implementing Responsive Text Widget

For a more encapsulated approach, we can create a custom widget that adjusts its font size based on the available space.

import 'package:flutter/material.dart';

class ResponsiveText extends StatelessWidget {
  final String text;
  final double scaleFactor;

  ResponsiveText({required this.text, this.scaleFactor = 0.05});

  @override
  Widget build(BuildContext context) {
    return LayoutBuilder(
      builder: (context, constraints) {
        double fontSize = constraints.maxWidth * scaleFactor;
        return Text(
          text,
          style: TextStyle(fontSize: fontSize),
        );
      },
    );
  }
}

class DynamicFontSizeApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Dynamic Font Size Example'),
        ),
        body: Center(
          child: ResponsiveText(text: 'Dynamic Font Size'),
        ),
      ),
    );
  }
}

void main() {
  runApp(DynamicFontSizeApp());
}

Output:

Output of Implementing Responsive Text Widget

Also Read:

Conclusion

Dynamic font size adjustment is crucial for creating adaptive and user-friendly Flutter apps. By utilizing methods like MediaQuery, LayoutBuilder, or creating custom widgets, you can ensure your app’s text remains readable and aesthetically pleasing across various devices.

Experiment with these approaches to find the perfect balance between responsiveness and design for your app.

You can follow the official: details of font sizes in flutter to set it up.

Ambika Dulal

About Ambika Dulal

Lead Mobile App Developer and Tech Consultant specializing in Flutter, Dart, and Firebase.