Nessuna descrizione
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

splash_screen.dart 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. import 'dart:async';
  2. import 'dart:convert';
  3. import 'dart:io';
  4. import 'package:flutter/cupertino.dart';
  5. import 'package:flutter/material.dart';
  6. import 'package:flutter/services.dart';
  7. import 'package:fluttertoast/fluttertoast.dart';
  8. import 'package:google_fonts/google_fonts.dart';
  9. import 'package:tower_app/Screens/Login/login_screen.dart';
  10. import 'package:tower_app/Screens/Splash/loginstatus_post.dart';
  11. import 'package:tower_app/constants.dart';
  12. import 'package:internet_connection_checker/internet_connection_checker.dart';
  13. import 'package:lottie/lottie.dart';
  14. import 'package:shared_preferences/shared_preferences.dart';
  15. import '../Home/home_screen.dart';
  16. import '../Splash/background.dart';
  17. import 'package:url_launcher/url_launcher.dart';
  18. import 'dart:developer' as logDev;
  19. class Splash extends StatefulWidget {
  20. const Splash({Key? key}) : super(key: key);
  21. @override
  22. _SplashState createState() => _SplashState();
  23. }
  24. class _SplashState extends State<Splash> {
  25. late StreamSubscription subscription;
  26. bool isDeviceConnected = false;
  27. bool isAlertSet = false;
  28. @override
  29. void initState() {
  30. //getConnectivity();
  31. //Timer(Duration(seconds: 10), () => loginStatus(context));
  32. Timer(Duration(seconds: 10), () => Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => LoginView())));
  33. super.initState();
  34. }
  35. /*getConnectivity() =>
  36. subscription = Connectivity().onConnectivityChanged.listen(
  37. (ConnectivityResult result) async {
  38. isDeviceConnected = await InternetConnectionChecker().hasConnection;
  39. if (!isDeviceConnected && isAlertSet == false) {
  40. showConnectivityDialogBox();
  41. setState(() => isAlertSet = true);
  42. }
  43. },
  44. );*/
  45. @override
  46. Widget build(BuildContext context) {
  47. return Scaffold(
  48. resizeToAvoidBottomInset: false,
  49. body: Stack(
  50. children: <Widget>[
  51. Background(),
  52. SplashScreen(),
  53. ],
  54. ));
  55. }
  56. /*showConnectivityDialogBox() =>
  57. showCupertinoDialog<String>(
  58. context: context,
  59. builder: (BuildContext context) =>
  60. CupertinoAlertDialog(
  61. title: const Text('No Connection'),
  62. content: const Text('Please check your internet connectivity'),
  63. actions: <Widget>[
  64. Column(
  65. children: [
  66. SizedBox(
  67. width: 250,
  68. height: 250,
  69. child: LottieBuilder.asset(
  70. //'assets/animation/animation_no_internet.json',
  71. 'assets/animation/animation_no_internet_3.json',
  72. repeat: true),
  73. ),
  74. ],
  75. ),
  76. TextButton(
  77. onPressed: () async {
  78. Navigator.pop(context, 'Cancel');
  79. setState(() => isAlertSet = false);
  80. isDeviceConnected =
  81. await InternetConnectionChecker().hasConnection;
  82. if (!isDeviceConnected && isAlertSet == false) {
  83. showConnectivityDialogBox();
  84. setState(() => isAlertSet = true);
  85. }
  86. },
  87. child: const Text('Retry'),
  88. ),
  89. ],
  90. ),
  91. );*/
  92. }
  93. class SplashScreen extends StatelessWidget {
  94. const SplashScreen({Key? key}) : super(key: key);
  95. @override
  96. Widget build(BuildContext context) {
  97. var size = MediaQuery.of(context).size;
  98. return Scaffold(
  99. body: Container(
  100. width: double.infinity,
  101. height: double.infinity,
  102. decoration: BoxDecoration(
  103. gradient: LinearGradient(
  104. begin: Alignment.topRight,
  105. end: Alignment.bottomRight,
  106. colors: [
  107. Color(0xFF4858A7),
  108. Color(0xFF4858A7),
  109. Color(0xFF6474C6),
  110. //Color(0xFF8C99DD),
  111. //Color(0xFFFFFFFF)
  112. ])),
  113. child: Center(
  114. child: Column(
  115. mainAxisAlignment: MainAxisAlignment.center,
  116. crossAxisAlignment: CrossAxisAlignment.center,
  117. children: [
  118. Container(
  119. /*decoration: BoxDecoration(
  120. shape: BoxShape.circle,
  121. color: Colors.white
  122. ),*/
  123. child: SizedBox(
  124. width: size.width * 0.55,
  125. //height: 250,
  126. child: Image.asset('assets/icons/tower_1.png')
  127. /*LottieBuilder.asset('assets/animation/animation_4.json',
  128. repeat: true, ),*/
  129. ),
  130. ),
  131. Container(
  132. margin: EdgeInsets.only(top: 15),
  133. child: SizedBox(
  134. child: Image.asset('assets/icons/logo_bakti_1.png',
  135. width: size.width * 0.25,)
  136. /*LottieBuilder.asset('assets/animation/animation_4.json',
  137. repeat: true, ),*/
  138. ),
  139. ),
  140. /* Text(appName,
  141. style: GoogleFonts.carterOne(fontSize: 25, color: Colors.white))*/
  142. ],
  143. ),
  144. ),
  145. ));
  146. }
  147. }
  148. Future<void> loginStatus(BuildContext context) async {
  149. final SharedPreferences prefs = await SharedPreferences.getInstance();
  150. String? version = prefs.getString('version');
  151. String? device = prefs.getString('device');
  152. String? session = prefs.getString('session');
  153. String? notif_token = prefs.getString('notif_token');
  154. if (session == null) {
  155. version = "1.0.0";
  156. session = "";
  157. notif_token = "";
  158. if (Platform.isAndroid){
  159. device = "android";
  160. } else if (Platform.isIOS){
  161. device = "ios";
  162. }
  163. }
  164. //logDev.log(session, name: "SESSIONNYA");
  165. LoginStatus_Post.connectToAPI(version!, device!, session, notif_token!).then((valueResult) async {
  166. Map<String, dynamic> object = json.decode(valueResult);
  167. if (object.containsKey("result").toString() == "true") {
  168. String status = object['result']['status'].toString();
  169. if (status == "success") {
  170. Fluttertoast.showToast(
  171. msg: "You are already logged in",
  172. toastLength: Toast.LENGTH_SHORT,
  173. gravity: ToastGravity.CENTER,
  174. timeInSecForIosWeb: 1,
  175. textColor: Colors.white,
  176. fontSize: 16.0);
  177. Navigator.pushReplacement(
  178. context, MaterialPageRoute(builder: (context) => HomeView()));
  179. } else if (status == "failed") {
  180. String message = object['result']['message'].toString();
  181. if (message == "Not Logged In") {
  182. Fluttertoast.showToast(
  183. msg: message,
  184. toastLength: Toast.LENGTH_SHORT,
  185. gravity: ToastGravity.CENTER,
  186. timeInSecForIosWeb: 1,
  187. textColor: Colors.white,
  188. fontSize: 16.0);
  189. Navigator.pushReplacement(
  190. context, MaterialPageRoute(builder: (context) => LoginView()));
  191. } else {
  192. String title = object['result']['title'].toString();
  193. String action = object['result']['action'].toString();
  194. String block = object['result']['block'].toString();
  195. if (block == "false") {
  196. Widget okButton = TextButton(
  197. child: Text(action),
  198. onPressed: () async {
  199. Uri url = Uri.parse(Uri.encodeFull("https://play.google.com/store/apps"));
  200. //final Uri uri = Uri(scheme: "https", host: "play.google.com");
  201. if(!await launchUrl(url, mode: LaunchMode.externalApplication)) {
  202. throw "Can not launch url";
  203. }
  204. },
  205. );
  206. Widget noButton = TextButton(
  207. child: Text("Later"),
  208. onPressed: () {
  209. if (session == ""){
  210. Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => LoginView()));
  211. } else {
  212. Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => HomeView()));
  213. }
  214. },
  215. );
  216. // set up the AlertDialog
  217. AlertDialog alert = AlertDialog(
  218. title: Text(appName),
  219. content: Text(title + "\n" + message),
  220. actions: [noButton, okButton],
  221. );
  222. // show the dialog
  223. showDialog(
  224. context: context,
  225. barrierDismissible: false,
  226. builder: (BuildContext context) {
  227. return alert;
  228. },
  229. );
  230. } else if (block == "true") {
  231. Widget okButton = TextButton(
  232. child: Text(action),
  233. onPressed: () async {
  234. if (action == "update") {
  235. _launchURL(Uri.parse("https://play.google.com/store/apps"));
  236. SystemChannels.platform.invokeMethod('SystemNavigator.pop');
  237. Uri url = Uri.parse(Uri.encodeFull("https://play.google.com/store/apps"));
  238. //final Uri uri = Uri(scheme: "https", host: "play.google.com");
  239. if(!await launchUrl(url, mode: LaunchMode.externalApplication)) {
  240. throw "Can not launch url";
  241. }
  242. } else if (action == "close") {
  243. SystemChannels.platform.invokeMethod('SystemNavigator.pop');
  244. }
  245. },
  246. );
  247. // set up the AlertDialog
  248. AlertDialog alert = AlertDialog(
  249. title: Text(appName),
  250. content: Text(title + "\n" + message),
  251. actions: [okButton],
  252. );
  253. // show the dialog
  254. showDialog(
  255. context: context,
  256. barrierDismissible: false,
  257. builder: (BuildContext context) {
  258. return alert;
  259. },
  260. );
  261. }
  262. }
  263. }
  264. } else {
  265. alertDialogFailedResponse(context);
  266. Fluttertoast.showToast(
  267. msg: "Server Response Error",
  268. toastLength: Toast.LENGTH_SHORT,
  269. gravity: ToastGravity.CENTER,
  270. timeInSecForIosWeb: 1,
  271. textColor: Colors.white,
  272. fontSize: 16.0);
  273. }
  274. });
  275. }
  276. _launchURL(Uri url) async {
  277. if (await canLaunchUrl(url)) {
  278. await launchUrl(url);
  279. } else {
  280. throw 'Could not launch $url';
  281. }
  282. }
  283. alertDialogFailedResponse(BuildContext context){
  284. Widget okButton = TextButton(
  285. child: Text("Refresh"),
  286. onPressed: () {
  287. Navigator.of(context, rootNavigator: true).pop();
  288. Navigator.pushReplacement(context, MaterialPageRoute(
  289. builder: (context) => SplashScreen()));
  290. },
  291. );
  292. Widget noButton = TextButton(
  293. child: Text("Back"),
  294. onPressed: () {
  295. Navigator.of(context, rootNavigator: true).pop();
  296. Navigator.pop(context);
  297. },
  298. );
  299. // set up the AlertDialog
  300. AlertDialog alert = AlertDialog(
  301. title: Text(appName),
  302. content: Text("Server Response Error"),
  303. actions: [
  304. noButton,
  305. okButton,
  306. ],
  307. );
  308. // show the dialog
  309. showDialog(
  310. context: context,
  311. barrierDismissible: false,
  312. builder: (BuildContext context) {
  313. return alert;
  314. },
  315. );
  316. }