Created
August 22, 2021 16:48
-
-
Save mhmadip/c40c33c7037b79e3b06ffbc4e19fff66 to your computer and use it in GitHub Desktop.
Hello Switch Flutter App
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
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