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 @@
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,18 +35,13 @@ 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(
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
......@@ -52,17 +52,13 @@ class _FilterChipExampleState extends State<FilterChipExample> {
children: ExerciseFilter.values.map((ExerciseFilter exercise) {
return FilterChip(
label: Text(exercise.name),
selected:_filters.contains(exercise.name),
onSelected: (bool value) {
selected: filters.contains(exercise),
onSelected: (bool selected) {
setState(() {
if (value) {
if (!_filters.contains(exercise.name)) {
_filters.add(exercise.name);
}
if (selected) {
filters.add(exercise);
} else {
_filters.removeWhere((String name) {
return name == exercise.name;
});
filters.remove(exercise);
}
});
},
......@@ -70,9 +66,12 @@ class _FilterChipExampleState extends State<FilterChipExample> {
}).toList(),
),
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 {
@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,10 +35,9 @@ 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.
final MaterialStateProperty<Icon?> thumbIcon = MaterialStateProperty.resolveWith<Icon?>(
(Set<MaterialState> states) {
if (states.contains(MaterialState.selected)) {
return const Icon(Icons.check);
}
......
......@@ -3,6 +3,7 @@
// found in the LICENSE file.
// Flutter code sample for [Switch].
import 'package:flutter/cupertino.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