Update Chips examples and rename files (#108538)
This commit is contained in:
52
examples/api/lib/material/action_chip/action_chip.0.dart
Normal file
52
examples/api/lib/material/action_chip/action_chip.0.dart
Normal file
@@ -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<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;
|
||||
});
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
70
examples/api/lib/material/choice_chip/choice_chip.0.dart
Normal file
70
examples/api/lib/material/choice_chip/choice_chip.0.dart
Normal file
@@ -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<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(),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
79
examples/api/lib/material/filter_chip/filter_chip.0.dart
Normal file
79
examples/api/lib/material/filter_chip/filter_chip.0.dart
Normal file
@@ -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<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(', ')}')
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
86
examples/api/lib/material/input_chip/input_chip.0.dart
Normal file
86
examples/api/lib/material/input_chip/input_chip.0.dart
Normal file
@@ -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<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'),
|
||||
)
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
});
|
||||
}
|
||||
@@ -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);
|
||||
});
|
||||
}
|
||||
@@ -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);
|
||||
});
|
||||
}
|
||||
33
examples/api/test/material/input_chip/input_chip.0_test.dart
Normal file
33
examples/api/test/material/input_chip/input_chip.0_test.dart
Normal file
@@ -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));
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user