Unverified Commit 87679ff1 authored by Taha Tesser's avatar Taha Tesser Committed by GitHub

Update date picker examples, remove unused variables and add missing tests (#121528)

Update date picker examples, remove unused variables and add missing tests
parent e5a382ec
...@@ -6,34 +6,32 @@ ...@@ -6,34 +6,32 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
void main() => runApp(const MyApp()); void main() => runApp(const DatePickerApp());
class MyApp extends StatelessWidget { class DatePickerApp extends StatelessWidget {
const MyApp({super.key}); const DatePickerApp({super.key});
static const String _title = 'Flutter Code Sample';
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return const MaterialApp( return MaterialApp(
theme: ThemeData(useMaterial3: true),
restorationScopeId: 'app', restorationScopeId: 'app',
title: _title, home: const DatePickerExample(restorationId: 'main'),
home: MyStatefulWidget(restorationId: 'main'),
); );
} }
} }
class MyStatefulWidget extends StatefulWidget { class DatePickerExample extends StatefulWidget {
const MyStatefulWidget({super.key, this.restorationId}); const DatePickerExample({super.key, this.restorationId});
final String? restorationId; final String? restorationId;
@override @override
State<MyStatefulWidget> createState() => _MyStatefulWidgetState(); State<DatePickerExample> createState() => _DatePickerExampleState();
} }
/// RestorationProperty objects can be used because of RestorationMixin. /// RestorationProperty objects can be used because of RestorationMixin.
class _MyStatefulWidgetState extends State<MyStatefulWidget> class _DatePickerExampleState extends State<DatePickerExample>
with RestorationMixin { with RestorationMixin {
// In this example, the restoration ID for the mixin is passed in through // In this example, the restoration ID for the mixin is passed in through
// the [StatefulWidget]'s constructor. // the [StatefulWidget]'s constructor.
......
...@@ -6,34 +6,33 @@ ...@@ -6,34 +6,33 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
void main() => runApp(const MyApp()); void main() => runApp(const DatePickerApp());
class MyApp extends StatelessWidget { class DatePickerApp extends StatelessWidget {
const MyApp({super.key}); const DatePickerApp({super.key});
static const String _title = 'Flutter Code Sample';
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return const MaterialApp( return MaterialApp(
theme: ThemeData(useMaterial3: true),
restorationScopeId: 'app', restorationScopeId: 'app',
title: _title, home: const DatePickerExample(restorationId: 'main'),
home: MyStatefulWidget(restorationId: 'main'),
); );
} }
} }
class MyStatefulWidget extends StatefulWidget {
const MyStatefulWidget({super.key, this.restorationId}); class DatePickerExample extends StatefulWidget {
const DatePickerExample({super.key, this.restorationId});
final String? restorationId; final String? restorationId;
@override @override
State<MyStatefulWidget> createState() => _MyStatefulWidgetState(); State<DatePickerExample> createState() => _DatePickerExampleState();
} }
/// RestorationProperty objects can be used because of RestorationMixin. /// RestorationProperty objects can be used because of RestorationMixin.
class _MyStatefulWidgetState extends State<MyStatefulWidget> class _DatePickerExampleState extends State<DatePickerExample>
with RestorationMixin { with RestorationMixin {
// In this example, the restoration ID for the mixin is passed in through // In this example, the restoration ID for the mixin is passed in through
// the [StatefulWidget]'s constructor. // the [StatefulWidget]'s constructor.
......
...@@ -16,8 +16,13 @@ class ChipApp extends StatelessWidget { ...@@ -16,8 +16,13 @@ class ChipApp extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return MaterialApp( return MaterialApp(
theme: ThemeData(colorSchemeSeed: const Color(0xff6750a4), useMaterial3: true), theme: ThemeData(useMaterial3: true),
home: const FilterChipExample(), home: Scaffold(
appBar: AppBar(
title: const Text('FilterChip Sample'),
),
body: const FilterChipExample(),
),
); );
} }
} }
...@@ -30,18 +35,13 @@ class FilterChipExample extends StatefulWidget { ...@@ -30,18 +35,13 @@ class FilterChipExample extends StatefulWidget {
} }
class _FilterChipExampleState extends State<FilterChipExample> { class _FilterChipExampleState extends State<FilterChipExample> {
bool favorite = false; Set<ExerciseFilter> filters = <ExerciseFilter>{};
final List<String> _filters = <String>[];
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final TextTheme textTheme = Theme.of(context).textTheme; final TextTheme textTheme = Theme.of(context).textTheme;
return Scaffold( return Center(
appBar: AppBar(
title: const Text('FilterChip Sample'),
),
body: Center(
child: Column( child: Column(
mainAxisAlignment: MainAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[ children: <Widget>[
...@@ -52,17 +52,13 @@ class _FilterChipExampleState extends State<FilterChipExample> { ...@@ -52,17 +52,13 @@ class _FilterChipExampleState extends State<FilterChipExample> {
children: ExerciseFilter.values.map((ExerciseFilter exercise) { children: ExerciseFilter.values.map((ExerciseFilter exercise) {
return FilterChip( return FilterChip(
label: Text(exercise.name), label: Text(exercise.name),
selected:_filters.contains(exercise.name), selected: filters.contains(exercise),
onSelected: (bool value) { onSelected: (bool selected) {
setState(() { setState(() {
if (value) { if (selected) {
if (!_filters.contains(exercise.name)) { filters.add(exercise);
_filters.add(exercise.name);
}
} else { } else {
_filters.removeWhere((String name) { filters.remove(exercise);
return name == exercise.name;
});
} }
}); });
}, },
...@@ -70,9 +66,12 @@ class _FilterChipExampleState extends State<FilterChipExample> { ...@@ -70,9 +66,12 @@ class _FilterChipExampleState extends State<FilterChipExample> {
}).toList(), }).toList(),
), ),
const SizedBox(height: 10.0), const SizedBox(height: 10.0),
Text('Looking for: ${_filters.join(', ')}') Text(
], 'Looking for: ${filters.map(
(ExerciseFilter e) => e.name).join(', ')}',
style: textTheme.labelLarge,
), ),
],
), ),
); );
} }
......
...@@ -14,6 +14,7 @@ class SwitchApp extends StatelessWidget { ...@@ -14,6 +14,7 @@ class SwitchApp extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return MaterialApp( return MaterialApp(
theme: ThemeData(useMaterial3: true),
home: Scaffold( home: Scaffold(
appBar: AppBar(title: const Text('Switch Sample')), appBar: AppBar(title: const Text('Switch Sample')),
body: const Center( body: const Center(
......
...@@ -14,6 +14,7 @@ class SwitchApp extends StatelessWidget { ...@@ -14,6 +14,7 @@ class SwitchApp extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return MaterialApp( return MaterialApp(
theme: ThemeData(useMaterial3: true),
home: Scaffold( home: Scaffold(
appBar: AppBar(title: const Text('Switch Sample')), appBar: AppBar(title: const Text('Switch Sample')),
body: const Center( body: const Center(
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
// Flutter code sample for Switch // Flutter code sample for [Switch].
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
...@@ -14,7 +14,7 @@ class SwitchApp extends StatelessWidget { ...@@ -14,7 +14,7 @@ class SwitchApp extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return MaterialApp( return MaterialApp(
theme: ThemeData(useMaterial3: true, colorSchemeSeed: const Color(0xff6750a4)), theme: ThemeData(useMaterial3: true),
home: Scaffold( home: Scaffold(
appBar: AppBar(title: const Text('Switch Sample')), appBar: AppBar(title: const Text('Switch Sample')),
body: const Center( body: const Center(
...@@ -35,10 +35,9 @@ class SwitchExample extends StatefulWidget { ...@@ -35,10 +35,9 @@ class SwitchExample extends StatefulWidget {
class _SwitchExampleState extends State<SwitchExample> { class _SwitchExampleState extends State<SwitchExample> {
bool light0 = true; bool light0 = true;
bool light1 = true; bool light1 = true;
bool light2 = true;
final MaterialStateProperty<Icon?> thumbIcon = MaterialStateProperty.resolveWith<Icon?>((Set<MaterialState> states) { final MaterialStateProperty<Icon?> thumbIcon = MaterialStateProperty.resolveWith<Icon?>(
// Thumb icon when the switch is selected. (Set<MaterialState> states) {
if (states.contains(MaterialState.selected)) { if (states.contains(MaterialState.selected)) {
return const Icon(Icons.check); return const Icon(Icons.check);
} }
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
// found in the LICENSE file. // found in the LICENSE file.
// Flutter code sample for [Switch]. // Flutter code sample for [Switch].
import 'package:flutter/cupertino.dart'; import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
......
// 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/date_picker/show_date_picker.0.dart' as example;
import 'package:flutter_test/flutter_test.dart';
void main() {
testWidgets('Can show date picker', (WidgetTester tester) async {
const String datePickerTitle = 'Select date';
const String initialDate = 'Sun, Jul 25';
await tester.pumpWidget(
const example.DatePickerApp(),
);
// The date picker is not shown initially.
expect(find.text(datePickerTitle), findsNothing);
expect(find.text(initialDate), findsNothing);
// Tap the button to show the date picker.
await tester.tap(find.byType(OutlinedButton));
await tester.pumpAndSettle();
// The initial date is shown.
expect(find.text(datePickerTitle), findsOneWidget);
expect(find.text(initialDate), findsOneWidget);
// Tap another date to select it.
await tester.tap(find.text('30'));
await tester.pumpAndSettle();
// The selected date is shown.
expect(find.text(datePickerTitle), findsOneWidget);
expect(find.text('Fri, Jul 30'), findsOneWidget);
// Tap OK to confirm the selection and close the date picker.
await tester.tap(find.text('OK'));
await tester.pumpAndSettle();
// The date picker is closed and the selected date is shown.
expect(find.text(datePickerTitle), findsNothing);
expect(find.text('Selected: 30/7/2021'), 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/date_picker/show_date_range_picker.0.dart' as example;
import 'package:flutter_test/flutter_test.dart';
void main() {
testWidgets('Can show date range picker', (WidgetTester tester) async {
const String datePickerTitle = 'Select range';
await tester.pumpWidget(
const example.DatePickerApp(),
);
// The date range picker is not shown initially.
expect(find.text(datePickerTitle), findsNothing);
expect(find.text('Jan 1'), findsNothing);
expect(find.text('Jan 5, 2021'), findsNothing);
// Tap the button to show the date range picker.
await tester.tap(find.byType(OutlinedButton));
await tester.pumpAndSettle();
// The date range picker shows initial date range.
expect(find.text(datePickerTitle), findsOneWidget);
expect(find.text('Jan 1'), findsOneWidget);
expect(find.text('Jan 5, 2021'), findsOneWidget);
// Tap to select new date range.
await tester.tap(find.text('18').first);
await tester.pumpAndSettle();
await tester.tap(find.text('22').first);
await tester.pumpAndSettle();
// The selected date range is shown.
expect(find.text(datePickerTitle), findsOneWidget);
expect(find.text('Jan 18'), findsOneWidget);
expect(find.text('Jan 22, 2021'), findsOneWidget);
// Tap Save to confirm the selection and close the date range picker.
await tester.tap(find.text('Save'));
await tester.pumpAndSettle();
// The date range picker is closed.
expect(find.text(datePickerTitle), findsNothing);
expect(find.text('Jan 18'), findsNothing);
expect(find.text('Jan 22, 2021'), findsNothing);
});
}
// 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/switch/switch.2.dart' as example;
import 'package:flutter_test/flutter_test.dart';
void main() {
testWidgets('Switch thumb icon supports material states', (WidgetTester tester) async {
const Set<MaterialState> selected = <MaterialState>{ MaterialState.selected };
const Set<MaterialState> unselected = <MaterialState>{};
await tester.pumpWidget(
const example.SwitchApp(),
);
Switch materialSwitch = tester.widget<Switch>(find.byType(Switch).first);
expect(materialSwitch.thumbIcon, null);
materialSwitch = tester.widget<Switch>(find.byType(Switch).last);
expect(materialSwitch.thumbIcon, isNotNull);
expect(
materialSwitch.thumbIcon!.resolve(selected)!.icon,
Icons.check,
);
expect(
materialSwitch.thumbIcon!.resolve(unselected)!.icon,
Icons.close,
);
});
}
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