flutter 功能的输出与预期结果不一致

w8f9ii69  于 2023-04-07  发布在  Flutter
关注(0)|答案(1)|浏览(124)

我想添加一个功能,如果比赛的最终得分等于预测得分,尾随文本应该是绿色的“Won”,否则应该是红色的“Lost”。但是我没有得到正确的逻辑,无论结果如何,尾随文本都显示lost。
这是一个很好的例子。

import 'package:flutter/material.dart';

class MyListTile extends StatelessWidget {
  const MyListTile({
    Key? key,
    required this.homeTeamName,
    required this.awayTeamName,
    required this.scoreFulltime,
    required this.oddsString,
  }) : super(key: key);

  final String homeTeamName;
  final String awayTeamName;
  final String scoreFulltime;
  final String oddsString;

  @override
  Widget build(BuildContext context) {
    final predictedScore = oddsString.split(' ')[0];
    final hasWon = scoreFulltime == predictedScore;

    return ListTile(
      leading: CircleAvatar(
        child: Text(homeTeamName[0]),
      ),
      contentPadding:
      const EdgeInsets.symmetric(horizontal: 30.0, vertical: 20.0),
      title: Padding(
        padding: const EdgeInsets.only(bottom: 10.0),
        child: Text(
          '$homeTeamName vs $awayTeamName',
          style: const TextStyle(fontWeight: FontWeight.w500, fontSize: 22),
        ),
      ),
      subtitle: RichText(
        text: TextSpan(
          text: 'Score:',
          style: const TextStyle(
            fontSize: 20,
            fontWeight: FontWeight.w800,
            color: Colors.blueAccent,
          ),
          children: <TextSpan>[
            TextSpan(
              text: ' $scoreFulltime  ',
              style: const TextStyle(color: Colors.black54),
            ),
            const TextSpan(
              text: '   Prediction:',
              style: TextStyle(color: Colors.blueAccent),
            ),
            TextSpan(
              text: ' $oddsString',
              style: const TextStyle(color: Colors.black54),
            ),
          ],
        ),
      ),
      trailing: Container(
        padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 4),
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(5),
          color: hasWon ? Colors.green : Colors.red,
        ),
        child: Text(
          hasWon ? 'Won' : 'Lost',
          style: const TextStyle(
            color: Colors.white,
            fontWeight: FontWeight.bold,
          ),
        ),
      ),
    );
  }
}

Screenshot of the scores screen
这是MyListTile的列表视图构建器

import 'package:flutter/material.dart';

import '../tips_widget.dart';

ListView myListBuilder(List<Map<String, dynamic>> tips) {
  return ListView.builder(
    itemCount: tips.length,
    itemBuilder: (context, index) {
      final tip = tips[index];
      final homeTeamName = tip['team1'];
      final awayTeamName = tip['team2'];
      final scoreFulltime = tip['score_fulltime'];
      final bettingTips = tip['betting_tips'];

      final odds = <String>[];
      bettingTips.forEach((key, value) {
        odds.add(value['odds']);
      });
      final oddsString = odds.join(' / ');

      return MyListTile(
          homeTeamName: homeTeamName,
          awayTeamName: awayTeamName,
          scoreFulltime: scoreFulltime,
          oddsString: oddsString,

      );
    },
  );
}
acruukt9

acruukt91#

我觉得你应该试试这个:

final hasWon = scoreFulltime == oddsString.replaceAll(“-”,”:”);

相关问题