7.4 C
New York
Monday, December 11, 2023

Displaying Subscripts and Superscripts in Flutter

There might be cases where you want to display some subscripts and superscripts in a Flutter application such as mathematical formulas, chemical equations, or physics curves.

To render subscripts, you can set the style of Text or TextSpan widgets like this:Advertisements

TextStyle(
  fontFeatures: [FontFeature.subscripts()]),
  /* other styles go here */
),

To render superscripts, you can set the style of Text or TextSpan widgets like this:

TextStyle(
  fontFeatures: [FontFeature.superscripts()]),
  /* other styles go here */
),

For more clarity, see the examples below.

Example 1: Using multiple Text widgets

Screenshot:

The code:

// Don't forget to import dart:ui into your code
Wrap(
              direction: Axis.horizontal,
              children: [
                Text('x'),
                Text(
                  '2',
                  style: TextStyle(fontFeatures: [FontFeature.superscripts()]),
                ),
                Text(' + '),
                Text('y'),
                Text(
                  '5',
                  style: TextStyle(fontFeatures: [FontFeature.superscripts()]),
                ),
                Text(' + '),
                Text('z'),
                Text(
                  'n',
                  style: TextStyle(fontFeatures: [FontFeature.superscripts()]),
                ),
              ],
            ),

Example 2: Using RichText + TextSpan

Screenshot:

The full source code:

// main.dart
import 'package:flutter/material.dart';
import 'dart:ui';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        // Remove the debug banner
        debugShowCheckedModeBanner: false,
        title: 'Kindacode.com',
        theme: ThemeData(
          primarySwatch: Colors.amber,
        ),
        home: HomePage());
  }
}

class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text('Kindacode.com'),
        ),
        body: Padding(
          padding: EdgeInsets.all(30),
          child: RichText(
            text: TextSpan(children: [
              TextSpan(
                  text: 'Acid Sulfuric: ',
                  style: TextStyle(color: Colors.purple, fontSize: 30)),
              TextSpan(
                  text: 'H',
                  style: TextStyle(color: Colors.purple, fontSize: 30)),
              TextSpan(
                  text: '2',
                  style: TextStyle(
                      color: Colors.purple,
                      fontSize: 30,
                      fontFeatures: [FontFeature.subscripts()])),
              TextSpan(
                text: 'S',
                style: TextStyle(color: Colors.purple, fontSize: 30),
              ),
              TextSpan(
                text: 'O',
                style: TextStyle(color: Colors.purple, fontSize: 30),
              ),
              TextSpan(
                text: '4',
                style: TextStyle(
                    color: Colors.purple,
                    fontSize: 30,
                    fontFeatures: [FontFeature.subscripts()]),
              ),
            ]),
          ),
        ));
  }
}

WhatÂ’s Next?

If you want to render Math symbols or dynamic content that loaded from APIs or databases:

Advertisements

Continue learning more new and interesting stuff about Flutter by taking a look at the following articles:

You can also check out our Flutter category page, or Dart category page for the latest tutorials and examples.

Advertisements

Related Articles

Latest Articles