Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

The output of the functionality isn't corresponding with the intended result

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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(“-”,”:”);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading