[英]Keep state of statefull widget that is inside a bloc builder
我有一個在 bloc 構建器中的 statefull 小部件,我的問題是是否無論如何我可以在重建之間保持小部件的狀態? 因為每次觸發 bloc 構建器時,都會重置 statefull 小部件狀態。
謝謝!
您可以使用AutomaticKeepAliveClientMixin
。
將它與您的狀態類一起使用。 您必須在狀態類中覆蓋wantKeepAlive
方法。 在您的情況下返回true
。
例子是
class _FooWidgetState extends State<FooWidget> with AutomaticKeepAliveClientMixin {
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
super.build(context);
// your build method
}
@override
bool wantKeepAlive => true;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.