美文网首页flutter开发问题记录flutter dart
flutter中tabview切换避免重回

flutter中tabview切换避免重回

作者: flutter开发精选 | 来源:发表于2018-12-10 10:26 被阅读179次

1.使用AutomaticKeepAliveClientMixin,根据文档说明
/// A mixin with convenience methods for clients of [AutomaticKeepAlive]. Used
/// with [State] subclasses.
///
/// Subclasses must implement [wantKeepAlive], and their [build] methods must
/// call super.build (the return value will always return null, and should be
/// ignored).
使用条件
1.必须是 Statefull 的widget
2.重写 wantKeepAlive
@override
bool get wantKeepAlive => true;
3.在build方法中,在返回你的内容之前必须调用super.build(context);
Widget build(BuildContext context) {
super.build(context);
return Scaffold(...);
}

国内的文章中都忽略了第三点,会导致意想不到的问题,所以建议大家还是多看官方文档

相关文章

网友评论

    本文标题:flutter中tabview切换避免重回

    本文链接:https://www.haomeiwen.com/subject/egfrhqtx.html