Unverified Commit 76df7368 authored by Taha Tesser's avatar Taha Tesser Committed by GitHub

Update Chips examples and rename files (#108538)

parent 8da73f72
// 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<ActionChipExample> createState() => _ActionChipExampleState();
}
class _ActionChipExampleState extends State<ActionChipExample> {
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;
});
},
),
),
);
}
}
// 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<ActionChoiceExample> createState() => _ActionChoiceExampleState();
}
class _ActionChoiceExampleState extends State<ActionChoiceExample> {
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: <Widget>[
Text('Choose an item', style: textTheme.labelLarge),
const SizedBox(height: 10.0),
Wrap(
spacing: 5.0,
children: List<Widget>.generate(
3,
(int index) {
return ChoiceChip(
label: Text('Item $index'),
selected: _value == index,
onSelected: (bool selected) {
setState(() {
_value = selected ? index : null;
});
},
);
},
).toList(),
),
],
),
),
);
}
}
// 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<FilterChipExample> createState() => _FilterChipExampleState();
}
class _FilterChipExampleState extends State<FilterChipExample> {
bool favorite = false;
final List<String> _filters = <String>[];
@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: <Widget>[
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(', ')}')
],
),
),
);
}
}
// 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<InputChipExample> createState() => _InputChipExampleState();
}
class _InputChipExampleState extends State<InputChipExample> {
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: <Widget>[
Wrap(
alignment: WrapAlignment.center,
spacing: 5.0,
children: List<Widget>.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'),
)
],
),
),
);
}
}
// 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);
});
}
// 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);
});
}
// 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);
});
}
// 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));
});
}
......@@ -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';
......
......@@ -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
......
......@@ -1310,9 +1310,6 @@ class _RawChipState extends State<RawChip> with MaterialStateMixin, TickerProvid
child: result,
),
);
// if (height != null) {
// result = SizedBox(height: height, child: result);
// }
return Semantics(
button: widget.tapEnabled,
container: true,
......
......@@ -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<MyThreeOptions> createState() => _MyThreeOptionsState();
/// }
///
/// class _MyThreeOptionsState extends State<MyThreeOptions> {
/// int? _value = 1;
///
/// @override
/// Widget build(BuildContext context) {
/// return Wrap(
/// children: List<Widget>.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
......
......@@ -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<CastFilter> {
/// final List<ActorFilterEntry> _cast = <ActorFilterEntry>[
/// const ActorFilterEntry('Aaron Burr', 'AB'),
/// const ActorFilterEntry('Alexander Hamilton', 'AH'),
/// const ActorFilterEntry('Eliza Hamilton', 'EH'),
/// const ActorFilterEntry('James Madison', 'JM'),
/// ];
/// final List<String> _filters = <String>[];
///
/// Iterable<Widget> 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: <Widget>[
/// 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
......
......@@ -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
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment