All types of Switches in Flutter

In this beat tutorial you will get to know the different kinds of beatswitches.

Single switch/hardware

The Switch widget takes the boolean value and the parameter of the onChanged function.

Switch (
Value: Value1, // valeurolic
onChanged: (fall) {
setState(() {
Value1 = fall;
});
},
),

http://31.220.61.170/wp-content/uploads/2020/11/All-types-of-Switches-in-Flutter.png http://31.220.61.170/wp-content/uploads/2020/11/1605766572_925_All-types-of-Switches-in-Flutter.png

Hardware switch

Cupertino switch

The Cupertino switch is exactly the same as a normal (hardware) switch, but looks like an iOS switch.

CupertinoSwitch(
Value: value1,
on Switches : (fall) {
setState(() {
value1 = fall;
});
},
),

If you want to change the appearance of the Switch depending on the platform on which it rotates, you can always use Switch.

http://31.220.61.170/wp-content/uploads/2020/11/1605766572_382_All-types-of-Switches-in-Flutter.png http://31.220.61.170/wp-content/uploads/2020/11/1605766573_379_All-types-of-Switches-in-Flutter.png

Cupertino switch (iOS)

Platform adjustment switch

Platform-Adaptive Switch allows you to create a switch widget whose appearance depends on the platform it’s running on.

Switch.adaptive(
Value: Value1,
onChanged: (fall) {
setState(() {
value1 = fall;
});
},
),

As you can see, it’s the same widget, but it automatically adapts to the platform and displays the Android content switch and the Cupertino switch for iOS.

If you look at the sources, you can even see for yourself how it works:

BuildWidget(BuildContextContext) {
switch (widget._switchType) {
case _SwitchType.material:
returns buildMaterialSwitch(context) ;

register _SwitchType.adaptive: {
final ThemeData theme = Theme.van (context);
claim (theme.plattform != null);
switch (theme.plattform) {
case TargetPlatform.android:
case TargetPlatform.fuchsia:
case TargetPlatform.linux:
case TargetPlatform.windows:
return buildMaterialSwitch(context);
case TargetPlatform.iOS:
case TargetPlatform.macOS:
return buildCupertinoSwitch(context);
}
}
}
assert(false);
returns zero;
}.

  • If the platform is Android, Fuschia, Linux or Windows, it will build the hardware switch,
  • If you are using the iOS or MacOS platform, turn on the Cupertino switch.

List of switches

The best way to use the switch is in the ListTile widget. This gives you enough flexibility to customize the text around it and makes the entire text tile very accessible.

SwitchListTile(
title : Text(‘Switch in list tile’),
subtitles : Text(‘some subtitle’),
value : value3,
onChanged : (fall) {
setState(() {
value3 = fall ;
}) ;
},
),
ListTile(
title : Text (‘Switch in list tile’), Subtitle
: Text (‘single subtitles’),
onChanged: Switch(
value: value2,
onChanged: (fall) {
setState(() {
value2 = fall;
});
},
),
),

SwitchListTile vs. ListTile with Switch as tracking widget.

If you want to change the appearance of the target platform, use SwitchListTile!

http://31.220.61.170/wp-content/uploads/2020/11/1605766573_839_All-types-of-Switches-in-Flutter.png http://31.220.61.170/wp-content/uploads/2020/11/1605766573_12_All-types-of-Switches-in-Flutter.png

The SwitchListle widget on the rise

I hope that beat was good for you. If you are interested in flutter, read the other flutter tutorials on this site. Some of them contain video courses.

Have fun while you learn!

Related Tags:

Leave a Reply