我想在開始加載其中覆寫的全屏時添加帶有灰色背景的全屏加載。
在過去,我使用了 Stack() 小部件,但堆疊沒有覆寫我的應用欄。
我認為在 Stack 小部件中添加 Scaffold 不是這樣做的好方法
uj5u.com熱心網友回復:
你可以試試以下
有一個實用程式類
class Utils {
late BuildContext context;
Utils(this.context);
// this is where you would do your fullscreen loading
Future<void> startLoading() async {
return await showDialog<void>(
context: context,
barrierDismissible: false,
builder: (BuildContext context) {
return const SimpleDialog(
elevation: 0.0,
backgroundColor: Colors.transparent, // can change this to your prefered color
children: <Widget>[
Center(
child: CircularProgressIndicator(),
)
],
);
},
);
}
Future<void> stopLoading() async {
Navigator.of(context).pop();
}
Future<void> showError(Object? error) async {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
action: SnackBarAction(
label: 'Dismiss',
onPressed: () {
ScaffoldMessenger.of(context).hideCurrentSnackBar();
},
),
backgroundColor: Colors.red,
content: Text(handleError(error)),
),
);
}
}
然后在需要加載的地方使用
ElevatedButton(
onPressed: () async {
FocusScope.of(context).unfocus();
if (widget.formkey!.currentState!.validate()) {
Utils(context).startLoading();
widget.formkey!.currentState!.save();
widget.authProvider
.signInWithEmailAndPassword(
widget.textController.text.trim(),
widget.passwordController.text.trim())
.then((user) async {
// do something with user
Utils(context).stopLoading();
}).onError(
(error, stackTrace) {
Utils(context).showError(error);
Utils(context).stopLoading();
},
);
}
},
child: const Text(
AppConstants.kBtnLogin,
style: TextStyle(color: Colors.white),
),
)
uj5u.com熱心網友回復:
Center(
child: CircularProgressIndicator(),
),
uj5u.com熱心網友回復:
添加一個 showDialog() 將確保它覆寫 Scaffold 內的 appBar,當您希望加載器出現時添加它:
showDialog(
context: context,
barrierDismissible: false,
builder: (context) {
return Container(
color: Colors.grey,
child: Center(
child: CircularProgressIndicator(
color: Colors.white,
),
),
);
},
);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/454190.html
