This repository has been archived by the owner on Feb 22, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support theming
CupertinoSwitch
s (#116510)
* Introduce flag to maximally apply CupertinoTheme * add missing docs * add tests * fix docs * fix test
- Loading branch information
Showing
9 changed files
with
228 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
}); | ||
}, | ||
), | ||
], | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.