import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:ota_update/ota_update.dart'; import 'package:http/http.dart' as http; import 'package:mobdr/service/shared_prefs.dart'; import 'package:mobdr/config/constant.dart'; class UpdateCheckPage extends StatefulWidget { @override _UpdateCheckPageState createState() => _UpdateCheckPageState(); } class _UpdateCheckPageState extends State { OtaEvent? currentEvent = null; String _currentVersion = SharedPrefs().appVersion; String _latestVersion = ''; String _releaseDate = ''; List _releaseNotes = []; String _downloadLink = ''; String _checksum = ''; @override void initState() { super.initState(); _fetchLatestVersion(); } Future _fetchLatestVersion() async { try { final response = await http .get(Uri.parse(DOMAIN_CHECK_VERSION + '/deploy/mobdr/version.json')); final data = jsonDecode(response.body); setState(() { _latestVersion = data['latest_version']; _releaseDate = data['release_date']; _releaseNotes = List.from(data['release_notes']); _downloadLink = data['download_link']; _checksum = data['checksum']; }); } catch (e) { print('Error retrieving latest version : $e'); } } Future _doUpdate() async { try { OtaUpdate() .execute( _downloadLink, // OPTIONAL destinationFilename: 'app_release.apk', //OPTIONAL, ANDROID ONLY - ABILITY TO VALIDATE CHECKSUM OF FILE: sha256checksum: _checksum, ) .listen( (OtaEvent event) { setState(() => currentEvent = event); }, ); } catch (e) { print('Failed to make OTA update. Details: $e'); } } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('Checking for update'), ), body: Center( child: currentEvent == null ? Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Text( 'Current version : ', style: TextStyle( fontWeight: FontWeight.bold, fontSize: 16, ), ), Text( _currentVersion, style: TextStyle( fontWeight: FontWeight.bold, fontSize: 22, color: Colors.red, ), ), SizedBox(height: 16), Text( 'Version available : ', style: TextStyle( fontWeight: FontWeight.bold, fontSize: 18, ), ), Text( _latestVersion, style: TextStyle( fontWeight: FontWeight.bold, fontSize: 24, color: Colors.green, ), ), if (_currentVersion != _latestVersion) ...[ SizedBox(height: 16), Text( 'Publication date : $_releaseDate', style: TextStyle(fontWeight: FontWeight.bold), ), SizedBox(height: 8), Text( 'Release notes :', style: TextStyle(fontWeight: FontWeight.bold), ), for (String note in _releaseNotes) ...[ SizedBox(height: 4), Text('- $note'), ], SizedBox(height: 16), ElevatedButton( onPressed: _doUpdate, child: Text( 'Update', style: TextStyle( fontWeight: FontWeight.bold, fontSize: 18, ), ), style: ElevatedButton.styleFrom( backgroundColor: Colors.blue, ), ), ] else ...[ SizedBox(height: 32), Text( 'You have the latest version', style: TextStyle( fontWeight: FontWeight.bold, fontSize: 18, ), ), ], ], ) : Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Text( 'OTA status', style: TextStyle( fontWeight: FontWeight.bold, fontSize: 24, ), ), SizedBox(height: 32), Text('${currentEvent!.status}'), SizedBox(height: 16), Text( '${currentEvent!.value}', style: TextStyle( fontWeight: FontWeight.bold, fontSize: 16, ), ) ], )), ); } }