美文网首页
Flutter 切换PageView保存状态

Flutter 切换PageView保存状态

作者: 郑永博 | 来源:发表于2019-12-05 15:46 被阅读0次
    //1 PageView 子View集成AutomaticKeepAliveClientMixin
    //2 build方法添加super.build(context);
    //3 重写方法wantKeepAlive,返回true
    class OnePage extends StatefulWidget {
      final Color color;
    
      const OnePage({Key key, this.color}) : super(key: key);
    
      @override
      _OnePageState createState() => new _OnePageState();
    }
    
    class _OnePageState extends State<OnePage> with AutomaticKeepAliveClientMixin<OnePage> {
      @override
      Widget build(BuildContext context) {
        super.build(context);
        return new SizedBox.expand(
          child: new ListView.builder(
            itemCount: 100,
            itemBuilder: (context, index) {
              return new Padding(
                padding: const EdgeInsets.all(10.0),
                child: new Text(
                  '$index',
                  style: new TextStyle(color: widget.color),
                ),
              );
            },
          ),
        );
      }
    
      @override
      bool get wantKeepAlive => true;
    }
    

    相关文章

      网友评论

          本文标题:Flutter 切换PageView保存状态

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