flutter基础(根据用户输入改变控件)-三

Flutter使用StatefulWidgets能够生成有状态对象的控件,然后用来保持状态

import 'package:flutter/material.dart';

void main(){
  runApp(new MaterialApp(
    title: "flutter控件状态改变",
    home: new Counter(),
  ));
}

class Counter extends StatefulWidget{

  @override
   _CounterState createState()=>new _CounterState();

}

class _CounterState extends State<Counter>{
  int _count = 0;
  void _increamnet(){
    setState(() {
      _count+=1;
    });
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return new Scaffold(
      appBar: new AppBar(
        title: new Text("flutter控件改变"),
      ),
      body: new Center(
        child: new Text("$_count"),
      ),
      floatingActionButton: new FloatingActionButton(onPressed: _increamnet,tooltip: "增加",child: new Icon(Icons.add),),
    );
  }
}

为什么StatefukWidget和State是单独的对象?

在flutter中,这两种类型的对象有不同的生命周期,控件是临时兑现个,用于构建应用程序的呈现。State对象在被build调用是是持久的,允许他们记住信息。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值