mirror of
https://github.com/flutter/flutter.git
synced 2025-06-03 00:51:18 +00:00

Deprecate `textScaleFactor` in favor of `textScaler`, in preparation for Android 14 [Non-linear font scaling to 200%](https://developer.android.com/about/versions/14/features#non-linear-font-scaling). The `TextScaler` class can be moved to `dart:ui` in the future, if we decide to use the Android platform API or AndroidX to get the scaling curve instead of hard coding the curve in the framework. I haven't put the Flutter version in the deprecation message so the analyzer checks are failing. Will do so after I finish the migration guide. **Why `TextScaler.textScaleFactor`** The author of a `TextScaler` subclass should provide a fallback `textScaleFactor`. By making `TextScaler` also contain the `textScaleFactor` information it also makes it easier to migrate: if a widget overrides `MediaQueryData.textScaler` in the tree, for unmigrated widgets in the subtree it would also have to override `MediaQueryData.textScaleFactor`, and that makes it difficult to remove `MediaQueryData.textScaleFactor` in the future. ## A full list of affected APIs in this PR Deprecated: The method/getter/setter/argument is annotated with a `@Deprecated()` annotation in this PR, and the caller should replace it with `textScaler` instead. Unless otherwise specified there will be a Flutter fix available to help with migration but it's still recommended to migrate case-by-case. **Replaced**: The method this `textScaleFactor` argument belongs to is rarely called directly by user code and is not overridden by any of the registered custom tests, so the argument is directly replaced by `TextScaler`. **To Be Deprecated**: The method/getter/setter/argument can't be deprecated in this PR because a registered customer test depends on it and a Flutter fix isn't available (or the test was run without applying flutter fixes first). This method/getter/setter/argument will be deprecated in a followup PR once the registered test is migrated. ### `Painting` Library | Affected API | State of `textScaleFactor` | Comment | | --- | --- | --- | | `InlineSpan.build({ double textScaleFactor = 1.0 })` argument | **Replaced** | | | `TextStyle.getParagraphStyle({ double TextScaleFactor = 1.0 })` argument | **Replaced** | | | `TextStyle.getTextStyle({ double TextScaleFactor = 1.0 })` argument| Deprecated | Can't replace:c47fd38dca/super_editor/lib/src/infrastructure/super_textfield/desktop/desktop_textfield.dart (L1903-L1905)
| | `TextPainter({ double TextScaleFactor = 1.0 })` constructor argument | Deprecated | | | `TextPainter.textScaleFactor` getter and setter | Deprecated | No Flutter Fix, not expressible yet | | `TextPainter.computeWidth({ double TextScaleFactor = 1.0 })` argument | Deprecated | | | `TextPainter.computeMaxIntrinsicWidth({ double TextScaleFactor = 1.0 })` argument | Deprecated | | ### `Rendering` Library | Affected API | State of `textScaleFactor` | Comment | | --- | --- | --- | | `RenderEditable({ double TextScaleFactor = 1.0 })` constructor argument | Deprecated | | | `RenderEditable.textScaleFactor` getter and setter | Deprecated | No Flutter Fix, not expressible yet | | `RenderParagraph({ double TextScaleFactor = 1.0 })` constructor argument | Deprecated | | | `RenderParagraph.textScaleFactor` getter and setter | Deprecated | No Flutter Fix, not expressible yet | ### `Widgets` Library | Affected API | State of `textScaleFactor` | Comment | | --- | --- | --- | | `MediaQueryData({ double TextScaleFactor = 1.0 })` constructor argument | **To Be Deprecated** |cd7b93532e/packages/flutter_markdown/test/text_scale_factor_test.dart (LL39C21-L39C35)
| | `MediaQueryData.textScaleFactor` getter | Deprecated | | | `MediaQueryData.copyWith({ double? TextScaleFactor })` argument | Deprecated | | | `MediaQuery.maybeTextScaleFactorOf(BuildContext context)` static method | Deprecated | No Flutter Fix, not expressible yet | | `MediaQuery.textScaleFactorOf(BuildContext context)` static method | **To Be Deprecated** |cd7b93532e/packages/flutter_markdown/lib/src/_functions_io.dart (L68-L70)
, No Flutter Fix, not expressible yet | | `RichText({ double TextScaleFactor = 1.0 })` constructor argument | **To Be Deprecated** |cd7b93532e/packages/flutter_markdown/lib/src/builder.dart (L829-L843)
| | `RichText.textScaleFactor` getter | **To Be Deprecated** | A constructor argument can't be deprecated right away| | `Text({ double? TextScaleFactor = 1.0 })` constructor argument | **To Be Deprecated** |914d120da1/packages/rfw/lib/src/flutter/core_widgets.dart (L647)
, No Flutter Fix because of https://github.com/dart-lang/sdk/issues/52664 | | `Text.rich({ double? TextScaleFactor = 1.0 })` constructor argument | **To Be Deprecated** | The default constructor has an argument that can't be deprecated right away. No Flutter Fix because of https://github.com/dart-lang/sdk/issues/52664 | | `Text.textScaleFactor` getter | **To Be Deprecated** | A constructor argument can't be deprecated right away | | `EditableText({ double? TextScaleFactor = 1.0 })` constructor argument | Deprecated | No Flutter Fix because of https://github.com/dart-lang/sdk/issues/52664 | | `EditableText.textScaleFactor` getter | Deprecated | | ### `Material` Library | Affected API | State of `textScaleFactor` | Comment | | --- | --- | --- | | `SelectableText({ double? TextScaleFactor = 1.0 })` constructor argument | **To Be Deprecated** |cd7b93532e/packages/flutter_markdown/lib/src/builder.dart (L829-L843)
, No Flutter Fix because of https://github.com/dart-lang/sdk/issues/52664 | | `SelectableText.rich({ double? TextScaleFactor = 1.0 })` constructor argument | **To Be Deprecated** | The default constructor has an argument that can't be deprecated right away. No Flutter Fix because of https://github.com/dart-lang/sdk/issues/52664 | | `SelectableText.textScaleFactor` getter | **To Be Deprecated** | A constructor argument can't be deprecated right away | A lot of material widgets (`Slider`, `RangeSlider`, `TimePicker`, and different types of buttons) also change their layout based on `textScaleFactor`. These need to be handled in a case-by-case fashion and will be migrated in follow-up PRs.
133 lines
3.8 KiB
Dart
133 lines
3.8 KiB
Dart
// Copyright 2014 The Flutter Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
// found in the LICENSE file.
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
/// Flutter code sample for [SliverAppBar].
|
|
|
|
void main() => runApp(const AppBarApp());
|
|
|
|
class AppBarApp extends StatelessWidget {
|
|
const AppBarApp({super.key});
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return const MaterialApp(
|
|
home: SliverAppBarExample(),
|
|
);
|
|
}
|
|
}
|
|
|
|
class SliverAppBarExample extends StatefulWidget {
|
|
const SliverAppBarExample({super.key});
|
|
|
|
@override
|
|
State<SliverAppBarExample> createState() => _SliverAppBarExampleState();
|
|
}
|
|
|
|
class _SliverAppBarExampleState extends State<SliverAppBarExample> {
|
|
bool _pinned = true;
|
|
bool _snap = false;
|
|
bool _floating = false;
|
|
|
|
// [SliverAppBar]s are typically used in [CustomScrollView.slivers], which in
|
|
// turn can be placed in a [Scaffold.body].
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
body: CustomScrollView(
|
|
slivers: <Widget>[
|
|
SliverAppBar(
|
|
pinned: _pinned,
|
|
snap: _snap,
|
|
floating: _floating,
|
|
expandedHeight: 160.0,
|
|
flexibleSpace: const FlexibleSpaceBar(
|
|
title: Text('SliverAppBar'),
|
|
background: FlutterLogo(),
|
|
),
|
|
),
|
|
const SliverToBoxAdapter(
|
|
child: SizedBox(
|
|
height: 20,
|
|
child: Center(
|
|
child: Text('Scroll to see the SliverAppBar in effect.'),
|
|
),
|
|
),
|
|
),
|
|
SliverList(
|
|
delegate: SliverChildBuilderDelegate(
|
|
(BuildContext context, int index) {
|
|
return Container(
|
|
color: index.isOdd ? Colors.white : Colors.black12,
|
|
height: 100.0,
|
|
child: Center(
|
|
child: Text('$index', textScaler: const TextScaler.linear(5)),
|
|
),
|
|
);
|
|
},
|
|
childCount: 20,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
bottomNavigationBar: BottomAppBar(
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(8),
|
|
child: OverflowBar(
|
|
overflowAlignment: OverflowBarAlignment.center,
|
|
children: <Widget>[
|
|
Row(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: <Widget>[
|
|
const Text('pinned'),
|
|
Switch(
|
|
onChanged: (bool val) {
|
|
setState(() {
|
|
_pinned = val;
|
|
});
|
|
},
|
|
value: _pinned,
|
|
),
|
|
],
|
|
),
|
|
Row(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: <Widget>[
|
|
const Text('snap'),
|
|
Switch(
|
|
onChanged: (bool val) {
|
|
setState(() {
|
|
_snap = val;
|
|
// Snapping only applies when the app bar is floating.
|
|
_floating = _floating || _snap;
|
|
});
|
|
},
|
|
value: _snap,
|
|
),
|
|
],
|
|
),
|
|
Row(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: <Widget>[
|
|
const Text('floating'),
|
|
Switch(
|
|
onChanged: (bool val) {
|
|
setState(() {
|
|
_floating = val;
|
|
_snap = _snap && _floating;
|
|
});
|
|
},
|
|
value: _floating,
|
|
),
|
|
],
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|