Reputation: 345
I have developed a Flutter application and I used ProgressDialog
widget (progress_dialog: ^1.2.0
). First, I show the ProgressDialog
widget and after some code, then I try to hide ProgressDialog. But it doesn't work.
This is my code
class SignUp extends StatefulWidget {
@override
_SignUpState createState() => _SignUpState();
}
class _SignUpState extends State<SignUp> {
@override
Widget build(BuildContext context) {
pr = new ProgressDialog(context,type: ProgressDialogType.Normal);
pr.style(
message: 'Sing Up .......',
borderRadius: 20.0,
backgroundColor: Colors.white,
progressWidget: CircularProgressIndicator(),
maxProgress: 1.0,
);
return Scaffold(
backgroundColor: Color.fromRGBO(23, 63, 73, 1),
appBar: AppBar(
title: Text("Sign up"),
backgroundColor: Colors.black,
),body: Stack(
......
_addUser();
),
);
}
Future _addUser() async {
pr.show();
.........
pr.hide();
}
}
After that, I find the solution to this problem.
Future.delayed(Duration(seconds: 3)).then((value) {
pr.hide();
});
But why does ProgressDialog.hide()
not work here but it works after delaying the process??
Upvotes: 2
Views: 3708
Reputation: 54397
When your code execute to pr.hide() the progress_dialog still not show on screen
so your pr.hide() not work
you can see issues https://github.com/fayaz07/progress_dialog/issues/38
and check pr.isShowing() will not work because in source code of progress_dialog https://github.com/fayaz07/progress_dialog/blob/master/lib/progress_dialog.dart
bool _isShowing
is ahead before real showDialog()
code snippet of progress_dialog
void show() {
if (!_isShowing) {
_dialog = new _Body();
_isShowing = true;
if (_showLogs) debugPrint('ProgressDialog shown');
showDialog<dynamic>(
context: _context,
barrierDismissible: false,
builder: (BuildContext context) {
_dismissingContext = context;
return WillPopScope(
onWillPop: () {
return Future.value(_barrierDismissible);
},
child: Dialog(
backgroundColor: _backgroundColor,
insetAnimationCurve: _insetAnimCurve,
insetAnimationDuration: Duration(milliseconds: 100),
elevation: _dialogElevation,
shape: RoundedRectangleBorder(
borderRadius:
BorderRadius.all(Radius.circular(_borderRadius))),
child: _dialog),
);
},
);
} else {
if (_showLogs) debugPrint("ProgressDialog already shown/showing");
}
}
}
full test code
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:progress_dialog/progress_dialog.dart';
ProgressDialog pr;
void main() {
runApp(MaterialApp(
home: FirstScreen(),
));
}
class MyApp extends StatelessWidget {
double percentage = 0.0;
@override
Widget build(BuildContext context) {
// pr = new ProgressDialog(context,
// type: ProgressDialogType.Normal, isDismissible: false);
pr = new ProgressDialog(context, type: ProgressDialogType.Download);
pr.style(
message: 'Downloading file...',
borderRadius: 10.0,
backgroundColor: Colors.white,
elevation: 10.0,
insetAnimCurve: Curves.easeInOut,
progress: 0.0,
maxProgress: 100.0,
progressTextStyle: TextStyle(
color: Colors.black, fontSize: 13.0, fontWeight: FontWeight.w400),
messageTextStyle: TextStyle(
color: Colors.black, fontSize: 19.0, fontWeight: FontWeight.w600),
);
return Scaffold(
body: Center(
child: RaisedButton(
child: Text(
'Show Dialog',
style: TextStyle(color: Colors.white),
),
color: Colors.blue,
onPressed: () {
pr.show();
Future.delayed(Duration(seconds: 2)).then((onvalue) {
percentage = percentage + 30.0;
print(percentage);
pr.update(
progress: percentage,
message: "Please wait...",
progressWidget: Container(
padding: EdgeInsets.all(8.0),
child: CircularProgressIndicator()),
maxProgress: 100.0,
progressTextStyle: TextStyle(
color: Colors.black,
fontSize: 13.0,
fontWeight: FontWeight.w400),
messageTextStyle: TextStyle(
color: Colors.black,
fontSize: 19.0,
fontWeight: FontWeight.w600),
);
Future.delayed(Duration(seconds: 2)).then((value) {
percentage = percentage + 30.0;
pr.update(
progress: percentage, message: "Few more seconds...");
print(percentage);
Future.delayed(Duration(seconds: 2)).then((value) {
percentage = percentage + 30.0;
pr.update(progress: percentage, message: "Almost done...");
print(percentage);
Future.delayed(Duration(seconds: 2)).then((value) {
pr.hide().whenComplete(() {
print(pr.isShowing());
});
percentage = 0.0;
});
});
});
});
Future.delayed(Duration(seconds: 10)).then((onValue) {
print("PR status ${pr.isShowing()}");
if (pr.isShowing())
pr.hide().then((isHidden) {
print(isHidden);
});
print("PR status ${pr.isShowing()}");
});
}),
),
);
}
}
class FirstScreen extends StatefulWidget {
@override
_FirstScreenState createState() => _FirstScreenState();
}
class _FirstScreenState extends State<FirstScreen> {
ProgressDialog pr;
@override
Widget build(BuildContext context) {
pr = new ProgressDialog(context, showLogs: true);
pr.style(message: 'Please wait...');
return Scaffold(
body: Center(
child: RaisedButton(
child: Text('Show dialog and go to next screen',
style: TextStyle(color: Colors.white)),
color: Colors.blueAccent,
onPressed: () async{
print("before pr show");
pr.show();
print("after pr show");
print(pr.isShowing());
pr.hide().whenComplete((){print("hide complete");}
);
print("after pr hide");
print(pr.isShowing());
/*Future.delayed(Duration(seconds: 3)).then((value) {
pr.hide().whenComplete(() {
Navigator.of(context).push(CupertinoPageRoute(
builder: (BuildContext context) => SecondScreen()));
});
});*/
},
),
),
);
}
}
class SecondScreen extends StatefulWidget {
@override
_SecondScreenState createState() => _SecondScreenState();
}
class _SecondScreenState extends State<SecondScreen> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(child: Text('I am second screen')),
);
}
}
Upvotes: 2