Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ linter:
rules:
- avoid_function_literals_in_foreach_calls
- avoid_renaming_method_parameters
- avoid_returning_null
- avoid_unused_constructor_parameters
- await_only_futures
- camel_case_types
Expand All @@ -25,13 +24,10 @@ linter:
- empty_statements
- implementation_imports
- invariant_booleans
- iterable_contains_unrelated_type
- list_remove_unrelated_type
- no_adjacent_strings_in_list
- non_constant_identifier_names
- only_throw_errors
- overridden_fields
- package_api_docs
- package_names
- package_prefixed_library_names
- prefer_final_locals
Expand Down
7 changes: 4 additions & 3 deletions demo/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,17 @@ apply plugin: 'com.android.application'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

android {
compileSdkVersion 27
namespace 'com.github.leisim.auto_size_text.demo'
compileSdk 34

lintOptions {
disable 'InvalidPackage'
}

defaultConfig {
applicationId "com.github.leisim.auto_size_text.demo"
minSdkVersion 16
targetSdkVersion 27
minSdkVersion 21
targetSdkVersion 34
versionCode 1
versionName 'v1'
}
Expand Down
1 change: 1 addition & 0 deletions demo/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
android:icon="@drawable/ic_launcher">
<activity
android:name=".MainActivity"
android:exported="true"
android:launchMode="singleTop"
android:theme="@style/LaunchTheme"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package com.github.leisim.auto_size_text.demo;

import android.os.Bundle;
import io.flutter.app.FlutterActivity;

import io.flutter.embedding.android.FlutterActivity;
import io.flutter.plugins.GeneratedPluginRegistrant;

public class MainActivity extends FlutterActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GeneratedPluginRegistrant.registerWith(this);
GeneratedPluginRegistrant.registerWith(this.getFlutterEngine());
}
}
2 changes: 1 addition & 1 deletion demo/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
}

dependencies {
classpath 'com.android.tools.build:gradle:3.1.2'
classpath 'com.android.tools.build:gradle:7.4.2'
}
}

Expand Down
1 change: 1 addition & 0 deletions demo/android/gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
org.gradle.jvmargs=-Xmx1536M
android.useAndroidX=true
3 changes: 1 addition & 2 deletions demo/android/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#Fri Jun 23 08:50:38 CEST 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip
7 changes: 1 addition & 6 deletions demo/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class App extends StatelessWidget {
DeviceOrientation.landscapeRight,
]);

SystemChrome.setEnabledSystemUIOverlays([]);
SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual, overlays: []);

return MaterialApp(
theme: ThemeData.light(),
Expand Down Expand Up @@ -133,11 +133,6 @@ class _DemoAppState extends State<DemoApp> {
title: Text('preset'),
activeColor: colors[4],
),
BottomNavyBarItem(
icon: Icon(MdiIcons.stackOverflow),
title: Text('replacement'),
activeColor: colors[5],
),
],
),
);
Expand Down
12 changes: 6 additions & 6 deletions demo/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
name: demo
description: AutoSizeText Demo App

version: 1.0.0+2
version: 1.0.0+3

environment:
sdk: '>=2.12.0-0 <3.0.0'
sdk: ">=3.2.0 <4.0.0"

dependencies:
flutter:
Expand All @@ -13,14 +13,14 @@ dependencies:
auto_size_text:
path: ../

bottom_navy_bar: ^4.2.0
material_design_icons_flutter: ^4.0.5755
bottom_navy_bar: ^6.1.0
material_design_icons_flutter: ^7.0.7296

dev_dependencies:
flutter_test:
flutter_test:
sdk: flutter

flutter:
uses-material-design: true

publish_to: none
publish_to: none
1 change: 1 addition & 0 deletions lib/auto_size_text.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
library auto_size_text;

import 'dart:async';
import 'dart:math';

import 'package:flutter/widgets.dart';

Expand Down
71 changes: 54 additions & 17 deletions lib/src/auto_size_text.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class AutoSizeText extends StatefulWidget {
this.overflowReplacement,
this.textScaleFactor,
this.maxLines,
this.minLetterSpacing,
this.semanticsLabel,
}) : textSpan = null,
super(key: key);
Expand All @@ -56,6 +57,7 @@ class AutoSizeText extends StatefulWidget {
this.overflowReplacement,
this.textScaleFactor,
this.maxLines,
this.minLetterSpacing,
this.semanticsLabel,
}) : data = null,
super(key: key);
Expand Down Expand Up @@ -184,7 +186,7 @@ class AutoSizeText extends StatefulWidget {
/// This property also affects [minFontSize], [maxFontSize] and [presetFontSizes].
///
/// The value given to the constructor as textScaleFactor. If null, will
/// use the [MediaQueryData.textScaleFactor] obtained from the ambient
/// use [TextScaler] obtained from the ambient
/// [MediaQuery], or 1.0 if there is no [MediaQuery] in scope.
final double? textScaleFactor;

Expand All @@ -201,6 +203,17 @@ class AutoSizeText extends StatefulWidget {
/// widget directly to entirely override the [DefaultTextStyle].
final int? maxLines;

/// The minimum letter spacing constraint to be used when auto-sizing text.
///
/// When specified, if the minimum font size is achieved and the text still
/// doesn't fit the available area, the letter spacing will be decreased until
/// the text fits or the [minLetterSpacing] value is achieved. It is decreased
/// by [stepGranularity] on each iteration.
final double? minLetterSpacing;

// The default letter spacing if none is specified.
static const double _defaultLetterSpacing = 0;

/// An alternative semantics label for this text.
///
/// If present, the semantics of this widget will contain this value instead
Expand Down Expand Up @@ -254,17 +267,20 @@ class _AutoSizeTextState extends State<AutoSizeText> {

_validateProperties(style, maxLines);

final result = _calculateFontSize(size, style, maxLines);
final result =
_calculateFontSize(size, style, maxLines, widget.minLetterSpacing);
final fontSize = result[0] as double;
final textFits = result[1] as bool;
final letterSpacing = result[2] as double?;

Widget text;

if (widget.group != null) {
widget.group!._updateFontSize(this, fontSize);
text = _buildText(widget.group!._fontSize, style, maxLines);
text =
_buildText(widget.group!._fontSize, letterSpacing, style, maxLines);
} else {
text = _buildText(fontSize, style, maxLines);
text = _buildText(fontSize, letterSpacing, style, maxLines);
}

if (widget.overflowReplacement != null && !textFits) {
Expand Down Expand Up @@ -306,16 +322,21 @@ class _AutoSizeTextState extends State<AutoSizeText> {
}

List _calculateFontSize(
BoxConstraints size, TextStyle? style, int? maxLines) {
BoxConstraints size,
TextStyle? style,
int? maxLines,
double? minLetterSpacing,
) {
final span = TextSpan(
style: widget.textSpan?.style ?? style,
text: widget.textSpan?.text ?? widget.data,
children: widget.textSpan?.children,
recognizer: widget.textSpan?.recognizer,
);

final userScale =
widget.textScaleFactor ?? MediaQuery.textScaleFactorOf(context);
final userScale = widget.textScaleFactor ?? 1.0;

var letterSpacing = span.style!.letterSpacing;

int left;
int right;
Expand All @@ -325,8 +346,9 @@ class _AutoSizeTextState extends State<AutoSizeText> {
final num defaultFontSize =
style!.fontSize!.clamp(widget.minFontSize, widget.maxFontSize);
final defaultScale = defaultFontSize * userScale / style.fontSize!;

if (_checkTextFits(span, defaultScale, maxLines, size)) {
return <Object>[defaultFontSize * userScale, true];
return <Object?>[defaultFontSize * userScale, true, letterSpacing];
}

left = (widget.minFontSize / widget.stepGranularity).floor();
Expand All @@ -337,9 +359,9 @@ class _AutoSizeTextState extends State<AutoSizeText> {
}

var lastValueFits = false;
var scale = userScale;
while (left <= right) {
final mid = (left + (right - left) / 2).floor();
double scale;
if (presetFontSizes == null) {
scale = mid * userScale * widget.stepGranularity / style!.fontSize!;
} else {
Expand All @@ -355,6 +377,20 @@ class _AutoSizeTextState extends State<AutoSizeText> {

if (!lastValueFits) {
right += 1;
if (minLetterSpacing != null) {
letterSpacing = letterSpacing ?? AutoSizeText._defaultLetterSpacing;
do {
letterSpacing =
max(minLetterSpacing, letterSpacing! - widget.stepGranularity);
final stepSpan = TextSpan(
style: span.style!.copyWith(letterSpacing: letterSpacing),
text: span.text,
children: span.children,
recognizer: span.recognizer,
);
lastValueFits = _checkTextFits(stepSpan, scale, maxLines, size);
} while (!lastValueFits && letterSpacing > minLetterSpacing);
}
}

double fontSize;
Expand All @@ -364,7 +400,7 @@ class _AutoSizeTextState extends State<AutoSizeText> {
fontSize = presetFontSizes[right] * userScale;
}

return <Object>[fontSize, lastValueFits];
return <Object?>[fontSize, lastValueFits, letterSpacing];
}

bool _checkTextFits(
Expand All @@ -379,7 +415,7 @@ class _AutoSizeTextState extends State<AutoSizeText> {
),
textAlign: widget.textAlign ?? TextAlign.left,
textDirection: widget.textDirection ?? TextDirection.ltr,
textScaleFactor: scale,
textScaler: TextScaler.linear(scale),
maxLines: words.length,
locale: widget.locale,
strutStyle: widget.strutStyle,
Expand All @@ -397,7 +433,7 @@ class _AutoSizeTextState extends State<AutoSizeText> {
text: text,
textAlign: widget.textAlign ?? TextAlign.left,
textDirection: widget.textDirection ?? TextDirection.ltr,
textScaleFactor: scale,
textScaler: TextScaler.linear(scale),
maxLines: maxLines,
locale: widget.locale,
strutStyle: widget.strutStyle,
Expand All @@ -410,34 +446,35 @@ class _AutoSizeTextState extends State<AutoSizeText> {
textPainter.width > constraints.maxWidth);
}

Widget _buildText(double fontSize, TextStyle style, int? maxLines) {
Widget _buildText(
double fontSize, double? letterSpacing, TextStyle style, int? maxLines) {
if (widget.data != null) {
return Text(
widget.data!,
key: widget.textKey,
style: style.copyWith(fontSize: fontSize),
style: style.copyWith(fontSize: fontSize, letterSpacing: letterSpacing),
strutStyle: widget.strutStyle,
textAlign: widget.textAlign,
textDirection: widget.textDirection,
locale: widget.locale,
softWrap: widget.softWrap,
overflow: widget.overflow,
textScaleFactor: 1,
textScaler: TextScaler.noScaling,
maxLines: maxLines,
semanticsLabel: widget.semanticsLabel,
);
} else {
return Text.rich(
widget.textSpan!,
key: widget.textKey,
style: style,
style: style.copyWith(letterSpacing: letterSpacing),
strutStyle: widget.strutStyle,
textAlign: widget.textAlign,
textDirection: widget.textDirection,
locale: widget.locale,
softWrap: widget.softWrap,
overflow: widget.overflow,
textScaleFactor: fontSize / style.fontSize!,
textScaler: TextScaler.linear(fontSize / style.fontSize!),
maxLines: maxLines,
semanticsLabel: widget.semanticsLabel,
);
Expand Down
6 changes: 3 additions & 3 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
name: auto_size_text
description: Flutter widget that automatically resizes text to fit perfectly within its bounds.
version: 3.0.0
version: 3.1.0
homepage: https://github.com/leisim/auto_size_text

environment:
sdk: '>=2.12.0 <3.0.0'
sdk: ">=3.2.0 <4.0.0"

dependencies:
flutter:
Expand All @@ -13,4 +13,4 @@ dependencies:
dev_dependencies:
flutter_test:
sdk: flutter
pedantic: '>=1.11.1 <3.0.0'
lints: ^4.0.0
Loading