Support theming CupertinoSwitchs (#116510)
* Introduce flag to maximally apply CupertinoTheme * add missing docs * add tests * fix docs * fix test
This commit is contained in:
68
examples/api/lib/material/switch/switch.3.dart
Normal file
68
examples/api/lib/material/switch/switch.3.dart
Normal file
@@ -0,0 +1,68 @@
|
||||
// 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 for [Switch].
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
void main() => runApp(const SwitchApp());
|
||||
|
||||
class SwitchApp extends StatelessWidget {
|
||||
const SwitchApp({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
theme: ThemeData.light(useMaterial3: true).copyWith(
|
||||
// Use the ambient [CupetinoThemeData] to style all widgets which would
|
||||
// otherwise use iOS defaults.
|
||||
cupertinoOverrideTheme: const CupertinoThemeData(applyThemeToAll: true),
|
||||
),
|
||||
home: Scaffold(
|
||||
appBar: AppBar(title: const Text('Switch Sample')),
|
||||
body: const Center(
|
||||
child: SwitchExample(),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class SwitchExample extends StatefulWidget {
|
||||
const SwitchExample({super.key});
|
||||
|
||||
@override
|
||||
State<SwitchExample> createState() => _SwitchExampleState();
|
||||
}
|
||||
|
||||
class _SwitchExampleState extends State<SwitchExample> {
|
||||
bool light = true;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: <Widget>[
|
||||
Switch.adaptive(
|
||||
value: light,
|
||||
onChanged: (bool value) {
|
||||
setState(() {
|
||||
light = value;
|
||||
});
|
||||
},
|
||||
),
|
||||
Switch.adaptive(
|
||||
// Don't use the ambient [CupetinoThemeData] to style this switch.
|
||||
applyCupertinoTheme: false,
|
||||
value: light,
|
||||
onChanged: (bool value) {
|
||||
setState(() {
|
||||
light = value;
|
||||
});
|
||||
},
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,7 @@
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_api_samples/material/switch/switch.0.dart' as example;
|
||||
import 'package:flutter_api_samples/material/switch/switch.1.dart' as example;
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
void main() {
|
||||
|
||||
24
examples/api/test/material/switch/switch.3_test.dart
Normal file
24
examples/api/test/material/switch/switch.3_test.dart
Normal 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/switch/switch.3.dart' as example;
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
void main() {
|
||||
testWidgets('Can toggle switch', (WidgetTester tester) async {
|
||||
await tester.pumpWidget(
|
||||
const example.SwitchApp(),
|
||||
);
|
||||
|
||||
final Finder switchFinder = find.byType(Switch).first;
|
||||
Switch materialSwitch = tester.widget<Switch>(switchFinder);
|
||||
expect(materialSwitch.value, true);
|
||||
|
||||
await tester.tap(switchFinder);
|
||||
await tester.pumpAndSettle();
|
||||
materialSwitch = tester.widget<Switch>(switchFinder);
|
||||
expect(materialSwitch.value, false);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user