import 'dart:io';
import 'package:flutter/material.dart';
import 'package:ota_update/ota_update.dart';
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
OtaEvent currentEvent;
bool _isLoading = false;
double _downloadProgress = 0.0;
Future<void> _checkForUpdate() async {
setState(() {
_isLoading = true;
});
try {
// Replace "APP_URL" with the URL of the new version of your app
OtaUpdate()
.execute("APP_URL")
.listen(
(OtaEvent event) {
setState(() {
currentEvent = event;
if (event.status == OtaStatus.DOWNLOADING) {
_downloadProgress = event.value;
}
});
},
);
} catch (e) {
print('Failed to check for update: $e');
} finally {
setState(() {
_isLoading = false;
});
}
}
void _onCancelPressed() {
OtaUpdate()
.cancelDownload()
.then((value) => setState(() {
currentEvent = OtaEvent(
status: OtaStatus.ABORTED,
value: 'Update cancelled',
);
_isLoading = false;
}));
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('OTA Update Example'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
if (_isLoading)
Column(
children: [
CircularProgressIndicator(),
SizedBox(height: 16),
Text(
'Downloading new version...',
style: TextStyle(fontSize: 16),
),
SizedBox(height: 16),
LinearProgressIndicator(
value: _downloadProgress,
),
SizedBox(height: 16),
ElevatedButton(
onPressed: _onCancelPressed,
child: Text('Cancel'),
),
],
)
else
ElevatedButton(
onPressed: _checkForUpdate,
child: Text('Check for Update'),
),
if (currentEvent != null)
Text('${currentEvent.status} : ${currentEvent.value}'),
],
),
),
);
}
}