I'm building my first app on flutter. On my splash screen I'm checking for data updates. First, the application parses initial data from xml then checks for latest database version and asks user whether to download the zip folder or not. So on splash screen I have a Text message and a progress indicator whose value need to be changed. For text: "Checking data update" > "Downloading" > "Preparing Data" & progress indicator first needs to be indeterminate, then, determinate, then indeterminate. The UI layout is in splash-screen.dart and method for checking data updates is in another data helper class. I want to know how can I update desired values from the methods in helper class as I'm unable to access UI items from there. I hvae tried using setstate after intistate and some other call ack options but nothing seems to e working. Please help what basic concept of data flow and communication I'm missing. Thank you!
splash-screen:
class SplashScreen extends StatefulWidget {
SplashScreenState createState() => new SplashScreenState();
static const String routeName = 'splash-screen';
}
@override
class SplashScreenState extends State<SplashScreen> {
AppInitialDataManager _appManager = new AppInitialDataManager();
AppDataModel appdata=new AppDataModel();
bool isDownload= false;
String _dataprogress ="Checking data update";
LinearProgressIndicator _progressIndicator = new LinearProgressIndicator(backgroundColor: Color(0xFFBFBFC0));
@override
void initState() {
super.initState();
_appManager.getData(context);
}
/** @override
void didInitState(){
checkDataUpdates();
}
void checkDataUpdates() async{
isDownload = await _appManager.checkDbVersion(context);
if (isDownload==true){
setState(() {
_dataprogress= "Downloading data files";
});
}
}**/
@override
Widget build(BuildContext context) {
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
]);
return Scaffold(
body: new Container(
padding: EdgeInsets.all(16.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
mainAxisSize: MainAxisSize.max, crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Consumer<ThemeNotifier>(
builder: (context, notifier, child) => SwitchListTile(
title: Text("Switch Theme",
style: Theme.of(context).textTheme.bodyText2),
onChanged: (value) {
notifier.toggleTheme();
},
value: notifier.nightTheme,
),
),
Text(
"App Title",
style: new TextStyle(
fontSize:55.0,
//color: const Color(0xFFFFFFFF),
fontWeight: FontWeight.w300,
fontFamily: "Roboto"),
textAlign:TextAlign.center
),
new FlutterLogo(
size: 206.0,
// colors: Colors.blue
),
_progressIndicator,
new Text(
_dataprogress ?? '',
style: new TextStyle(fontSize:16.0,
// color: const Color(0xFFFFFFFF),
fontWeight: FontWeight.w300,
fontFamily: "Roboto"),
),
new Text(
"Oranization name",
style: new TextStyle(fontSize:30.0,
// color: const Color(0xFFFFFFFF),
fontWeight: FontWeight.w300,
fontFamily: "Roboto",
),
textAlign:TextAlign.center
)
]),
));
}
}
DataProviding class:
class AppInitialDataManager {
final Xml2Json xml2Json = Xml2Json();
AppDataModel appdata;
ProgressDialog pr;
DatabaseManager _dm = new DatabaseManager();
getData(BuildContext context) async {
try {
http.Response response = await http.get(link);
xml2Json.parse(response.body);
print(response.body);
var jsondata = xml2Json.toParker();
Map<String, dynamic> parseddata = json.decode(jsondata);
appdata = new AppDataModel.fromJson(parseddata);
print('App version, ${appdata.version}!');
print('Tajweed URL ${appdata.tajweed}.');
print('New db version ${appdata.db.version}.');
versionCheck(context);
}
catch (exception) {
print('Unable to Parse Data');
}
}
versionCheck(context) async {
//Get Current installed version of app
// final PackageInfo info = await PackageInfo.fromPlatform();
// double currentVersion = double.parse(info.version.trim().replaceAll(".", ""));
double currentVersion = 8.0;
print('Current version:, $currentVersion');
try {
var newVersion = appdata.version;
double latestVersion = double.parse(newVersion);
print('New version, $latestVersion');
if (latestVersion > currentVersion) {
_showVersionDialog(context);
}
} catch (exception) {
print('Unable to compare versions. Cached or default values will be '
'used');
}
}
_showVersionDialog(context) async {
await showDialog<String>(
context: context,
barrierDismissible: false,
builder: (BuildContext context) {
String title = "New Update Available";
String message =
"There is a newer version of app available please update it now.";
String btnLabel = "Update Now";
String btnLabelCancel = "Later";
return Platform.isIOS
? new CupertinoAlertDialog(
title: Text(title),
content: Text(message),
actions: <Widget>[
FlatButton(
child: Text(btnLabel),
onPressed: () => _launchURL(APP_STORE_URL),
),
FlatButton(
child: Text(btnLabelCancel),
onPressed: () => Navigator.pop(context),
),
],
)
: new AlertDialog(
title: Text(title),
content: Text(message),
actions: <Widget>[
FlatButton(
child: Text(btnLabel),
onPressed: () => _launchURL(PLAY_STORE_URL),
),
FlatButton(
child: Text(btnLabelCancel),
onPressed: () => Navigator.pop(context),
),
],
);
},
);
checkDbVersion(context);
}
_launchURL(String url) async {
if (await canLaunch(url)) {
await launch(url);
} else {
throw 'Could not launch $url';
}
}
Future<bool> checkDbVersion(BuildContext context) async
{
double currentdbVersion = _dm.currentdbVersion;
print('Current db version:, $currentdbVersion');
try {
var newVersion = appdata.db.version;
double latestVersion = double.parse(newVersion);
print('New db version, $latestVersion');
if (latestVersion > currentdbVersion) {
http.Response r = await http.head('https://data.db.zip');
final fileSize = r.headers["content-length"];
var sizeMBd = double.parse(fileSize)/(1024*1024);
var sizeMB = sizeMBd.toStringAsFixed(2);
showDialog<void>(
context: context,
barrierDismissible: false, // user must tap button!
builder: (BuildContext context) {
return AlertDialog(
title: Text('AlertDialog Title'),
content: SingleChildScrollView(
child: ListBody(
children: <Widget>[
Text('File size is, $sizeMB. MB'),
],
),
),
actions: <Widget>[
TextButton(
child: Text('Approve'),
onPressed: () {
Navigator.of(context).pop();
return true; // here i want to change values on the splash screen
},
),
],
);
},
);
//.downloadZip(context);
_dm.currentdbVersion= latestVersion;
print(' db version updated, $latestVersion');
}
else{
// pr = new ProgressDialog(context,type: ProgressDialogType.Normal, isDismissible: true, showLogs: true);
pr.style(message: 'Dataase already updated');
pr.show();
Future.delayed(Duration(seconds: 3)).then((value){
pr.hide().whenComplete(() {
print(pr.isShowing());
});
} );
}
} catch (exception) {
print('Unable to compare db versions. Cached or default values will be '
'used');
}
}
}
what i tried to run checkdersion() after etData():
@override
void initState() {
super.initState();
checkDataUpdates();
}
/** @override
void didInitState(){
checkDataUpdates();
}
**/
void checkDataUpdates() async{
await _appManager.getData(context);
isDownload = await _appManager.checkDbVersion(context);
if (isDownload==true){
setState(() {
_dataprogress= "Downloading data files";
});
}
}
User contributions licensed under CC BY-SA 3.0