Update Chips examples and rename files (#108538)

This commit is contained in:
Taha Tesser
2022-07-29 10:17:06 +03:00
committed by GitHub
parent 8da73f7229
commit 76df7368b5
14 changed files with 422 additions and 128 deletions

View 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;
});
},
),
),
);
}
}

View 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(),
),
],
),
),
);
}
}

View 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(', ')}')
],
),
),
);
}
}

View 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'),
)
],
),
),
);
}
}

View File

@@ -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);
});
}

View File

@@ -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);
});
}

View File

@@ -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);
});
}

View 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));
});
}