Skip to content

Instantly share code, notes, and snippets.

@mhmadip
Created August 22, 2021 16:48
Show Gist options
  • Save mhmadip/c40c33c7037b79e3b06ffbc4e19fff66 to your computer and use it in GitHub Desktop.
Save mhmadip/c40c33c7037b79e3b06ffbc4e19fff66 to your computer and use it in GitHub Desktop.
Hello Switch Flutter App
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
void main() {
runApp(MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
appBar: AppBar(
title: Text("Hello Switch"),
),
body: HelloSwitch(),
)));
}
class HelloSwitch extends StatefulWidget {
@override
_HelloSwitchState createState() => _HelloSwitchState();
}
class _HelloSwitchState extends State<HelloSwitch> {
bool isSwitch = false;
@override
Widget build(BuildContext context) {
return Center(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text("Hello!"),
//Testing the actual device platform of the user's device, if its iOs then show iOS switch, otherwise show Android switch
Theme.of(context).platform == TargetPlatform.iOS
? CupertinoSwitch(
value: isSwitch,
onChanged: (toggle) {
setState(() {
isSwitch = toggle;
});
},
)
: Switch(
value: isSwitch,
onChanged: (bool toggle) {
setState(() {
isSwitch = toggle;
});
}),
],
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment