Skip to content

Mirzaazmath/font_scaler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

FontScaler – Dynamic Text Scaling for Flutter

FontScaler is a lightweight Flutter package that allows you to dynamically adjust the text scale across your entire app. It supports predefined scale levels (like small, medium, large) and a custom mode for user-defined scaling. Optional persistent storage using SharedPreferences lets you retain the user’s preference across sessions.

Features

  • ✅ Supports Android,IOS,Web
  • ✅ Predefined scale levels: micro, small, medium, default, large, xl, etc.
  • ✅ Custom font scaling (e.g., customValue: 2.25)
  • ✅ Persist user settings using SharedPreferences
  • ✅ MediaQuery-aware text scaling
  • ✅ Easy integration with global access using FontScalerProvider.of(context)

Result

Output 1 Output 2
Output 1 Output 2

Getting started

In the pubspec.yaml of your flutter project, add the following dependency:

dependencies:
  ...
  font_scaler: ^1.0.3

Import it:

import 'package:font_scaler/font_scaler.dart';

Usage

1.Initialize in main.dart

void main()  {
  // Need to Wrap MyApp with FontScaler
  runApp(FontScaler(
    child: MyApp(),
  ));
}
  1. Pass Builder in Material App
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      // Need to Add FontScalerProvider.of(context).builder to reflect the changes
      builder: FontScalerProvider.of(context).builder,
      home: HomeScreen(),
    );
  }
}
  1. Update the text scale from anywhere
// default Option
FontScalerProvider.of(context).updateFontScale(FontScale.large);
// Custom option with customValue
FontScalerProvider.of(context).updateFontScale(FontScale.custom, customValue: 2.4);
  1. Here is the list of enum you can use
// Default  Enum  

enum FontScale {
  ultraMicro,
  micro,
  extraSmall,
  small,
  medium,
  fDefault,
  large,
  extraLarge,
  doubleXL,
  ultraXL,
  custom,
}
// Just Pass like this
// EX:
FontScalerProvider.of(context).updateFontScale(FontScale.medium);
  1. Get the current Selected Font Scale type
// This will return the currentFontScale 
FontScalerProvider.of(context).currentFontScale;

Example Use Case 1

Let users control font sizes

ElevatedButton(
  onPressed: () {
    FontScalerProvider.of(context)
        .updateFontScale(FontScale.custom, customValue: 1.25);
  },
  child: Text("Increase Font Size"),
);

Example Use Case 2

Let users control font sizes

        Slider(
          min: 1,
          max: 5,
          value: slideValue,
          onChanged: (double value) {
            FontScalerProvider.of(
              context,
            ).updateFontScale(FontScale.custom, customValue: value);
            setState(() {
              slideValue = value;
            });
          },
        ),

Usage With SharedPreferences

Using SharedPreferences user can save the selected font Scale locally whenever app restarts it will take the value from local storage

In the pubspec.yaml of your flutter project, add the following dependency:

dependencies:
  ...
  font_scaler: ^1.0.3
  shared_preferences: any

Import it:

import 'package:font_scaler/font_scaler.dart';
import 'package:shared_preferences/shared_preferences.dart';

1.Initialize in main.dart

void main()async{ // Make this Async
  // Add this Line
  WidgetsFlutterBinding.ensureInitialized();
  // Create SharedPreferences Instance
  final SharedPreferences prefs = await SharedPreferences.getInstance();
  // Need to Wrap MyApp with FontScaler
  runApp(FontScaler(
    // savePermanent will save the selected fontScale locally
    // savePermanent work with  SharedPreferences Instance
    // So Whenever the app reopens the font Scale will be same as user selected last time
    savePermanent: true,
      // Need to pass SharedPreferences Instance that will use in storing the data locally
      prefs: prefs,
      child: MyApp()));
}
  1. Pass Builder in Material App
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      // Need to Add FontScalerProvider.of(context).builder to reflect the changes
      builder: FontScalerProvider.of(context).builder,
      home: HomeScreen(),
    );
  }
}
  1. Update the text scale from anywhere
// default Option
FontScalerProvider.of(context).updateFontScale(FontScale.large);
// Custom option with customValue
FontScalerProvider.of(context).updateFontScale(FontScale.custom, customValue: 1.4);
  1. You can clear the saved font Scale and set it to default
 //  This Will Clear the change and remove from local storage and set back to default fontScale
  FontScalerProvider.of(context).clear();

Support

Buy Me A Coffee

Star project on Github

Author

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors