【问题标题】:Flutter Status Color change颤振状态颜色变化
【发布时间】:2020-10-25 06:24:49
【问题描述】:

如果待处理变成红色,我如何更改来自数据库的状态值的颜色,如果完成它变成绿色

我的数据库中有一个值等待和完成,如果显示的状态为等待状态,我的颤动中的状态颜色将为红色,如果完成,颜色将为绿色

final x = list[i];
debugPrint(x.toString());
return Container(
  padding: EdgeInsets.all(20.0),
  child: Row(
    children: <Widget>[
      Expanded(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Text(
              'Vessel Name:\t' + x["vesselname"],
              style: TextStyle(fontSize: 20.0, fontWeight: FontWeight.bold),
            ),
            Text('Ship Call #:\t' + x["scn"], style: TextStyle(fontSize: 20.0)),
            Text('Vessel Number:\t' + x["voyageno"], style: TextStyle(fontSize: 20.0)),
            Text('Status:\t' + x["status"], style: TextStyle(fontSize: 20.0)),
            Divider()
          ],
        ),
      ),
    ],
  ),
);

这是我对数据库的请求:

var url = "http://192.168.1.15/vessellog/read.php";
  List<dynamic> list = [];
  Future<void> getData() async {
    list.clear();
    setState(() {
      loading = true;
    });
    Future<SharedPreferences> _prefs = SharedPreferences.getInstance();
    SharedPreferences preferences = await _prefs;
    String id = preferences.getString("id");
    final response = await http.get(url + "?client_signature=$id");
    debugPrint(response.body);
    if (response.contentLength == 2) {
    } else
      setState(() {
        list = jsonDecode(response.body);
        loading = false;
      });
  }

【问题讨论】:

    标签: flutter


    【解决方案1】:

    您需要使用 TextStyle 中的颜色参数。根据loading 值更改它。

    这是你的完整代码,这个小改动,见注释行:

    final x = list[i];
    debugPrint(x.toString());
    return Container(
      padding: EdgeInsets.all(20.0),
      child: Row(
        children: <Widget>[
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Text(
                  'Vessel Name:\t' + x["vesselname"],
                  style: TextStyle(fontSize: 20.0, fontWeight: FontWeight.bold),
                ),
                Text('Ship Call #:\t' + x["scn"], style: TextStyle(fontSize: 20.0)),
                Text('Vessel Number:\t' + x["voyageno"], style: TextStyle(fontSize: 20.0)),
                Text(
                  'Status:\t' + x["status"],
                  style: TextStyle(
                    fontSize: 20.0,
                    color: loading ? Colors.red : Colors.green, // isPending is the value from your database
                  ),
                ),
                Divider()
              ],
            ),
          ),
        ],
      ),
    );
    

    【讨论】:

    • 是的等待和完成实际上我的项目是网络和移动当网络中的用户将状态更新为完成时文本中的颜色将在完成时变为绿色,在等待时变为红色
    • 那么这能解决你的问题吗?如果不是,请解释原因。
    • 我有一个未定义的待处理
    • 您必须根据数据库中的状态值定义 isPending
    • 对不起,我是 Flutter 的新手
    猜你喜欢
    • 2021-04-19
    • 2020-09-27
    • 2019-12-04
    • 1970-01-01
    • 1970-01-01
    • 2019-04-19
    • 2021-09-28
    • 2019-10-03
    • 2020-06-12
    相关资源
    最近更新 更多