//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;
}
网友评论