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),
),
);
}
}
总结: