File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ // Copyright 2013 The Flutter Authors
2+ // Use of this source code is governed by a BSD-style license that can be
3+ // found in the LICENSE file.
4+
5+ import 'package:material_ui/material_ui.dart' ;
6+
7+ /// Flutter code sample for [Switch.thumbColor] .
8+
9+ class SwitchExample extends StatelessWidget {
10+ const SwitchExample ({super .key, required this .onChanged});
11+
12+ final ValueChanged <bool >? onChanged;
13+
14+ ValueChanged <bool >? get _nullableOnChanged => onChanged;
15+ bool get _value => true ;
16+
17+ @override
18+ Widget build (BuildContext context) {
19+ return
20+ // #region body
21+ Switch (
22+ value: _value,
23+ // If `onChanged` is null, this switch is disabled.
24+ // If `onChanged` is not null, this switch is enabled.
25+ onChanged: _nullableOnChanged,
26+ thumbColor: WidgetStateProperty .resolveWith <Color >((
27+ Set <WidgetState > states,
28+ ) {
29+ if (states.contains (WidgetState .disabled)) {
30+ return Colors .orange.withValues (alpha: .48 );
31+ }
32+ return Colors .orange;
33+ }),
34+ )
35+ // #endregion body
36+ ;
37+ }
38+ }
Original file line number Diff line number Diff line change 1+ // Copyright 2013 The Flutter Authors
2+ // Use of this source code is governed by a BSD-style license that can be
3+ // found in the LICENSE file.
4+
5+ import 'package:material_ui/material_ui.dart' ;
6+
7+ /// Flutter code sample for [Switch.trackColor] .
8+
9+ class SwitchExample extends StatelessWidget {
10+ const SwitchExample ({super .key, required this .onChanged});
11+
12+ final ValueChanged <bool >? onChanged;
13+
14+ ValueChanged <bool >? get _nullableOnChanged => onChanged;
15+ bool get _value => true ;
16+
17+ @override
18+ Widget build (BuildContext context) {
19+ return
20+ // #region body
21+ Switch (
22+ value: _value,
23+ // If `onChanged` is null, this switch is disabled.
24+ // If `onChanged` is not null, this switch is enabled.
25+ onChanged: _nullableOnChanged,
26+ trackColor: WidgetStateProperty .resolveWith <Color >((
27+ Set <WidgetState > states,
28+ ) {
29+ if (states.contains (WidgetState .disabled)) {
30+ return Colors .orange.withValues (alpha: .48 );
31+ }
32+ return Colors .orange;
33+ }),
34+ )
35+ // #endregion body
36+ ;
37+ }
38+ }
Original file line number Diff line number Diff line change 1+ // Copyright 2013 The Flutter Authors
2+ // Use of this source code is governed by a BSD-style license that can be
3+ // found in the LICENSE file.
4+
5+ import 'package:material_ui/material_ui.dart' ;
6+
7+ /// Flutter code sample for [Switch.trackOutlineColor] .
8+
9+ class SwitchExample extends StatelessWidget {
10+ const SwitchExample ({super .key, required this .onChanged});
11+
12+ final ValueChanged <bool >? onChanged;
13+
14+ ValueChanged <bool >? get _nullableOnChanged => onChanged;
15+ bool get _value => true ;
16+
17+ @override
18+ Widget build (BuildContext context) {
19+ return
20+ // #region body
21+ Switch (
22+ value: _value,
23+ // If `onChanged` is null, this switch is disabled.
24+ // If `onChanged` is not null, this switch is enabled.
25+ onChanged: _nullableOnChanged,
26+ trackOutlineColor: WidgetStateProperty .resolveWith <Color ?>((
27+ Set <WidgetState > states,
28+ ) {
29+ if (states.contains (WidgetState .disabled)) {
30+ return Colors .orange.withValues (alpha: .48 );
31+ }
32+ return null ; // Use the default color.
33+ }),
34+ )
35+ // #endregion body
36+ ;
37+ }
38+ }
Original file line number Diff line number Diff line change 1+ // Copyright 2013 The Flutter Authors
2+ // Use of this source code is governed by a BSD-style license that can be
3+ // found in the LICENSE file.
4+
5+ import 'package:material_ui/material_ui.dart' ;
6+
7+ /// Flutter code sample for [Switch.trackOutlineWidth] .
8+
9+ class SwitchExample extends StatelessWidget {
10+ const SwitchExample ({super .key, required this .onChanged});
11+
12+ final ValueChanged <bool >? onChanged;
13+
14+ ValueChanged <bool >? get _nullableOnChanged => onChanged;
15+ bool get _value => true ;
16+
17+ @override
18+ Widget build (BuildContext context) {
19+ return
20+ // #region body
21+ Switch (
22+ value: _value,
23+ // If `onChanged` is null, this switch is disabled.
24+ // If `onChanged` is not null, this switch is enabled.
25+ onChanged: _nullableOnChanged,
26+ trackOutlineWidth: WidgetStateProperty .resolveWith <double ?>((
27+ Set <WidgetState > states,
28+ ) {
29+ if (states.contains (WidgetState .disabled)) {
30+ return 5.0 ;
31+ }
32+ return null ; // Use the default width.
33+ }),
34+ )
35+ // #endregion body
36+ ;
37+ }
38+ }
Original file line number Diff line number Diff line change 1+ // Copyright 2013 The Flutter Authors
2+ // Use of this source code is governed by a BSD-style license that can be
3+ // found in the LICENSE file.
4+
5+ import 'package:material_ui/material_ui.dart' ;
6+
7+ /// Flutter code sample for [Switch.thumbIcon] .
8+
9+ class SwitchExample extends StatelessWidget {
10+ const SwitchExample ({super .key, required this .onChanged});
11+
12+ final ValueChanged <bool >? onChanged;
13+
14+ ValueChanged <bool >? get _nullableOnChanged => onChanged;
15+ bool get _value => true ;
16+
17+ @override
18+ Widget build (BuildContext context) {
19+ return
20+ // #region body
21+ Switch (
22+ value: _value,
23+ // If `onChanged` is null, this switch is disabled.
24+ // If `onChanged` is not null, this switch is enabled.
25+ onChanged: _nullableOnChanged,
26+ thumbIcon: WidgetStateProperty .resolveWith <Icon ?>((
27+ Set <WidgetState > states,
28+ ) {
29+ if (states.contains (WidgetState .disabled)) {
30+ return const Icon (Icons .close);
31+ }
32+ return null ; // All other states will use the default thumbIcon.
33+ }),
34+ )
35+ // #endregion body
36+ ;
37+ }
38+ }
Original file line number Diff line number Diff line change 1+ // Copyright 2013 The Flutter Authors
2+ // Use of this source code is governed by a BSD-style license that can be
3+ // found in the LICENSE file.
4+
5+ import 'package:flutter_test/flutter_test.dart' ;
6+ import 'package:material_ui/material_ui.dart' ;
7+ import 'package:material_ui_examples/switch/switch.snippet.0.dart' as example;
8+
9+ void main () {
10+ testWidgets ('Switch thumbColor resolves based on enabled state' , (
11+ WidgetTester tester,
12+ ) async {
13+ Widget buildApp ({required ValueChanged <bool >? onChanged}) {
14+ return MaterialApp (
15+ home: Scaffold (
16+ body: Center (child: example.SwitchExample (onChanged: onChanged)),
17+ ),
18+ );
19+ }
20+
21+ await tester.pumpWidget (buildApp (onChanged: (bool _) {}));
22+ await tester.pumpAndSettle ();
23+ expect (
24+ find.byType (Switch ),
25+ paints
26+ ..rrect ()
27+ ..rrect ()
28+ ..rrect (color: Colors .orange),
29+ );
30+
31+ await tester.pumpWidget (buildApp (onChanged: null ));
32+ await tester.pumpAndSettle ();
33+ expect (
34+ find.byType (Switch ),
35+ paints
36+ ..rrect ()
37+ ..rrect ()
38+ ..rrect (
39+ color: Color .alphaBlend (
40+ Colors .orange.withValues (alpha: .48 ),
41+ ThemeData ().colorScheme.surface,
42+ ),
43+ ),
44+ );
45+ });
46+ }
Original file line number Diff line number Diff line change 1+ // Copyright 2013 The Flutter Authors
2+ // Use of this source code is governed by a BSD-style license that can be
3+ // found in the LICENSE file.
4+
5+ import 'package:flutter_test/flutter_test.dart' ;
6+ import 'package:material_ui/material_ui.dart' ;
7+ import 'package:material_ui_examples/switch/switch.snippet.1.dart' as example;
8+
9+ void main () {
10+ testWidgets ('Switch trackColor resolves based on enabled state' , (
11+ WidgetTester tester,
12+ ) async {
13+ Widget buildApp ({required ValueChanged <bool >? onChanged}) {
14+ return MaterialApp (
15+ home: Scaffold (
16+ body: Center (child: example.SwitchExample (onChanged: onChanged)),
17+ ),
18+ );
19+ }
20+
21+ await tester.pumpWidget (buildApp (onChanged: (bool _) {}));
22+ await tester.pumpAndSettle ();
23+ expect (find.byType (Switch ), paints..rrect (color: Colors .orange));
24+
25+ await tester.pumpWidget (buildApp (onChanged: null ));
26+ await tester.pumpAndSettle ();
27+ expect (
28+ find.byType (Switch ),
29+ paints..rrect (color: Colors .orange.withValues (alpha: .48 )),
30+ );
31+ });
32+ }
Original file line number Diff line number Diff line change 1+ // Copyright 2013 The Flutter Authors
2+ // Use of this source code is governed by a BSD-style license that can be
3+ // found in the LICENSE file.
4+
5+ import 'package:flutter_test/flutter_test.dart' ;
6+ import 'package:material_ui/material_ui.dart' ;
7+ import 'package:material_ui_examples/switch/switch.snippet.2.dart' as example;
8+
9+ void main () {
10+ testWidgets ('Switch trackOutlineColor resolves based on enabled state' , (
11+ WidgetTester tester,
12+ ) async {
13+ Widget buildApp ({required ValueChanged <bool >? onChanged}) {
14+ return MaterialApp (
15+ home: Scaffold (
16+ body: Center (child: example.SwitchExample (onChanged: onChanged)),
17+ ),
18+ );
19+ }
20+
21+ await tester.pumpWidget (buildApp (onChanged: (bool _) {}));
22+ await tester.pumpAndSettle ();
23+ expect (
24+ find.byType (Switch ),
25+ paints
26+ ..rrect ()
27+ ..rrect (color: Colors .transparent),
28+ );
29+
30+ await tester.pumpWidget (buildApp (onChanged: null ));
31+ await tester.pumpAndSettle ();
32+ expect (
33+ find.byType (Switch ),
34+ paints
35+ ..rrect ()
36+ ..rrect (color: Colors .orange.withValues (alpha: .48 )),
37+ );
38+ });
39+ }
Original file line number Diff line number Diff line change 1+ // Copyright 2013 The Flutter Authors
2+ // Use of this source code is governed by a BSD-style license that can be
3+ // found in the LICENSE file.
4+
5+ import 'package:flutter_test/flutter_test.dart' ;
6+ import 'package:material_ui/material_ui.dart' ;
7+ import 'package:material_ui_examples/switch/switch.snippet.3.dart' as example;
8+
9+ void main () {
10+ testWidgets ('Switch trackOutlineWidth resolves based on enabled state' , (
11+ WidgetTester tester,
12+ ) async {
13+ Widget buildApp ({required ValueChanged <bool >? onChanged}) {
14+ return MaterialApp (
15+ home: Scaffold (
16+ body: Center (child: example.SwitchExample (onChanged: onChanged)),
17+ ),
18+ );
19+ }
20+
21+ await tester.pumpWidget (buildApp (onChanged: (bool _) {}));
22+ await tester.pumpAndSettle ();
23+ expect (
24+ find.byType (Switch ),
25+ paints
26+ ..rrect ()
27+ ..rrect (strokeWidth: 2.0 ),
28+ );
29+
30+ await tester.pumpWidget (buildApp (onChanged: null ));
31+ await tester.pumpAndSettle ();
32+ expect (
33+ find.byType (Switch ),
34+ paints
35+ ..rrect ()
36+ ..rrect (strokeWidth: 5.0 ),
37+ );
38+ });
39+ }
Original file line number Diff line number Diff line change 1+ // Copyright 2013 The Flutter Authors
2+ // Use of this source code is governed by a BSD-style license that can be
3+ // found in the LICENSE file.
4+
5+ import 'package:flutter_test/flutter_test.dart' ;
6+ import 'package:material_ui/material_ui.dart' ;
7+ import 'package:material_ui_examples/switch/switch.snippet.4.dart' as example;
8+
9+ void main () {
10+ testWidgets ('Switch thumbIcon resolves based on enabled state' , (
11+ WidgetTester tester,
12+ ) async {
13+ Widget buildApp ({required ValueChanged <bool >? onChanged}) {
14+ return MaterialApp (
15+ home: Scaffold (
16+ body: Center (child: example.SwitchExample (onChanged: onChanged)),
17+ ),
18+ );
19+ }
20+
21+ await tester.pumpWidget (buildApp (onChanged: (bool _) {}));
22+ await tester.pumpAndSettle ();
23+ expect (find.byType (Switch ), isNot (paints..paragraph ()));
24+
25+ await tester.pumpWidget (buildApp (onChanged: null ));
26+ await tester.pumpAndSettle ();
27+ expect (find.byType (Switch ), paints..paragraph ());
28+ });
29+ }
You can’t perform that action at this time.
0 commit comments