flutter - Flutter中如何通过dart改变一个Widget的Value
问题描述
我想知道如果满足某个条件,我可以在颤动中更改子小部件的值,例如尾随图标的颜色
这是一些伪代码:
if(condition){
trailing Icon(Icons.favorite).color = Colors.red[500]
}else{
trailing = Icon(Icons.Favorite).color = Colors.blue[300]
}
谢谢你。
解决方案
你想要这样的东西吗?
如果是,请尝试以下代码:
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
bool colorIndex = true;
void _changeColor(val) {
setState(() {
this.colorIndex = val;
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Welcome to Flutter',
home: Scaffold(
appBar: AppBar(
title: Text('Welcome to Flutter'),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
_changeColor(!colorIndex);
},
child: Icon(Icons.touch_app),
),
body: Center(
child: Padding(
padding: const EdgeInsets.all(50.0),
child: Card(
child: ListTile(
title: Text('Click FAB to change color'),
trailing: Icon(
Icons.favorite,
color: colorIndex ? Colors.red[500] : Colors.blue[300],
),
),
),
),
),
),
);
}
}
推荐阅读
- c# - 有没有办法通过使用不同的 url foreach 请求多次获取请求来使用 api?
- java - 如果没有休眠生成的 @Id 值,休眠 @SelectBeforeUpdate(false) 将不起作用
- apache-spark - 当 parquet 用于压缩时,列名中的空格会引发异常
- sql - 从通过 GROUP BY 获得的数据子集中 SELECT MIN
- python - 有没有用python一次去噪多个图像的功能
- javascript - 检查javascript中的二维矩阵中是否存在字符串?
- datatables - DataTables 导出 PDF,从未编辑的数据库中导出原始数据
- web - Tizen Web Widget,从后台启动应用程序到前台
- c - 调用内联函数时如何更改所有调用函数的优化?
- java - 过滤地图
> 到地图