From 87679ff1c3ba493b5d05e12ff5d2d851e40dbb88 Mon Sep 17 00:00:00 2001 From: Taha Tesser Date: Tue, 28 Feb 2023 10:31:51 +0200 Subject: [PATCH] Update date picker examples, remove unused variables and add missing tests (#121528) Update date picker examples, remove unused variables and add missing tests --- .../date_picker/show_date_picker.0.dart | 22 +++--- .../date_picker/show_date_range_picker.0.dart | 23 +++--- .../material/filter_chip/filter_chip.0.dart | 79 +++++++++---------- .../api/lib/material/switch/switch.0.dart | 1 + .../api/lib/material/switch/switch.1.dart | 1 + .../api/lib/material/switch/switch.2.dart | 19 +++-- .../api/lib/material/switch/switch.3.dart | 1 + .../date_picker/show_date_picker.0_test.dart | 46 +++++++++++ .../show_date_range_picker.0_test.dart | 51 ++++++++++++ .../test/material/switch/switch.2_test.dart | 32 ++++++++ 10 files changed, 201 insertions(+), 74 deletions(-) create mode 100644 examples/api/test/material/date_picker/show_date_picker.0_test.dart create mode 100644 examples/api/test/material/date_picker/show_date_range_picker.0_test.dart create mode 100644 examples/api/test/material/switch/switch.2_test.dart diff --git a/examples/api/lib/material/date_picker/show_date_picker.0.dart b/examples/api/lib/material/date_picker/show_date_picker.0.dart index ab2cdc305b..79b658ca09 100644 --- a/examples/api/lib/material/date_picker/show_date_picker.0.dart +++ b/examples/api/lib/material/date_picker/show_date_picker.0.dart @@ -6,34 +6,32 @@ import 'package:flutter/material.dart'; -void main() => runApp(const MyApp()); +void main() => runApp(const DatePickerApp()); -class MyApp extends StatelessWidget { - const MyApp({super.key}); - - static const String _title = 'Flutter Code Sample'; +class DatePickerApp extends StatelessWidget { + const DatePickerApp({super.key}); @override Widget build(BuildContext context) { - return const MaterialApp( + return MaterialApp( + theme: ThemeData(useMaterial3: true), restorationScopeId: 'app', - title: _title, - home: MyStatefulWidget(restorationId: 'main'), + home: const DatePickerExample(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; @override - State createState() => _MyStatefulWidgetState(); + State createState() => _DatePickerExampleState(); } /// RestorationProperty objects can be used because of RestorationMixin. -class _MyStatefulWidgetState extends State +class _DatePickerExampleState extends State with RestorationMixin { // In this example, the restoration ID for the mixin is passed in through // the [StatefulWidget]'s constructor. diff --git a/examples/api/lib/material/date_picker/show_date_range_picker.0.dart b/examples/api/lib/material/date_picker/show_date_range_picker.0.dart index ef4b6d4d01..873ce9ad03 100644 --- a/examples/api/lib/material/date_picker/show_date_range_picker.0.dart +++ b/examples/api/lib/material/date_picker/show_date_range_picker.0.dart @@ -6,34 +6,33 @@ import 'package:flutter/material.dart'; -void main() => runApp(const MyApp()); +void main() => runApp(const DatePickerApp()); -class MyApp extends StatelessWidget { - const MyApp({super.key}); - - static const String _title = 'Flutter Code Sample'; +class DatePickerApp extends StatelessWidget { + const DatePickerApp({super.key}); @override Widget build(BuildContext context) { - return const MaterialApp( + return MaterialApp( + theme: ThemeData(useMaterial3: true), restorationScopeId: 'app', - title: _title, - home: MyStatefulWidget(restorationId: 'main'), + home: const DatePickerExample(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; @override - State createState() => _MyStatefulWidgetState(); + State createState() => _DatePickerExampleState(); } /// RestorationProperty objects can be used because of RestorationMixin. -class _MyStatefulWidgetState extends State +class _DatePickerExampleState extends State with RestorationMixin { // In this example, the restoration ID for the mixin is passed in through // the [StatefulWidget]'s constructor. diff --git a/examples/api/lib/material/filter_chip/filter_chip.0.dart b/examples/api/lib/material/filter_chip/filter_chip.0.dart index 734b0dd209..d3da52ff9f 100644 --- a/examples/api/lib/material/filter_chip/filter_chip.0.dart +++ b/examples/api/lib/material/filter_chip/filter_chip.0.dart @@ -16,8 +16,13 @@ class ChipApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( - theme: ThemeData(colorSchemeSeed: const Color(0xff6750a4), useMaterial3: true), - home: const FilterChipExample(), + theme: ThemeData(useMaterial3: true), + home: Scaffold( + appBar: AppBar( + title: const Text('FilterChip Sample'), + ), + body: const FilterChipExample(), + ), ); } } @@ -30,49 +35,43 @@ class FilterChipExample extends StatefulWidget { } class _FilterChipExampleState extends State { - bool favorite = false; - final List _filters = []; + Set 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 exercise', 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(', ')}') - ], - ), + return Center( + child: Column( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + Text('Choose an exercise', 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), + onSelected: (bool selected) { + setState(() { + if (selected) { + filters.add(exercise); + } else { + filters.remove(exercise); + } + }); + }, + ); + }).toList(), + ), + const SizedBox(height: 10.0), + Text( + 'Looking for: ${filters.map( + (ExerciseFilter e) => e.name).join(', ')}', + style: textTheme.labelLarge, + ), + ], ), ); } diff --git a/examples/api/lib/material/switch/switch.0.dart b/examples/api/lib/material/switch/switch.0.dart index 0b8d4c4974..8fe620b66f 100644 --- a/examples/api/lib/material/switch/switch.0.dart +++ b/examples/api/lib/material/switch/switch.0.dart @@ -14,6 +14,7 @@ class SwitchApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( + theme: ThemeData(useMaterial3: true), home: Scaffold( appBar: AppBar(title: const Text('Switch Sample')), body: const Center( diff --git a/examples/api/lib/material/switch/switch.1.dart b/examples/api/lib/material/switch/switch.1.dart index fee110a3a8..59d28f4d0f 100644 --- a/examples/api/lib/material/switch/switch.1.dart +++ b/examples/api/lib/material/switch/switch.1.dart @@ -14,6 +14,7 @@ class SwitchApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( + theme: ThemeData(useMaterial3: true), home: Scaffold( appBar: AppBar(title: const Text('Switch Sample')), body: const Center( diff --git a/examples/api/lib/material/switch/switch.2.dart b/examples/api/lib/material/switch/switch.2.dart index 3d9de18da2..08fbf99623 100644 --- a/examples/api/lib/material/switch/switch.2.dart +++ b/examples/api/lib/material/switch/switch.2.dart @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -// Flutter code sample for Switch +// Flutter code sample for [Switch]. import 'package:flutter/material.dart'; @@ -14,7 +14,7 @@ class SwitchApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( - theme: ThemeData(useMaterial3: true, colorSchemeSeed: const Color(0xff6750a4)), + theme: ThemeData(useMaterial3: true), home: Scaffold( appBar: AppBar(title: const Text('Switch Sample')), body: const Center( @@ -35,15 +35,14 @@ class SwitchExample extends StatefulWidget { class _SwitchExampleState extends State { bool light0 = true; bool light1 = true; - bool light2 = true; - final MaterialStateProperty thumbIcon = MaterialStateProperty.resolveWith((Set states) { - // Thumb icon when the switch is selected. - if (states.contains(MaterialState.selected)) { - return const Icon(Icons.check); - } - return const Icon(Icons.close); - }, + final MaterialStateProperty thumbIcon = MaterialStateProperty.resolveWith( + (Set states) { + if (states.contains(MaterialState.selected)) { + return const Icon(Icons.check); + } + return const Icon(Icons.close); + }, ); @override diff --git a/examples/api/lib/material/switch/switch.3.dart b/examples/api/lib/material/switch/switch.3.dart index 3ed31ea455..3ed57f2397 100644 --- a/examples/api/lib/material/switch/switch.3.dart +++ b/examples/api/lib/material/switch/switch.3.dart @@ -3,6 +3,7 @@ // found in the LICENSE file. // Flutter code sample for [Switch]. + import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; diff --git a/examples/api/test/material/date_picker/show_date_picker.0_test.dart b/examples/api/test/material/date_picker/show_date_picker.0_test.dart new file mode 100644 index 0000000000..732d0d992a --- /dev/null +++ b/examples/api/test/material/date_picker/show_date_picker.0_test.dart @@ -0,0 +1,46 @@ +// 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); + }); +} diff --git a/examples/api/test/material/date_picker/show_date_range_picker.0_test.dart b/examples/api/test/material/date_picker/show_date_range_picker.0_test.dart new file mode 100644 index 0000000000..36059487ab --- /dev/null +++ b/examples/api/test/material/date_picker/show_date_range_picker.0_test.dart @@ -0,0 +1,51 @@ +// 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); + }); +} diff --git a/examples/api/test/material/switch/switch.2_test.dart b/examples/api/test/material/switch/switch.2_test.dart new file mode 100644 index 0000000000..67eaf775b4 --- /dev/null +++ b/examples/api/test/material/switch/switch.2_test.dart @@ -0,0 +1,32 @@ +// 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 selected = { MaterialState.selected }; + const Set unselected = {}; + + await tester.pumpWidget( + const example.SwitchApp(), + ); + + Switch materialSwitch = tester.widget(find.byType(Switch).first); + expect(materialSwitch.thumbIcon, null); + + materialSwitch = tester.widget(find.byType(Switch).last); + expect(materialSwitch.thumbIcon, isNotNull); + expect( + materialSwitch.thumbIcon!.resolve(selected)!.icon, + Icons.check, + ); + expect( + materialSwitch.thumbIcon!.resolve(unselected)!.icon, + Icons.close, + ); + }); +}