首页 > 其他 > 详细

tabController保活

时间:2020-01-11 19:20:00      阅读:70      评论:0      收藏:0      [点我收藏+]

代码:

import ‘package:flutter/material.dart‘;
class MyhomePage extends StatefulWidget {
MyhomePage({Key key}) : super(key: key);

@override
_MyhomePageState createState() => _MyhomePageState();
}

class _MyhomePageState extends State<MyhomePage> with AutomaticKeepAliveClientMixin {

int _count = 0;
@override
bool get wantKeepAlive => true;//方法
void _inaddCount(){
setState(() {
_count ++;
});
}

@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(‘计数‘),
Text(‘$_count‘,style: Theme.of(context).textTheme.display2,)

],
),
),
floatingActionButton: FloatingActionButton(
onPressed:_inaddCount,
tooltip: ‘点击加1‘,
child: Icon(Icons.add),
),
);
}
}
总结:
 

保活—就是保持tabController中的数据不会归零

继承于AutomaticKeepAliveClientMixin

//重写wantKeepAlie 方法 并置为true

  bool get wantKeepAlive => true;//方法

写个内置函数 重写setState()

tabController保活

原文:https://www.cnblogs.com/pp-pping/p/12180714.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!