Scanbot SDK has been acquired by Apryse! Learn more

Learn more
Skip to content

Dynamically adjust font size in Flutter

Ivan July 5, 2023 2 mins read
Flutter vs Ionic: How to choose the best framework for your mobile app

In Flutter, fixed font sizes can compromise display quality across varied screen sizes and text lengths.

Here are three effective ways to dynamically adjust font size:

1. AutoSizeText Widget

AutoSizeText widget allows text to resize within available space, adjusting to different screen sizes. It offers the option to set minimum and maximum font sizes and handle text overflow.

Usage example:

AutoSizeText(
  'Sample text',
  minFontSize: 12,
  maxFontSize: 20,
  overflow: TextOverflow.ellipsis,
)

2. FittedBox Widget

FittedBox scales text to fit container boundaries while preserving the aspect ratio. This widget is especially useful for responsive designs.

Usage example:

FittedBox(
  fit: BoxFit.scaleDown,
  child: Text(
    'Sample text',
    style: TextStyle(fontSize: 16),
  ),
)

3. LayoutBuilder Widget

LayoutBuilder allows widgets to adapt to the constraints of parent widgets, modifying font size to match available space. When no parent widget is defined, the device screen serves as the parent, adjusting font size to screen size.

Mobile applications typically have screen sizes 480 pixels wide or less, while iPads or tablets range from 481 to 960 pixels. Web or desktop platforms generally exceed 960 pixels in width.

Usage example:

LayoutBuilder(
  builder: (context, constraints) {
    final width = constraints.maxWidth;
    var fontSize = 16.0;
    if (width <= 480) {
      fontSize = 16.0;
    } else if (width > 480 && width <= 960) {
      fontSize = 22.0;
    } else {
      fontSize = 28.0;
    }
    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Text(
            'Sample responsive text',
            textAlign: TextAlign.center,
            style: TextStyle(fontSize: fontSize),
          ),
        ],
      ),
    );
  },
)

In this example, font sizes are assigned based on screen width, making the text display responsive to the device’s screen width.

While implementing LayoutBuilder may require more initial consideration, its capacity to create fully responsive designs is worthwhile, ensuring a consistent user experience across devices.

Related blog posts

Experience our demo apps

Barcode Icon Art

Barcode Scanner SDK

Scan 1D and 2D barcodes reliably in under 0.04s. Try features like Batch Scanning, Scan & Count, and our AR Overlays.

Launch Web Demo

Scan the code to launch the web demo on your phone.

Web QR Code

Also available to download from:

Document Icon Art

Document Scanner SDK

Scan documents quickly and accurately with our free demo app. Create crisp digital scans in seconds.

Launch Web Demo

Scan the code to launch the web demo on your phone.

Black and white QR code. Scan this code for quick access to information.

Also available to download from:

Data_capture Icon Art

Data Capture Modules

Try fast, accurate data capture with our demo app. Extract data from any document instantly – 100% secure.

Launch Web Demo

Scan the code to launch the web demo on your phone.

Black and white QR code. Scan this quick response code with your smartphone.

Also available to download from: