bool _isFavorited = true;
int _favoriteCount = 41;
void _toggleFavorite() {
setState(() {
// If the lake is currently favorited, unfavorite it.
if (_isFavorited) {
_favoriteCount -= 1;
_isFavorited = false;
// Otherwise, favorite it.
} else {
_favoriteCount += 1;
_isFavorited = true;
}
});
}
@override
Widget build(BuildContext context) {
return new Row(
mainAxisSize: MainAxisSize.min,
children: [
new Container(
padding: new EdgeInsets.all(0.0),
child: new IconButton(
icon: (_isFavorited
-
? new Icon(Icons.star)
- new Icon(Icons.star_border)),
color: Colors.red[500],
onPressed: _toggleFavorite,
),
),
new SizedBox(
width: 18.0,
child: new Container(
child: new Text(‘$_favoriteCount’),
),
),
],
);
}
}
代码运行结果如下:
有多种方法可以管理状态,用户可以选择使用何种管理方法,一般而言就在父widget中管理状态。
1.widget管理自己的state
有些情况下widget在内部管理其状态是最好的。例如, 当ListView的内容超过渲染框时, ListView自动滚动。大多数使用ListView的开发人员不想管理ListView的滚动行为,因此ListView本身管理其滚动偏移量。 class TapBoxA extends StatefulWidget{ @override State createState() { return new _TapBoxA(); } }
class _TapBoxA extends State{
bool _active = false;
void _handleTap(){
setState(() {
_active = !_active;
});
}
@override
Widget build(BuildContext context) {
return new GestureDetector(
onTap: _handleTap,
child: new Container(
child: new Center(
child: new Text(
_active ? “Active” : “Inactive”,
style: new TextStyle(fontSize: 32, color: Colors.white),
),
),
width: 200.0,
height: 200.0,
decoration: new BoxDecoration(
color: _active ? Colors.lightGreen[700] :Colors.grey[600],
),
),
);
}
}
代码运行效果如图:
2.父widget管理widget的状态
对于父widget来说,管理状态并告诉其子widget何时更新通常是最有意义的。 例如,IconButton允许您将图标视为可点按的按钮。 IconButton是一个无状态的小部件,因为我们认为父widget需要知道该按钮是否被点击来采取相应的处理。在以下示例中,TapboxB通过回调将其状态导出到其父项。由于TapboxB不管理任何状态,因此它的父类为StatelessWidget。
class ParentWidget extends StatefulWidget{
@override
State createState() {
return new _ParentWidgetState();
}
}
class _ParentWidgetState extends State{
bool _active = false;
void _handleTapboxChanged(bool newValue){
setState(() {
_active = newValue;
});
}
@override
Widget build(BuildContext context) {
return new Container(
child: TapBoxB(
active: _active,
onChanged: _handleTapboxChanged,
),
);
}
}
class TapBoxB extends StatelessWidget{
-
TapBoxB({Key key, this.active: false, @required this.onChanged})
- super(key: key);
final bool active;
final ValueChanged onChanged;
void _handleTap(){
onChanged(!active);
}
@override
Widget build(BuildContext context) {
return new GestureDetector(
onTap: _handleTap,
child: new Container(
child: new Center(
child: new Text(
active ? “Active” : “Inactive”,
style: new TextStyle(fontSize: 32, color: Colors.white),
),
),
width: 200,
height: 200,
decoration: new BoxDecoration(
color: active ? Colors.lightGreen[700] :Colors.grey[600],
),
),
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级安卓工程师,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年最新Android移动开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Android)
大家的负担。**
[外链图片转存中…(img-zHv1KMAi-1710924575059)]
[外链图片转存中…(img-MnwaaXLm-1710924575060)]
[外链图片转存中…(img-sAvN3dKm-1710924575060)]
[外链图片转存中…(img-LFNoFBvp-1710924575061)]
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Android)
[外链图片转存中…(img-9XmVa2dU-1710924575061)]