Flutter StatefulWidget基本demo
动态更改页面数据class HomePage extends StatefulWidget {@override_HomePageState createState() => _HomePageState();}//自定义有状态组件class _HomePageState extends State<HomePage> {int count=1;@overrideWidget b
·
动态更改页面数据
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
//自定义有状态组件
class _HomePageState extends State<HomePage> {
int count=1;
@override
Widget build(BuildContext context) {
return Container(
alignment: Alignment.center,
child: Column(
children: <Widget>[
SizedBox(height: 200),
Text('${this.count}',style: TextStyle(
fontSize: 20
),),
RaisedButton(
child: Text('Button'),
onPressed: (){
setState(() {
this.count++;
});
},
)
],
),
);
}
}
//自定义有状态组件
class _HomePageState extends State<HomePage> {
List list=[];
int count=1;
@override
Widget build(BuildContext context) {
return ListView(
children: <Widget>[
Column(
children: list.map((value){
return Text(value,style: TextStyle(
fontSize: 20
),);
}).toList()
),
RaisedButton(
child: Text('新增一条数据'),
onPressed: (){
setState(() {
list.add('数据${this.count++}');
});
},
)
],
);
}
}
更多推荐
已为社区贡献3条内容
所有评论(0)