Update the Material IconAlignment sample (#179159)

Migrates the samples to remove references to the deprecated
`ButtonStyleButton.iconAlignment`.

The sample doesn't actually use `ButtonStyleButton.iconAlignment`
directly, but it does refer to it in docs.

Addresses: https://github.com/flutter/flutter/issues/179149

## Pre-launch Checklist

- [x] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [x] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [x] I read and followed the [Flutter Style Guide], including [Features
we expect every widget to implement].
- [x] I signed the [CLA].
- [x] I listed at least one issue that this PR fixes in the description
above.
- [x] I updated/added relevant documentation (doc comments with `///`).
- [x] I added new tests to check the change I am making, or this PR is
[test-exempt].
- [x] I followed the [breaking change policy] and added [Data Driven
Fixes] where supported.
- [x] All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel
on [Discord].

**Note**: The Flutter team is currently trialing the use of [Gemini Code
Assist for
GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code).
Comments from the `gemini-code-assist` bot should not be taken as
authoritative feedback from the Flutter team. If you find its comments
useful you can update your code accordingly, but if you are unsure or
disagree with the feedback, please feel free to wait for a Flutter team
member's review for guidance on which automated comments should be
addressed.

<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview
[Tree Hygiene]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md
[test-exempt]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests
[Flutter Style Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md
[Features we expect every widget to implement]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement
[CLA]: https://cla.developers.google.com/
[flutter/tests]: https://github.com/flutter/tests
[breaking change policy]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes
[Discord]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md
[Data Driven Fixes]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
This commit is contained in:
Loïc Sharma 2025-12-17 12:04:12 -08:00 committed by GitHub
parent ef3dbceed2
commit 297e7477d9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 126 additions and 133 deletions

View File

@ -5,11 +5,11 @@
import 'package:collection/collection.dart';
import 'package:flutter/material.dart';
// Flutter code sample for [DropdownMenu]s. The first dropdown menu
// has the default outlined border and demos using the
// [DropdownMenuEntry] style parameter to customize its appearance.
// The second dropdown menu customizes the appearance of the dropdown
// menu's text field with its [DropdownMenu.inputDecorationTheme] parameter.
/// Flutter code sample for [DropdownMenu]s. The first dropdown menu
/// has the default outlined border and demos using the
/// [DropdownMenuEntry] style parameter to customize its appearance.
/// The second dropdown menu customizes the appearance of the dropdown
/// menu's text field with its [DropdownMenu.inputDecorationTheme] parameter.
void main() {
runApp(const DropdownMenuExample());

View File

@ -4,34 +4,30 @@
import 'package:flutter/material.dart';
// ignore: deprecated_member_use
/// Flutter code sample for using [ButtonStyleButton.iconAlignment] parameter.
/// Flutter code sample for the [IconAlignment] property on various Material
/// button widgets.
void main() {
runApp(const ButtonStyleButtonIconAlignmentApp());
runApp(const IconAlignmentApp());
}
class ButtonStyleButtonIconAlignmentApp extends StatelessWidget {
const ButtonStyleButtonIconAlignmentApp({super.key});
class IconAlignmentApp extends StatelessWidget {
const IconAlignmentApp({super.key});
@override
Widget build(BuildContext context) {
return const MaterialApp(
home: Scaffold(body: ButtonStyleButtonIconAlignmentExample()),
);
return const MaterialApp(home: Scaffold(body: IconAlignmentExample()));
}
}
class ButtonStyleButtonIconAlignmentExample extends StatefulWidget {
const ButtonStyleButtonIconAlignmentExample({super.key});
class IconAlignmentExample extends StatefulWidget {
const IconAlignmentExample({super.key});
@override
State<ButtonStyleButtonIconAlignmentExample> createState() =>
_ButtonStyleButtonIconAlignmentExampleState();
State<IconAlignmentExample> createState() => _IconAlignmentExampleState();
}
class _ButtonStyleButtonIconAlignmentExampleState
extends State<ButtonStyleButtonIconAlignmentExample> {
class _IconAlignmentExampleState extends State<IconAlignmentExample> {
TextDirection _textDirection = TextDirection.ltr;
IconAlignment _iconAlignment = IconAlignment.start;

View File

@ -1,107 +0,0 @@
// 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';
import 'package:flutter_api_samples/material/button_style_button/button_style_button.icon_alignment.0.dart'
as example;
import 'package:flutter_test/flutter_test.dart';
void main() {
testWidgets(
'ButtonStyleButton.iconAlignment updates button icons alignment',
(WidgetTester tester) async {
await tester.pumpWidget(
const example.ButtonStyleButtonIconAlignmentApp(),
);
Finder findButtonMaterial(String text) {
return find
.ancestor(of: find.text(text), matching: find.byType(Material))
.first;
}
void expectedLeftIconPosition({
required double iconOffset,
required double textButtonIconOffset,
}) {
expect(
tester.getTopLeft(findButtonMaterial('ElevatedButton')).dx,
tester.getTopLeft(find.byIcon(Icons.sunny)).dx - iconOffset,
);
expect(
tester.getTopLeft(findButtonMaterial('FilledButton')).dx,
tester.getTopLeft(find.byIcon(Icons.beach_access)).dx - iconOffset,
);
expect(
tester.getTopLeft(findButtonMaterial('FilledButton Tonal')).dx,
tester.getTopLeft(find.byIcon(Icons.cloud)).dx - iconOffset,
);
expect(
tester.getTopLeft(findButtonMaterial('OutlinedButton')).dx,
tester.getTopLeft(find.byIcon(Icons.light)).dx - iconOffset,
);
expect(
tester.getTopLeft(findButtonMaterial('TextButton')).dx,
tester.getTopLeft(find.byIcon(Icons.flight_takeoff)).dx -
textButtonIconOffset,
);
}
void expectedRightIconPosition({
required double iconOffset,
required double textButtonIconOffset,
}) {
expect(
tester.getTopRight(findButtonMaterial('ElevatedButton')).dx,
tester.getTopRight(find.byIcon(Icons.sunny)).dx + iconOffset,
);
expect(
tester.getTopRight(findButtonMaterial('FilledButton')).dx,
tester.getTopRight(find.byIcon(Icons.beach_access)).dx + iconOffset,
);
expect(
tester.getTopRight(findButtonMaterial('FilledButton Tonal')).dx,
tester.getTopRight(find.byIcon(Icons.cloud)).dx + iconOffset,
);
expect(
tester.getTopRight(findButtonMaterial('OutlinedButton')).dx,
tester.getTopRight(find.byIcon(Icons.light)).dx + iconOffset,
);
expect(
tester.getTopRight(findButtonMaterial('TextButton')).dx,
tester.getTopRight(find.byIcon(Icons.flight_takeoff)).dx +
textButtonIconOffset,
);
}
// Test initial icon alignment in LTR.
expectedLeftIconPosition(iconOffset: 16, textButtonIconOffset: 12);
// Update icon alignment to end.
await tester.tap(find.text('end'));
await tester.pumpAndSettle();
// Test icon alignment end in LTR.
expectedRightIconPosition(iconOffset: 24, textButtonIconOffset: 16);
// Reset icon alignment to start.
await tester.tap(find.text('start'));
await tester.pumpAndSettle();
// Change text direction to RTL.
await tester.tap(find.text('RTL'));
await tester.pumpAndSettle();
// Test icon alignment start in LTR.
expectedRightIconPosition(iconOffset: 16, textButtonIconOffset: 12);
// Update icon alignment to end.
await tester.tap(find.text('end'));
await tester.pumpAndSettle();
// Test icon alignment end in LTR.
expectedLeftIconPosition(iconOffset: 24, textButtonIconOffset: 16);
},
);
}

View File

@ -0,0 +1,104 @@
// 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';
import 'package:flutter_api_samples/material/icon_alignment/icon_alignment.0.dart'
as example;
import 'package:flutter_test/flutter_test.dart';
void main() {
testWidgets("IconAlignment updates buttons' icons alignment", (
WidgetTester tester,
) async {
await tester.pumpWidget(const example.IconAlignmentApp());
Finder findButtonMaterial(String text) {
return find
.ancestor(of: find.text(text), matching: find.byType(Material))
.first;
}
void expectedLeftIconPosition({
required double iconOffset,
required double textButtonIconOffset,
}) {
expect(
tester.getTopLeft(findButtonMaterial('ElevatedButton')).dx,
tester.getTopLeft(find.byIcon(Icons.sunny)).dx - iconOffset,
);
expect(
tester.getTopLeft(findButtonMaterial('FilledButton')).dx,
tester.getTopLeft(find.byIcon(Icons.beach_access)).dx - iconOffset,
);
expect(
tester.getTopLeft(findButtonMaterial('FilledButton Tonal')).dx,
tester.getTopLeft(find.byIcon(Icons.cloud)).dx - iconOffset,
);
expect(
tester.getTopLeft(findButtonMaterial('OutlinedButton')).dx,
tester.getTopLeft(find.byIcon(Icons.light)).dx - iconOffset,
);
expect(
tester.getTopLeft(findButtonMaterial('TextButton')).dx,
tester.getTopLeft(find.byIcon(Icons.flight_takeoff)).dx -
textButtonIconOffset,
);
}
void expectedRightIconPosition({
required double iconOffset,
required double textButtonIconOffset,
}) {
expect(
tester.getTopRight(findButtonMaterial('ElevatedButton')).dx,
tester.getTopRight(find.byIcon(Icons.sunny)).dx + iconOffset,
);
expect(
tester.getTopRight(findButtonMaterial('FilledButton')).dx,
tester.getTopRight(find.byIcon(Icons.beach_access)).dx + iconOffset,
);
expect(
tester.getTopRight(findButtonMaterial('FilledButton Tonal')).dx,
tester.getTopRight(find.byIcon(Icons.cloud)).dx + iconOffset,
);
expect(
tester.getTopRight(findButtonMaterial('OutlinedButton')).dx,
tester.getTopRight(find.byIcon(Icons.light)).dx + iconOffset,
);
expect(
tester.getTopRight(findButtonMaterial('TextButton')).dx,
tester.getTopRight(find.byIcon(Icons.flight_takeoff)).dx +
textButtonIconOffset,
);
}
// Test initial icon alignment in LTR.
expectedLeftIconPosition(iconOffset: 16, textButtonIconOffset: 12);
// Update icon alignment to end.
await tester.tap(find.text('end'));
await tester.pumpAndSettle();
// Test icon alignment end in LTR.
expectedRightIconPosition(iconOffset: 24, textButtonIconOffset: 16);
// Reset icon alignment to start.
await tester.tap(find.text('start'));
await tester.pumpAndSettle();
// Change text direction to RTL.
await tester.tap(find.text('RTL'));
await tester.pumpAndSettle();
// Test icon alignment start in LTR.
expectedRightIconPosition(iconOffset: 16, textButtonIconOffset: 12);
// Update icon alignment to end.
await tester.tap(find.text('end'));
await tester.pumpAndSettle();
// Test icon alignment end in LTR.
expectedLeftIconPosition(iconOffset: 24, textButtonIconOffset: 16);
});
}

View File

@ -29,7 +29,7 @@ import 'theme.dart';
import 'theme_data.dart';
import 'tooltip.dart';
/// {@template flutter.material.ButtonStyleButton.iconAlignment}
/// {@template flutter.material.ButtonStyle.iconAlignment}
/// Determines the alignment of the icon within the widgets such as:
/// - [ElevatedButton.icon],
/// - [FilledButton.icon],
@ -48,7 +48,7 @@ import 'tooltip.dart';
/// This sample demonstrates how to use `iconAlignment` to align the button icon to the start
/// or the end of the button.
///
/// ** See code in examples/api/lib/material/button_style_button/button_style_button.icon_alignment.0.dart **
/// ** See code in examples/api/lib/material/icon_alignment/icon_alignment.0.dart **
/// {@end-tool}
///
/// {@endtemplate}
@ -163,7 +163,7 @@ abstract class ButtonStyleButton extends StatefulWidget {
/// Defaults to true.
final bool? isSemanticButton;
/// {@macro flutter.material.ButtonStyleButton.iconAlignment}
/// {@macro flutter.material.ButtonStyle.iconAlignment}
@Deprecated(
'Remove this parameter as it is now ignored. '
'Use ButtonStyle.iconAlignment instead. '

View File

@ -91,7 +91,7 @@ class ElevatedButton extends ButtonStyleButton {
/// If [icon] is null, this constructor will create an [ElevatedButton]
/// that doesn't display an icon.
///
/// {@macro flutter.material.ButtonStyleButton.iconAlignment}
/// {@macro flutter.material.ButtonStyle.iconAlignment}
///
ElevatedButton.icon({
super.key,

View File

@ -101,7 +101,7 @@ class FilledButton extends ButtonStyleButton {
/// If [icon] is null, this constructor will create a [FilledButton]
/// that doesn't display an icon.
///
/// {@macro flutter.material.ButtonStyleButton.iconAlignment}
/// {@macro flutter.material.ButtonStyle.iconAlignment}
///
FilledButton.icon({
super.key,

View File

@ -95,7 +95,7 @@ class OutlinedButton extends ButtonStyleButton {
/// If [icon] is null, this constructor will create an outlined button
/// that doesn't display an icon.
///
/// {@macro flutter.material.ButtonStyleButton.iconAlignment}
/// {@macro flutter.material.ButtonStyle.iconAlignment}
///
OutlinedButton.icon({
super.key,

View File

@ -104,7 +104,7 @@ class TextButton extends ButtonStyleButton {
/// If [icon] is null, this constructor will create a [TextButton]
/// that doesn't display an icon.
///
/// {@macro flutter.material.ButtonStyleButton.iconAlignment}
/// {@macro flutter.material.ButtonStyle.iconAlignment}
///
TextButton.icon({
super.key,