I wanted to add a functionality whereby if the final score of the match is equal to the predicted score, the trailing text should be "Won" with green colour, else it should be "Lost" with red colour. But then I’m not getting the logic right, no matter the outcome, the trailing text shows lost.
This is the code below.
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
This is the listview builder of 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,
);
},
);
}
>Solution :
I think you should try this:
final hasWon = scoreFulltime == oddsString.replaceAll(“-”,”:”);