Flutter StatefulWidget基本demo

动态更改页面数据

在这里插入图片描述

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++}');
             });
           },
         )
      ],
    );
  }
}

猜你喜欢

转载自blog.csdn.net/qq_42572245/article/details/106639584