Advertisement
yurvan

Untitled

Dec 2nd, 2020
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.57 KB | None | 0 0
  1. import 'dart:convert';
  2. import 'dart:developer';
  3. import 'package:cocreate/pages/store/components/categories_list.dart';
  4. import 'package:cocreate/shared/shared.dart';
  5. import 'package:cocreate/utils/colors.dart';
  6. import 'package:flutter/material.dart';
  7. import 'package:http/http.dart' as http;
  8. import 'dart:io';
  9. import 'package:dio/dio.dart';
  10. import 'package:flutter_local_notifications/flutter_local_notifications.dart';
  11. import 'package:open_file/open_file.dart';
  12. import 'package:path_provider/path_provider.dart';
  13. import 'package:flutter/foundation.dart';
  14. import 'package:permission_handler/permission_handler.dart';
  15. import 'package:path/path.dart' as path;
  16.  
  17. class BodyStore extends StatefulWidget {
  18. BodyStore({Key key, this.title}) : super(key: key);
  19.  
  20. final String title;
  21.  
  22. @override
  23. _BodyStore createState() => _BodyStore();
  24. }
  25.  
  26. class _BodyStore extends State<BodyStore> {
  27. //APIDATASTORE
  28. final String apiUrl = "https://storeapi.digi46.id/index";
  29. //APIAPK
  30. final String _fileUrl = "https://storeapi.digi46.id/index";
  31. final Dio _dio = Dio();
  32. final String _fileName = "";
  33. String _progress = "-";
  34.  
  35. //progressbar
  36.  
  37. FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin;
  38.  
  39. @override
  40. void initState() {
  41. super.initState();
  42.  
  43. flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
  44. final android = AndroidInitializationSettings('@mipmap/ic_launcher');
  45. final iOS = IOSInitializationSettings();
  46. final initSettings = InitializationSettings(android, iOS);
  47.  
  48. flutterLocalNotificationsPlugin.initialize(initSettings,
  49. onSelectNotification: _onSelectNotification);
  50. }
  51.  
  52. Future<void> _onSelectNotification(String json) async {
  53. final obj = jsonDecode(json);
  54.  
  55. if (obj['isSuccess']) {
  56. OpenFile.open(obj['filePath']);
  57. } else {
  58. showDialog(
  59. context: context,
  60. builder: (_) => AlertDialog(
  61. title: Text('Error'),
  62. content: Text('${obj['error']}'),
  63. ),
  64. );
  65. }
  66. }
  67.  
  68. Future<void> _showNotification(Map<String, dynamic> downloadStatus) async {
  69. final android = AndroidNotificationDetails(
  70. 'channel id', 'channel name', 'channel description',
  71. priority: Priority.High, importance: Importance.Max);
  72. final iOS = IOSNotificationDetails();
  73. final platform = NotificationDetails(android, iOS);
  74. final json = jsonEncode(downloadStatus);
  75. final isSuccess = downloadStatus['isSuccess'];
  76.  
  77. await flutterLocalNotificationsPlugin.show(
  78. 0, // notification id
  79. isSuccess ? 'Success' : 'Failure',
  80. isSuccess
  81. ? 'File has been downloaded successfully!'
  82. : 'There was an error while downloading the file.',
  83. platform,
  84. payload: json);
  85. }
  86.  
  87. Future<Directory> _getDownloadDirectory() async {
  88. // if (Platform.isAndroid) {
  89. // return await DownloadsPathProvider.downloadsDirectory;
  90. // }
  91.  
  92. // in this example we are using only Android and iOS so I can assume
  93. // that you are not trying it for other platforms and the if statement
  94. // for iOS is unnecessary
  95.  
  96. // iOS directory visible to user
  97. return await getApplicationDocumentsDirectory();
  98. }
  99.  
  100. Future<bool> _requestPermissions() async {
  101. var permission = await PermissionHandler()
  102. .checkPermissionStatus(PermissionGroup.storage);
  103.  
  104. if (permission != PermissionStatus.granted) {
  105. await PermissionHandler().requestPermissions([PermissionGroup.storage]);
  106. permission = await PermissionHandler()
  107. .checkPermissionStatus(PermissionGroup.storage);
  108. }
  109.  
  110. return permission == PermissionStatus.granted;
  111. }
  112.  
  113. void _onReceiveProgress(int received, int total) {
  114. if (total != -1) {
  115. setState(() {
  116. _progress = (received / total * 100).toStringAsFixed(0) + "%";
  117. });
  118. }
  119. }
  120.  
  121. Future<void> _startDownload(String savePath, String tipeApk) async {
  122. Map<String, dynamic> result = {
  123. 'isSuccess': false,
  124. 'filePath': null,
  125. 'error': null,
  126. };
  127. print("tipe apk : " + tipeApk);
  128. try {
  129. final response = await _dio.download(_fileUrl + tipeApk, savePath,
  130. onReceiveProgress: _onReceiveProgress);
  131.  
  132. result['isSuccess'] = response.statusCode == 200;
  133. result['filePath'] = savePath;
  134. } catch (ex) {
  135. result['error'] = ex.toString();
  136. } finally {
  137. await _showNotification(result);
  138. }
  139. log("$result");
  140. print("$result");
  141. }
  142.  
  143. Future<void> _download(String tipeApk) async {
  144. final dir = await _getDownloadDirectory();
  145. final isPermissionStatusGranted = await _requestPermissions();
  146.  
  147. if (isPermissionStatusGranted) {
  148. final savePath = path.join(dir.path, _fileName);
  149. await _startDownload(savePath, tipeApk);
  150. } else {
  151. // handle the scenario when user declines the permissions
  152. }
  153. }
  154.  
  155. Future<List<dynamic>> fetchDataStore() async {
  156. var data = await http.get(apiUrl);
  157. return json.decode(data.body)['Data'];
  158. }
  159.  
  160. String _idApps(dynamic datastore) {
  161. return datastore['Idapps'];
  162. }
  163.  
  164. String _appImg(dynamic datastore) {
  165. return datastore['Appimg'];
  166. }
  167.  
  168. String _name(dynamic datastore) {
  169. return datastore['Apsname'];
  170. }
  171.  
  172. String _appversion(dynamic datastore) {
  173. return datastore['Appversion'];
  174. }
  175.  
  176. String _description(dynamic datastore) {
  177. return datastore['Description'];
  178. }
  179.  
  180. String _operats(dynamic datastore) {
  181. return datastore['Operats'];
  182. }
  183.  
  184. String _appName(dynamic datastore) {
  185. return datastore['Appname'];
  186. }
  187.  
  188. String _createdAt(dynamic datastore) {
  189. return datastore['CreatedAt'];
  190. }
  191.  
  192. @override
  193. Widget build(BuildContext context) {
  194. return Column(
  195. children: [
  196. Row(
  197. children: [
  198. Container(
  199. margin: EdgeInsets.all(kDefaultPadding),
  200. child: Align(
  201. alignment: Alignment.centerLeft,
  202. child: Text(
  203. "Store",
  204. style: TextStyle(fontSize: 30, fontWeight: FontWeight.bold),
  205. ),
  206. ),
  207. ),
  208. Container(
  209. margin: EdgeInsets.fromLTRB(250.0, 30.0, 10.0, 10.0),
  210. child: Align(
  211. alignment: Alignment.center,
  212. child: Text(
  213. '$_progress',
  214. ),
  215. ),
  216. ),
  217. ],
  218. ),
  219. CategoriesList(),
  220. Expanded(
  221. child: Stack(
  222. children: [
  223. Container(
  224. decoration: BoxDecoration(
  225. color: Colors.white,
  226. ),
  227. ),
  228. // Text(
  229. // '$_progress',
  230. // style: Theme.of(context).textTheme.display1,
  231. // ),
  232. // ListView.builder(
  233. // itemCount: 7,
  234. // itemBuilder: (context, index) => AppCard(),
  235. // )
  236. Container(
  237. child: FutureBuilder<List<dynamic>>(
  238. future: fetchDataStore(),
  239. builder: (BuildContext context, AsyncSnapshot snapshot) {
  240. if (snapshot.hasData) {
  241. // print(_age(snapshot.data[0]));
  242. return ListView.builder(
  243. padding: EdgeInsets.all(2),
  244. itemCount: snapshot.data.length,
  245. itemBuilder: (BuildContext context, int index) {
  246. Size size = MediaQuery.of(context).size;
  247. return Container(
  248. margin: EdgeInsets.symmetric(
  249. horizontal: kDefaultPadding, vertical: 10),
  250. // color: Colors.lightBlue,
  251. height: 100,
  252. child: Stack(
  253. alignment: AlignmentDirectional.topCenter,
  254. children: [
  255. Container(
  256. height: 100,
  257. decoration: BoxDecoration(
  258. borderRadius: BorderRadius.circular(10),
  259. color: Colors.white,
  260. boxShadow: [
  261. BoxShadow(
  262. blurRadius: 6,
  263. offset: Offset(1, 3),
  264. color:
  265. Colors.black.withOpacity(0.2))
  266. ]),
  267. ),
  268. Positioned(
  269. left: 5,
  270. top: 8,
  271. child: Row(
  272. children: [
  273. Container(
  274. margin: EdgeInsets.all(5),
  275. height: 60,
  276. width: 60,
  277. // child: Image.asset(
  278. // "assets/apklogo2.png",
  279. // fit: BoxFit.cover,
  280. // ),
  281. child: Image.network(
  282. // "https://ulummudin.xyz/files/images/5fb202b53f48c20001968243.png",
  283. _appImg(snapshot.data[index]),
  284. fit: BoxFit.contain,
  285. ),
  286. ),
  287. Container(
  288. padding: EdgeInsets.all(8),
  289. margin: EdgeInsets.only(left: 10),
  290. child: Column(
  291. crossAxisAlignment:
  292. CrossAxisAlignment.start,
  293. children: [
  294. Text(
  295. _name(snapshot.data[index]),
  296. overflow:
  297. TextOverflow.ellipsis,
  298. style:
  299. TextStyle(fontSize: 16),
  300. ),
  301. Text(
  302. _appversion(
  303. snapshot.data[index]),
  304. style: TextStyle(
  305. color: kAccentColor4),
  306. ),
  307. Text(
  308. _operats(
  309. snapshot.data[index]),
  310. style: TextStyle(
  311. color: kAccentColor4)),
  312. // Text(
  313. // '$_progress',
  314. // ),
  315. ],
  316. ),
  317. )
  318. ],
  319. )),
  320. Positioned(
  321. right: 0,
  322. child: SizedBox(
  323. height: 100,
  324. width: size.width - 330,
  325. child: Container(
  326. padding: EdgeInsets.symmetric(
  327. horizontal: kDefaultPadding),
  328. child: Image.asset(
  329. "assets/icon-download.png",
  330. ),
  331. ),
  332. )),
  333. InkWell(
  334. onTap: () => _download(
  335. _appName(snapshot.data[index]),
  336. ),
  337.  
  338. // handle your onTap here
  339. child: Container(height: 200, width: 200),
  340. ),
  341. ],
  342. ),
  343. );
  344. });
  345. } else {
  346. return Center(
  347. child: CircularProgressIndicator(
  348. valueColor:
  349. new AlwaysStoppedAnimation<Color>(COLOR_GREEN),
  350. ));
  351. }
  352. },
  353. ),
  354. ),
  355. ],
  356. ),
  357. )
  358. ],
  359. );
  360. }
  361. }
  362.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement