From 76df7368b523de75acee6d185f88052d561a01a8 Mon Sep 17 00:00:00 2001 From: Taha Tesser Date: Fri, 29 Jul 2022 10:17:06 +0300 Subject: [PATCH] Update Chips examples and rename files (#108538) --- .../material/action_chip/action_chip.0.dart | 52 +++++++++++ .../material/choice_chip/choice_chip.0.dart | 70 +++++++++++++++ .../material/filter_chip/filter_chip.0.dart | 79 +++++++++++++++++ .../lib/material/input_chip/input_chip.0.dart | 86 +++++++++++++++++++ .../action_chip/action_chip.0_test.dart | 22 +++++ .../choice_chip/choice_chip.0_test.dart | 24 ++++++ .../filter_chip/filter_chip.0_test.dart | 36 ++++++++ .../input_chip/input_chip.0_test.dart | 33 +++++++ packages/flutter/lib/material.dart | 8 +- .../{chip_action.dart => action_chip.dart} | 17 +--- packages/flutter/lib/src/material/chip.dart | 3 - .../{chip_choice.dart => choice_chip.dart} | 37 +------- .../{chip_filter.dart => filter_chip.dart} | 65 +------------- .../{chip_input.dart => input_chip.dart} | 18 ++-- 14 files changed, 422 insertions(+), 128 deletions(-) create mode 100644 examples/api/lib/material/action_chip/action_chip.0.dart create mode 100644 examples/api/lib/material/choice_chip/choice_chip.0.dart create mode 100644 examples/api/lib/material/filter_chip/filter_chip.0.dart create mode 100644 examples/api/lib/material/input_chip/input_chip.0.dart create mode 100644 examples/api/test/material/action_chip/action_chip.0_test.dart create mode 100644 examples/api/test/material/choice_chip/choice_chip.0_test.dart create mode 100644 examples/api/test/material/filter_chip/filter_chip.0_test.dart create mode 100644 examples/api/test/material/input_chip/input_chip.0_test.dart rename packages/flutter/lib/src/material/{chip_action.dart => action_chip.dart} (95%) rename packages/flutter/lib/src/material/{chip_choice.dart => choice_chip.dart} (90%) rename packages/flutter/lib/src/material/{chip_filter.dart => filter_chip.dart} (81%) rename packages/flutter/lib/src/material/{chip_input.dart => input_chip.dart} (96%) diff --git a/examples/api/lib/material/action_chip/action_chip.0.dart b/examples/api/lib/material/action_chip/action_chip.0.dart new file mode 100644 index 0000000000..9bbc8bc502 --- /dev/null +++ b/examples/api/lib/material/action_chip/action_chip.0.dart @@ -0,0 +1,52 @@ +// 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. + +// Flutter code sample ActionChip + +import 'package:flutter/material.dart'; + +void main() => runApp(const ChipApp()); + +class ChipApp extends StatelessWidget { + const ChipApp({super.key}); + + @override + Widget build(BuildContext context) { + return MaterialApp( + theme: ThemeData(colorSchemeSeed: const Color(0xff6750a4), useMaterial3: true), + home: const ActionChipExample(), + ); + } +} + +class ActionChipExample extends StatefulWidget { + const ActionChipExample({super.key}); + + @override + State createState() => _ActionChipExampleState(); +} + +class _ActionChipExampleState extends State { + bool favorite = false; + + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar( + title: const Text('ActionChip Sample'), + ), + body: Center( + child: ActionChip( + avatar: Icon(favorite ? Icons.favorite : Icons.favorite_border), + label: const Text('Save to favorites'), + onPressed: () { + setState(() { + favorite = !favorite; + }); + }, + ), + ), + ); + } +} diff --git a/examples/api/lib/material/choice_chip/choice_chip.0.dart b/examples/api/lib/material/choice_chip/choice_chip.0.dart new file mode 100644 index 0000000000..0a920183f0 --- /dev/null +++ b/examples/api/lib/material/choice_chip/choice_chip.0.dart @@ -0,0 +1,70 @@ +// 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. + +// Flutter code sample ActionChoice + +import 'package:flutter/material.dart'; + +void main() => runApp(const ChipApp()); + +class ChipApp extends StatelessWidget { + const ChipApp({super.key}); + + @override + Widget build(BuildContext context) { + return MaterialApp( + theme: ThemeData(colorSchemeSeed: const Color(0xff6750a4), useMaterial3: true), + home: const ActionChoiceExample(), + ); + } +} + +class ActionChoiceExample extends StatefulWidget { + const ActionChoiceExample({super.key}); + + @override + State createState() => _ActionChoiceExampleState(); +} + +class _ActionChoiceExampleState extends State { + int? _value = 1; + + @override + Widget build(BuildContext context) { + final TextTheme textTheme = Theme.of(context).textTheme; + + return Scaffold( + appBar: AppBar( + title: const Text('ActionChoice Sample'), + ), + body: Center( + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + mainAxisAlignment: MainAxisAlignment.center, + children: [ + Text('Choose an item', style: textTheme.labelLarge), + const SizedBox(height: 10.0), + Wrap( + spacing: 5.0, + children: List.generate( + 3, + (int index) { + return ChoiceChip( + label: Text('Item $index'), + selected: _value == index, + onSelected: (bool selected) { + setState(() { + _value = selected ? index : null; + }); + }, + ); + }, + ).toList(), + ), + ], + ), + ), + ); + } +} diff --git a/examples/api/lib/material/filter_chip/filter_chip.0.dart b/examples/api/lib/material/filter_chip/filter_chip.0.dart new file mode 100644 index 0000000000..622a351dec --- /dev/null +++ b/examples/api/lib/material/filter_chip/filter_chip.0.dart @@ -0,0 +1,79 @@ +// 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. + +// Flutter code sample FilterChip + +import 'package:flutter/material.dart'; + +enum ExerciseFilter { walking, running, cycling, hiking } + +void main() => runApp(const ChipApp()); + +class ChipApp extends StatelessWidget { + const ChipApp({super.key}); + + @override + Widget build(BuildContext context) { + return MaterialApp( + theme: ThemeData(colorSchemeSeed: const Color(0xff6750a4), useMaterial3: true), + home: const FilterChipExample(), + ); + } +} + +class FilterChipExample extends StatefulWidget { + const FilterChipExample({super.key}); + + @override + State createState() => _FilterChipExampleState(); +} + +class _FilterChipExampleState extends State { + bool favorite = false; + final List _filters = []; + + @override + Widget build(BuildContext context) { + final TextTheme textTheme = Theme.of(context).textTheme; + + return Scaffold( + appBar: AppBar( + title: const Text('FilterChip Sample'), + ), + body: Center( + child: Column( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + Text('Choose an execise', style: textTheme.labelLarge), + const SizedBox(height: 5.0), + Wrap( + spacing: 5.0, + children: ExerciseFilter.values.map((ExerciseFilter exercise) { + return FilterChip( + label: Text(exercise.name), + selected:_filters.contains(exercise.name), + onSelected: (bool value) { + setState(() { + if (value) { + if (!_filters.contains(exercise.name)) { + _filters.add(exercise.name); + } + } else { + _filters.removeWhere((String name) { + return name == exercise.name; + }); + } + }); + }, + ); + }).toList(), + ), + const SizedBox(height: 10.0), + Text('Looking for: ${_filters.join(', ')}') + ], + ), + ), + ); + } +} diff --git a/examples/api/lib/material/input_chip/input_chip.0.dart b/examples/api/lib/material/input_chip/input_chip.0.dart new file mode 100644 index 0000000000..68e7c6c07b --- /dev/null +++ b/examples/api/lib/material/input_chip/input_chip.0.dart @@ -0,0 +1,86 @@ +// 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. + +// Flutter code sample InputChip + +import 'package:flutter/material.dart'; + +void main() => runApp(const ChipApp()); + +class ChipApp extends StatelessWidget { + const ChipApp({super.key}); + + @override + Widget build(BuildContext context) { + return MaterialApp( + theme: ThemeData(colorSchemeSeed: const Color(0xff6750a4), useMaterial3: true), + home: const InputChipExample(), + ); + } +} + +class InputChipExample extends StatefulWidget { + const InputChipExample({super.key}); + + @override + State createState() => _InputChipExampleState(); +} + +class _InputChipExampleState extends State { + int inputs = 3; + int? selectedIndex; + + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar( + title: const Text('InputChip Sample'), + ), + body: Center( + child: Column( + mainAxisSize: MainAxisSize.min, + mainAxisAlignment: MainAxisAlignment.center, + children: [ + Wrap( + alignment: WrapAlignment.center, + spacing: 5.0, + children: List.generate( + inputs, + (int index) { + return InputChip( + label: Text('Person ${index + 1}'), + selected: selectedIndex == index, + onSelected: (bool selected) { + setState(() { + if (selectedIndex == index) { + selectedIndex = null; + } else { + selectedIndex = index; + } + }); + }, + onDeleted: () { + setState(() { + inputs = inputs - 1; + }); + }, + ); + }, + ).toList(), + ), + const SizedBox(height: 10), + ElevatedButton( + onPressed: () { + setState(() { + inputs = 3; + }); + }, + child: const Text('Reset'), + ) + ], + ), + ), + ); + } +} diff --git a/examples/api/test/material/action_chip/action_chip.0_test.dart b/examples/api/test/material/action_chip/action_chip.0_test.dart new file mode 100644 index 0000000000..939db75ec4 --- /dev/null +++ b/examples/api/test/material/action_chip/action_chip.0_test.dart @@ -0,0 +1,22 @@ +// 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/action_chip/action_chip.0.dart' as example; +import 'package:flutter_test/flutter_test.dart'; + +void main() { + testWidgets('ActionChip updates avatar when tapped', (WidgetTester tester) async { + await tester.pumpWidget( + const example.ChipApp(), + ); + + expect(find.byIcon(Icons.favorite_border), findsOneWidget); + + await tester.tap(find.byType(ActionChip)); + await tester.pumpAndSettle(); + + expect(find.byIcon(Icons.favorite), findsOneWidget); + }); +} diff --git a/examples/api/test/material/choice_chip/choice_chip.0_test.dart b/examples/api/test/material/choice_chip/choice_chip.0_test.dart new file mode 100644 index 0000000000..b44c985408 --- /dev/null +++ b/examples/api/test/material/choice_chip/choice_chip.0_test.dart @@ -0,0 +1,24 @@ +// 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/choice_chip/choice_chip.0.dart' as example; +import 'package:flutter_test/flutter_test.dart'; + +void main() { + testWidgets('Can choose an item using ChoiceChip', (WidgetTester tester) async { + await tester.pumpWidget( + const example.ChipApp(), + ); + + ChoiceChip choosenChip = tester.widget(find.byType(ChoiceChip).at(1)); + expect(choosenChip.selected, true); + + await tester.tap(find.byType(ChoiceChip).at(0)); + await tester.pumpAndSettle(); + + choosenChip = tester.widget(find.byType(ChoiceChip).at(0)); + expect(choosenChip.selected, true); + }); +} diff --git a/examples/api/test/material/filter_chip/filter_chip.0_test.dart b/examples/api/test/material/filter_chip/filter_chip.0_test.dart new file mode 100644 index 0000000000..92c2ac216c --- /dev/null +++ b/examples/api/test/material/filter_chip/filter_chip.0_test.dart @@ -0,0 +1,36 @@ +// 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/filter_chip/filter_chip.0.dart' as example; +import 'package:flutter_test/flutter_test.dart'; + +void main() { + testWidgets('Filter exercises using FilterChip', (WidgetTester tester) async { + const String baseText = 'Looking for: '; + + await tester.pumpWidget( + const example.ChipApp(), + ); + + expect(find.text(baseText), findsOneWidget); + + FilterChip filterChip = tester.widget(find.byType(FilterChip).at(2)); + expect(filterChip.selected, false); + + await tester.tap(find.byType(FilterChip).at(2)); + await tester.pumpAndSettle(); + filterChip = tester.widget(find.byType(FilterChip).at(2)); + expect(filterChip.selected, true); + + expect(find.text('${baseText}cycling'), findsOneWidget); + + await tester.tap(find.byType(FilterChip).at(3)); + await tester.pumpAndSettle(); + filterChip = tester.widget(find.byType(FilterChip).at(3)); + expect(filterChip.selected, true); + + expect(find.text('${baseText}cycling, hiking'), findsOneWidget); + }); +} diff --git a/examples/api/test/material/input_chip/input_chip.0_test.dart b/examples/api/test/material/input_chip/input_chip.0_test.dart new file mode 100644 index 0000000000..c8f1ec0dd2 --- /dev/null +++ b/examples/api/test/material/input_chip/input_chip.0_test.dart @@ -0,0 +1,33 @@ +// 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/input_chip/input_chip.0.dart' as example; +import 'package:flutter_test/flutter_test.dart'; + +void main() { + testWidgets('', (WidgetTester tester) async { + await tester.pumpWidget( + const example.ChipApp(), + ); + + expect(find.byType(InputChip), findsNWidgets(3)); + + await tester.tap(find.byIcon(Icons.clear).at(0)); + await tester.pumpAndSettle(); + expect(find.byType(InputChip), findsNWidgets(2)); + + await tester.tap(find.byIcon(Icons.clear).at(0)); + await tester.pumpAndSettle(); + expect(find.byType(InputChip), findsNWidgets(1)); + + await tester.tap(find.byIcon(Icons.clear).at(0)); + await tester.pumpAndSettle(); + expect(find.byType(InputChip), findsNWidgets(0)); + + await tester.tap(find.text('Reset')); + await tester.pumpAndSettle(); + expect(find.byType(InputChip), findsNWidgets(3)); + }); +} diff --git a/packages/flutter/lib/material.dart b/packages/flutter/lib/material.dart index 256d82da9b..5276c6f87c 100644 --- a/packages/flutter/lib/material.dart +++ b/packages/flutter/lib/material.dart @@ -21,6 +21,7 @@ library material; export 'src/material/about.dart'; +export 'src/material/action_chip.dart'; export 'src/material/animated_icons.dart'; export 'src/material/app.dart'; export 'src/material/app_bar.dart'; @@ -49,11 +50,8 @@ export 'src/material/checkbox.dart'; export 'src/material/checkbox_list_tile.dart'; export 'src/material/checkbox_theme.dart'; export 'src/material/chip.dart'; -export 'src/material/chip_action.dart'; -export 'src/material/chip_choice.dart'; -export 'src/material/chip_filter.dart'; -export 'src/material/chip_input.dart'; export 'src/material/chip_theme.dart'; +export 'src/material/choice_chip.dart'; export 'src/material/circle_avatar.dart'; export 'src/material/color_scheme.dart'; export 'src/material/colors.dart'; @@ -82,6 +80,7 @@ export 'src/material/expansion_panel.dart'; export 'src/material/expansion_tile.dart'; export 'src/material/expansion_tile_theme.dart'; export 'src/material/feedback.dart'; +export 'src/material/filter_chip.dart'; export 'src/material/flexible_space_bar.dart'; export 'src/material/floating_action_button.dart'; export 'src/material/floating_action_button_location.dart'; @@ -98,6 +97,7 @@ export 'src/material/ink_sparkle.dart'; export 'src/material/ink_splash.dart'; export 'src/material/ink_well.dart'; export 'src/material/input_border.dart'; +export 'src/material/input_chip.dart'; export 'src/material/input_date_picker_form_field.dart'; export 'src/material/input_decorator.dart'; export 'src/material/list_tile.dart'; diff --git a/packages/flutter/lib/src/material/chip_action.dart b/packages/flutter/lib/src/material/action_chip.dart similarity index 95% rename from packages/flutter/lib/src/material/chip_action.dart rename to packages/flutter/lib/src/material/action_chip.dart index 8207b69049..070119ff69 100644 --- a/packages/flutter/lib/src/material/chip_action.dart +++ b/packages/flutter/lib/src/material/action_chip.dart @@ -30,20 +30,11 @@ import 'theme_data.dart'; /// /// Requires one of its ancestors to be a [Material] widget. /// -/// {@tool snippet} +/// {@tool dartpad} +/// This example shows how to create an [ActionChip] with a leading icon. +/// The icon is updated when the [ActionChip] is pressed. /// -/// ```dart -/// ActionChip( -/// avatar: CircleAvatar( -/// backgroundColor: Colors.grey.shade800, -/// child: const Text('AB'), -/// ), -/// label: const Text('Aaron Burr'), -/// onPressed: () { -/// print('If you stand for nothing, Burr, what’ll you fall for?'); -/// } -/// ) -/// ``` +/// ** See code in examples/api/lib/material/action_chip/action_chip.0.dart ** /// {@end-tool} /// /// ## Material Design 3 diff --git a/packages/flutter/lib/src/material/chip.dart b/packages/flutter/lib/src/material/chip.dart index 9fa6d1cf08..9c12aabe16 100644 --- a/packages/flutter/lib/src/material/chip.dart +++ b/packages/flutter/lib/src/material/chip.dart @@ -1310,9 +1310,6 @@ class _RawChipState extends State with MaterialStateMixin, TickerProvid child: result, ), ); - // if (height != null) { - // result = SizedBox(height: height, child: result); - // } return Semantics( button: widget.tapEnabled, container: true, diff --git a/packages/flutter/lib/src/material/chip_choice.dart b/packages/flutter/lib/src/material/choice_chip.dart similarity index 90% rename from packages/flutter/lib/src/material/chip_choice.dart rename to packages/flutter/lib/src/material/choice_chip.dart index 1f361bf6ae..e9888d1766 100644 --- a/packages/flutter/lib/src/material/chip_choice.dart +++ b/packages/flutter/lib/src/material/choice_chip.dart @@ -19,40 +19,11 @@ import 'theme_data.dart'; /// Requires one of its ancestors to be a [Material] widget. The [selected] and /// [label] arguments must not be null. /// -/// {@tool snippet} +/// {@tool dartpad} +/// This example shows how to create [ChoiceChip]s with [onSelected]. When the +/// user taps, the chip will be selected. /// -/// ```dart -/// class MyThreeOptions extends StatefulWidget { -/// const MyThreeOptions({super.key}); -/// -/// @override -/// State createState() => _MyThreeOptionsState(); -/// } -/// -/// class _MyThreeOptionsState extends State { -/// int? _value = 1; -/// -/// @override -/// Widget build(BuildContext context) { -/// return Wrap( -/// children: List.generate( -/// 3, -/// (int index) { -/// return ChoiceChip( -/// label: Text('Item $index'), -/// selected: _value == index, -/// onSelected: (bool selected) { -/// setState(() { -/// _value = selected ? index : null; -/// }); -/// }, -/// ); -/// }, -/// ).toList(), -/// ); -/// } -/// } -/// ``` +/// ** See code in examples/api/lib/material/choice_chip/choice_chip.0.dart ** /// {@end-tool} /// /// ## Material Design 3 diff --git a/packages/flutter/lib/src/material/chip_filter.dart b/packages/flutter/lib/src/material/filter_chip.dart similarity index 81% rename from packages/flutter/lib/src/material/chip_filter.dart rename to packages/flutter/lib/src/material/filter_chip.dart index 96f9775582..a4e1da1898 100644 --- a/packages/flutter/lib/src/material/chip_filter.dart +++ b/packages/flutter/lib/src/material/filter_chip.dart @@ -21,69 +21,10 @@ import 'theme_data.dart'; /// /// Requires one of its ancestors to be a [Material] widget. /// -/// {@tool snippet} +/// {@tool dartpad} +/// This example shows how to use [FilterChip]s to filter through exercises. /// -/// ```dart -/// class ActorFilterEntry { -/// const ActorFilterEntry(this.name, this.initials); -/// final String name; -/// final String initials; -/// } -/// -/// class CastFilter extends StatefulWidget { -/// const CastFilter({super.key}); -/// -/// @override -/// State createState() => CastFilterState(); -/// } -/// -/// class CastFilterState extends State { -/// final List _cast = [ -/// const ActorFilterEntry('Aaron Burr', 'AB'), -/// const ActorFilterEntry('Alexander Hamilton', 'AH'), -/// const ActorFilterEntry('Eliza Hamilton', 'EH'), -/// const ActorFilterEntry('James Madison', 'JM'), -/// ]; -/// final List _filters = []; -/// -/// Iterable get actorWidgets { -/// return _cast.map((ActorFilterEntry actor) { -/// return Padding( -/// padding: const EdgeInsets.all(4.0), -/// child: FilterChip( -/// avatar: CircleAvatar(child: Text(actor.initials)), -/// label: Text(actor.name), -/// selected: _filters.contains(actor.name), -/// onSelected: (bool value) { -/// setState(() { -/// if (value) { -/// _filters.add(actor.name); -/// } else { -/// _filters.removeWhere((String name) { -/// return name == actor.name; -/// }); -/// } -/// }); -/// }, -/// ), -/// ); -/// }); -/// } -/// -/// @override -/// Widget build(BuildContext context) { -/// return Column( -/// mainAxisAlignment: MainAxisAlignment.center, -/// children: [ -/// Wrap( -/// children: actorWidgets.toList(), -/// ), -/// Text('Look for: ${_filters.join(', ')}'), -/// ], -/// ); -/// } -/// } -/// ``` +/// ** See code in examples/api/lib/material/filter_chip/filter_chip.0.dart ** /// {@end-tool} /// /// ## Material Design 3 diff --git a/packages/flutter/lib/src/material/chip_input.dart b/packages/flutter/lib/src/material/input_chip.dart similarity index 96% rename from packages/flutter/lib/src/material/chip_input.dart rename to packages/flutter/lib/src/material/input_chip.dart index 0d09ace91f..d47ee17bcd 100644 --- a/packages/flutter/lib/src/material/chip_input.dart +++ b/packages/flutter/lib/src/material/input_chip.dart @@ -30,20 +30,12 @@ import 'theme_data.dart'; /// * In a horizontally scrollable list, like a [ListView] whose /// scrollDirection is [Axis.horizontal]. /// -/// {@tool snippet} +/// {@tool dartpad} +/// This example shows how to create [InputChip]s with [onSelected] and +/// [onDeleted] callbacks. When the user taps the chip, the chip will be selected. +/// When the user taps the delete icon, the chip will be deleted. /// -/// ```dart -/// InputChip( -/// avatar: CircleAvatar( -/// backgroundColor: Colors.grey.shade800, -/// child: const Text('AB'), -/// ), -/// label: const Text('Aaron Burr'), -/// onPressed: () { -/// print('I am the one thing in life.'); -/// } -/// ) -/// ``` +/// ** See code in examples/api/lib/material/input_chip/input_chip.0.dart ** /// {@end-tool} /// /// ## Material Design 3