### Import AutoSizeText Package Source: https://context7.com/simc/auto_size_text/llms.txt Import the AutoSizeText package into your Dart file to use its widgets. ```dart import 'package:auto_size_text/auto_size_text.dart'; ``` -------------------------------- ### MIT License Source: https://github.com/simc/auto_size_text/blob/master/README.md The MIT License grants permissions for using, copying, modifying, merging, publishing, distributing, sublicensing, and/or selling copies of the software. ```text Copyright (c) 2018 Simon Leier Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ``` -------------------------------- ### AutoSizeGroupBuilder for Safe Group Provisioning Source: https://context7.com/simc/auto_size_text/llms.txt AutoSizeGroupBuilder safely creates and provides an AutoSizeGroup instance to child widgets via a builder callback. Use this widget instead of manually managing a StatefulWidget to hold the group. ```dart AutoSizeGroupBuilder( builder: (context, group) { return Column( children: [ SizedBox( width: 300, height: 50, child: AutoSizeText( 'Label One', style: TextStyle(fontSize: 60), minFontSize: 1, maxLines: 1, group: group, ), ), SizedBox( width: 150, // narrower — this drives the group's font size down height: 50, child: AutoSizeText( 'Label Two', style: TextStyle(fontSize: 60), minFontSize: 1, maxLines: 1, group: group, ), ), ], ); // Both texts adopt the font size needed for the 150px-wide "Label Two". }, ) ``` -------------------------------- ### Basic AutoSizeText Usage Source: https://context7.com/simc/auto_size_text/llms.txt Use AutoSizeText as a direct replacement for the Text widget. It requires a parent widget that provides bounded width and height constraints. The font size will shrink from the specified size until the text fits within the bounds, respecting the maxLines. ```dart import 'package:auto_size_text/auto_size_text.dart'; import 'package:flutter/material.dart'; class BasicExample extends StatelessWidget { @override Widget build(BuildContext context) { return SizedBox( width: 200, height: 140, child: AutoSizeText( 'This string will be automatically resized to fit in two lines.', style: TextStyle(fontSize: 30), // starting font size maxLines: 2, ), ); // Result: font size is reduced from 30 until the text fits within // 200×140 px at most 2 lines. } } ``` -------------------------------- ### Use Rich Text with AutoSizeText.rich Source: https://github.com/simc/auto_size_text/blob/master/README.md Utilize `AutoSizeText.rich` for rich text content, similar to `Text.rich`. The `fontSize` in the `style` parameter serves as the reference for font size calculation. The text will be at least a fraction of its original size based on `minFontSize`. ```dart AutoSizeText.rich( TextSpan(text: 'A really long String'), style: TextStyle(fontSize: 20), minFontSize: 5, ) ``` -------------------------------- ### AutoSizeText.rich for Rich Text Support Source: https://context7.com/simc/auto_size_text/llms.txt Use AutoSizeText.rich() for auto-sizing styled spans, links, or mixed inline content. The fontSize in the top-level style parameter acts as the scaling reference, and all TextSpan children scale proportionally. ```dart SizedBox( width: 300, height: 80, child: AutoSizeText.rich( TextSpan( text: 'Hello ', style: TextStyle(fontSize: 30, color: Colors.black), children: [ TextSpan( text: 'Flutter', style: TextStyle( fontWeight: FontWeight.bold, color: Colors.blue, ), ), TextSpan(text: ' World!'), ], ), minFontSize: 10, maxLines: 1, overflow: TextOverflow.ellipsis, ), ) // The entire span tree scales together. "Flutter" stays bold/blue at the // scaled size. At minFontSize=10, the text can shrink to 1/3 of 30pt. ``` -------------------------------- ### AutoSizeText with Step Granularity Source: https://github.com/simc/auto_size_text/blob/master/README.md Define the step size for font scaling. stepGranularity determines how much the font size decreases in each step. A value of 1 or greater is recommended for performance. ```dart AutoSizeText( 'A really long String', style: TextStyle(fontSize: 40), minFontSize: 10, stepGranularity: 10, maxLines: 4, overflow: TextOverflow.ellipsis, ) ``` -------------------------------- ### Restrict AutoSizeText to Preset Font Sizes Source: https://context7.com/simc/auto_size_text/llms.txt Use `presetFontSizes` to provide a descending list of allowed font sizes. When this property is set, `minFontSize`, `maxFontSize`, and `stepGranularity` are ignored. The widget tries sizes in the order provided until one fits. ```dart SizedBox( width: 300, height: 100, child: AutoSizeText( 'Hello World', presetFontSizes: [40, 20, 14], // tried in order: 40 → 20 → 14 maxLines: 1, ), ) // If width=500 → uses 40pt. If width=300 → uses 20pt. If width=100 → uses 14pt. // Example matching the test cases: SizedBox( width: 20, // very narrow height: 100, child: AutoSizeText( 'XXXXX', presetFontSizes: [100, 50, 5], ), ) // Result: font size resolves to 5 (the only size that fits in 20px width). ``` -------------------------------- ### AutoSizeText with MaxLines Source: https://github.com/simc/auto_size_text/blob/master/README.md The maxLines parameter functions identically to the standard Text widget. If not specified, AutoSizeText adjusts text to available width and height. ```dart AutoSizeText( 'A really long String', style: TextStyle(fontSize: 30), maxLines: 2, ) ``` -------------------------------- ### Set MinFontSize for AutoSizeText.rich Source: https://github.com/simc/auto_size_text/blob/master/README.md For AutoSizeText.rich, if text does not resize as expected, check the minFontSize. Setting it to 0 or providing an explicit style can resolve issues. Consider lowering stepGranularity if minFontSize is set to 0. ```dart AutoSizeText.rich( TextSpan( text: 'Text that will not be resized correctly in some cases', style: TextStyle(fontSize: 200), ), ) ``` ```dart AutoSizeText.rich( TextSpan( text: 'Text that will be resized correctly', style: TextStyle(fontSize: 200), ), minFontSize: 0, stepGranularity: 0.1, ) ``` -------------------------------- ### Provide Bounded Constraints for AutoSizeText Source: https://context7.com/simc/auto_size_text/llms.txt AutoSizeText requires bounded width and height. Wrap it with Expanded, SizedBox, or any constraint-providing widget when used inside Row, Column, or other unbounded containers to prevent overflow. ```dart // WRONG — Row does not constrain its children, text will overflow Row( children: [ AutoSizeText('Some long text', maxLines: 1), ], ) ``` ```dart // CORRECT — Expanded provides the missing constraint Row( children: [ Expanded( child: AutoSizeText( 'Some long text', maxLines: 1, style: TextStyle(fontSize: 20), ), ), ], ) ``` ```dart // CORRECT — SizedBox provides explicit bounds SizedBox( width: 200, height: 50, child: AutoSizeText( 'Some long text', maxLines: 1, style: TextStyle(fontSize: 20), ), ) ``` -------------------------------- ### Provide Fallback Widget for AutoSizeText Overflow Source: https://context7.com/simc/auto_size_text/llms.txt Use `overflowReplacement` to display a fallback widget when the text cannot fit within the bounds, even at `minFontSize`. This cannot be used in conjunction with the `overflow` property. ```dart SizedBox( width: 100, height: 20, child: AutoSizeText( 'This text is way too long to ever fit here', minFontSize: 20, // high minimum so text will overflow overflowReplacement: Text( 'Text too long', style: TextStyle(fontSize: 12, color: Colors.red), ), ), ) // Result: if the text cannot fit at ≥20pt, displays "Text too long" in red instead. ``` -------------------------------- ### Add auto_size_text to pubspec.yaml Source: https://context7.com/simc/auto_size_text/llms.txt Add the auto_size_text package to your pubspec.yaml file to include it in your Flutter project. ```yaml dependencies: auto_size_text: ^3.0.0 ``` -------------------------------- ### AutoSizeGroup for Synchronized Font Sizes Source: https://context7.com/simc/auto_size_text/llms.txt AutoSizeGroup synchronizes the font size of multiple AutoSizeText instances to render at the smallest size required by any member of the group. Instantiate AutoSizeGroup once within a StatefulWidget's state and pass it to all widgets that should share sizing. ```dart class PricingRow extends StatefulWidget { @override _PricingRowState createState() => _PricingRowState(); } class _PricingRowState extends State { // Instantiate once inside StatefulWidget state — never inside build() final _group = AutoSizeGroup(); @override Widget build(BuildContext context) { return Row( children: [ Expanded( child: AutoSizeText( 'Short', group: _group, maxLines: 1, style: TextStyle(fontSize: 30), ), ), Expanded( child: AutoSizeText( 'A much longer label that forces downsizing', group: _group, maxLines: 1, style: TextStyle(fontSize: 30), ), ), ], ); // Both texts render at the font size required by the longer label, // keeping the UI visually consistent. } } ``` -------------------------------- ### Provide Overflow Replacement with AutoSizeText Source: https://github.com/simc/auto_size_text/blob/master/README.md Use `overflowReplacement` to display a fallback widget when the text overflows its bounds. This is useful to prevent text from becoming unreadably small. ```dart AutoSizeText( 'A String tool long to display without extreme scaling or overflow.', maxLines: 1, overflowReplacement: Text('Sorry String too long'), ) ``` -------------------------------- ### AutoSizeText with Min and Max Font Size Source: https://github.com/simc/auto_size_text/blob/master/README.md Control the range of font sizes AutoSizeText can use. minFontSize sets the smallest allowed size, defaulting to 12. maxFontSize caps the largest size, useful when inheriting font sizes. ```dart AutoSizeText( 'A really long String', style: TextStyle(fontSize: 30), minFontSize: 18, maxLines: 4, overflow: TextOverflow.ellipsis, ) ``` -------------------------------- ### Control AutoSizeText Resize Step Size Source: https://context7.com/simc/auto_size_text/llms.txt The `stepGranularity` property controls the font size decrease step during resizing. Larger values improve performance but offer less precise fitting. Both `minFontSize` and `maxFontSize` must be multiples of `stepGranularity`. Must be ≥ 0.1. ```dart SizedBox( width: 200, height: 80, child: AutoSizeText( 'A really long string', style: TextStyle(fontSize: 40), minFontSize: 10, stepGranularity: 10, // tries 40, 30, 20, 10 maxLines: 4, overflow: TextOverflow.ellipsis, ), ) // Result: font size jumps in steps of 10 — faster but less precise fit. ``` -------------------------------- ### textKey for Accessing Inner Text Widget Source: https://context7.com/simc/auto_size_text/llms.txt Use textKey to assign a key to the inner Text widget built by AutoSizeText. This allows the underlying Text widget to be found in widget tests or the widget tree independently of the outer AutoSizeText key. ```dart final innerTextKey = GlobalKey(); AutoSizeText( 'Hello World', key: Key('auto-size-widget'), // key for AutoSizeText itself textKey: innerTextKey, // key for the inner Text widget style: TextStyle(fontSize: 20), ) // In tests: // final text = tester.widget(find.byKey(innerTextKey)); // expect(text.data, 'Hello World'); ``` -------------------------------- ### Constrain AutoSizeText Font Size Range Source: https://context7.com/simc/auto_size_text/llms.txt Use `minFontSize` and `maxFontSize` to set the lower and upper bounds for the font size. If the text still overflows at `minFontSize`, the `overflow` property will be applied. `maxFontSize` is useful for constraining inherited font sizes. ```dart SizedBox( width: 300, height: 80, child: AutoSizeText( 'A really long string that needs to shrink but not too small', style: TextStyle(fontSize: 40), minFontSize: 18, // never go below 18 maxFontSize: 40, // never exceed 40 maxLines: 2, overflow: TextOverflow.ellipsis, // applied if text still doesn't fit at minFontSize ), ) // Result: font scales between 18–40. If text cannot fit at 18pt, it is ellipsized. ``` -------------------------------- ### wrapWords for Preventing Mid-Word Line Breaks Source: https://context7.com/simc/auto_size_text/llms.txt Set wrapWords to false to prevent individual words from being broken across lines. This forces further font downsizing until all words fit on their own lines. ```dart SizedBox( width: 120, height: 60, child: AutoSizeText( 'Supercalifragilistic', style: TextStyle(fontSize: 24), wrapWords: false, // shrinks font until the whole word fits on one line maxLines: 1, overflow: TextOverflow.ellipsis, ) ) ``` -------------------------------- ### Constrain AutoSizeText Width in Row Source: https://github.com/simc/auto_size_text/blob/master/README.md When using AutoSizeText within a Row, ensure it has a constrained width. Wrap it with Expanded to prevent overflow. ```dart Row( children: [ AutoSizeText( 'Here is a very long text, which should be resized', maxLines: 1, ), ], ) ``` ```dart Row( children: [ Expanded( // Constrains AutoSizeText to the width of the Row child: AutoSizeText( 'Here is a very long text, which should be resized', maxLines: 1, ) ), ], ) } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.