Update date picker examples, remove unused variables and add missing tests (#121528)
Update date picker examples, remove unused variables and add missing tests
This commit is contained in:
@@ -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<MyStatefulWidget> createState() => _MyStatefulWidgetState();
|
||||
State<DatePickerExample> createState() => _DatePickerExampleState();
|
||||
}
|
||||
|
||||
/// RestorationProperty objects can be used because of RestorationMixin.
|
||||
class _MyStatefulWidgetState extends State<MyStatefulWidget>
|
||||
class _DatePickerExampleState extends State<DatePickerExample>
|
||||
with RestorationMixin {
|
||||
// In this example, the restoration ID for the mixin is passed in through
|
||||
// the [StatefulWidget]'s constructor.
|
||||
|
||||
@@ -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<MyStatefulWidget> createState() => _MyStatefulWidgetState();
|
||||
State<DatePickerExample> createState() => _DatePickerExampleState();
|
||||
}
|
||||
|
||||
/// RestorationProperty objects can be used because of RestorationMixin.
|
||||
class _MyStatefulWidgetState extends State<MyStatefulWidget>
|
||||
class _DatePickerExampleState extends State<DatePickerExample>
|
||||
with RestorationMixin {
|
||||
// In this example, the restoration ID for the mixin is passed in through
|
||||
// the [StatefulWidget]'s constructor.
|
||||
|
||||
@@ -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<FilterChipExample> {
|
||||
bool favorite = false;
|
||||
final List<String> _filters = <String>[];
|
||||
Set<ExerciseFilter> filters = <ExerciseFilter>{};
|
||||
|
||||
@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 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: <Widget>[
|
||||
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,
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -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(
|
||||
|
||||
@@ -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(
|
||||
|
||||
@@ -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<SwitchExample> {
|
||||
bool light0 = true;
|
||||
bool light1 = true;
|
||||
bool light2 = true;
|
||||
|
||||
final MaterialStateProperty<Icon?> thumbIcon = MaterialStateProperty.resolveWith<Icon?>((Set<MaterialState> 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<Icon?> thumbIcon = MaterialStateProperty.resolveWith<Icon?>(
|
||||
(Set<MaterialState> states) {
|
||||
if (states.contains(MaterialState.selected)) {
|
||||
return const Icon(Icons.check);
|
||||
}
|
||||
return const Icon(Icons.close);
|
||||
},
|
||||
);
|
||||
|
||||
@override
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
// found in the LICENSE file.
|
||||
|
||||
// Flutter code sample for [Switch].
|
||||
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.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);
|
||||
});
|
||||
}
|
||||
@@ -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);
|
||||
});
|
||||
}
|
||||
32
examples/api/test/material/switch/switch.2_test.dart
Normal file
32
examples/api/test/material/switch/switch.2_test.dart
Normal file
@@ -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<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,
|
||||
);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user