Introduce iconAlignment for the buttons with icon (#137348)

Adds `iconAlignment` property to `ButtonStyleButton` widget.

Fixes #89564

### Example

https://github.com/flutter/flutter/assets/13456345/1b5236c4-5c60-4915-b3c6-0a56c43f8a19
This commit is contained in:
Tirth
2024-02-12 22:38:20 +05:30
committed by GitHub
parent 82668f1688
commit 10442399fb
11 changed files with 942 additions and 17 deletions

View File

@@ -0,0 +1,147 @@
// 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 using [ButtonStyleButton.iconAlignment] parameter.
void main() {
runApp(const ButtonStyleButtonIconAlignmentApp());
}
class ButtonStyleButtonIconAlignmentApp extends StatelessWidget {
const ButtonStyleButtonIconAlignmentApp({super.key});
@override
Widget build(BuildContext context) {
return const MaterialApp(
home: Scaffold(
body: ButtonStyleButtonIconAlignmentExample(),
),
);
}
}
class ButtonStyleButtonIconAlignmentExample extends StatefulWidget {
const ButtonStyleButtonIconAlignmentExample({super.key});
@override
State<ButtonStyleButtonIconAlignmentExample> createState() => _ButtonStyleButtonIconAlignmentExampleState();
}
class _ButtonStyleButtonIconAlignmentExampleState extends State<ButtonStyleButtonIconAlignmentExample> {
TextDirection _textDirection = TextDirection.ltr;
IconAlignment _iconAlignment = IconAlignment.start;
@override
Widget build(BuildContext context) {
return SafeArea(
child: Directionality(
key: const Key('Directionality'),
textDirection: _textDirection,
child: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Spacer(),
OverflowBar(
spacing: 10,
overflowSpacing: 20,
alignment: MainAxisAlignment.center,
overflowAlignment: OverflowBarAlignment.center,
children: <Widget>[
ElevatedButton.icon(
onPressed: () {},
icon: const Icon(Icons.sunny),
label: const Text('ElevatedButton'),
iconAlignment: _iconAlignment,
),
FilledButton.icon(
onPressed: () {},
icon: const Icon(Icons.beach_access),
label: const Text('FilledButton'),
iconAlignment: _iconAlignment,
),
FilledButton.tonalIcon(
onPressed: () {},
icon: const Icon(Icons.cloud),
label: const Text('FilledButton Tonal'),
iconAlignment: _iconAlignment,
),
OutlinedButton.icon(
onPressed: () {},
icon: const Icon(Icons.light),
label: const Text('OutlinedButton'),
iconAlignment: _iconAlignment,
),
TextButton.icon(
onPressed: () {},
icon: const Icon(Icons.flight_takeoff),
label: const Text('TextButton'),
iconAlignment: _iconAlignment,
),
],
),
const Spacer(),
OverflowBar(
alignment: MainAxisAlignment.spaceEvenly,
overflowAlignment: OverflowBarAlignment.center,
spacing: 10,
overflowSpacing: 10,
children: <Widget>[
Column(
children: <Widget>[
const Text('Icon alignment'),
const SizedBox(height: 10),
SegmentedButton<IconAlignment>(
onSelectionChanged: (Set<IconAlignment> value) {
setState(() {
_iconAlignment = value.first;
});
},
selected: <IconAlignment>{ _iconAlignment },
segments: IconAlignment.values.map((IconAlignment iconAlignment) {
return ButtonSegment<IconAlignment>(
value: iconAlignment,
label: Text(iconAlignment.name),
);
}).toList(),
),
],
),
Column(
children: <Widget>[
const Text('Text direction'),
const SizedBox(height: 10),
SegmentedButton<TextDirection>(
onSelectionChanged: (Set<TextDirection> value) {
setState(() {
_textDirection = value.first;
});
},
selected: <TextDirection>{ _textDirection },
segments: const <ButtonSegment<TextDirection>>[
ButtonSegment<TextDirection>(
value: TextDirection.ltr,
label: Text('LTR'),
),
ButtonSegment<TextDirection>(
value: TextDirection.rtl,
label: Text('RTL'),
),
],
),
],
),
],
),
const Spacer(),
],
),
),
),
);
}
}

View File

@@ -0,0 +1,102 @@
// 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);
});
}