विजेट प्रॉपर्टी में इफ कंडीशन कैसे लिख सकते हैं?


मैं आइकन विजेट में if कंडीशन का उपयोग करना चाहता हूं, लेकिन डार्ट एक त्रुटि देता है। इसका सही उपयोग कैसे किया जाना चाहिए? धन्यवाद।

class Cell extends StatelessWidget {
  const Cell({super.key, this.caseTrend = false});

  final bool caseTrend;

  @override
  Widget build(BuildContext context) {
    return Container(
      alignment: Alignment.center,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        children: [
          const Text("23.410"),
          Icon(
          if(caseTrend) { //<== getting error here: Expected an identifier.dart Expected to find ')'.
            Icons.trending_up,
            color: Colors.green,
          } 
          else
          {
            Icons.trending_down,
            color: Colors.red,
          },
            size: 24, 
          )
        ],
      ),
    );
  }
}

मैंने क्या प्रयास किया है:

class Cell extends StatelessWidget {
  const Cell({super.key, this.caseTrend = false});

  final bool caseTrend;

  @override
  Widget build(BuildContext context) {
    return Container(
      alignment: Alignment.center,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        children: [
          const Text("23.410"),
          Icon( //This way working well but using conditions two times
           caseTrend ? Icons.trending_up : Icons.trending_down, 
           color: caseTrend ? Colors.green : Colors.red,
            size: 24, 
          )
        ],
      ),
    );
  }
}

समाधान 1

दुर्भाग्य से, आप if स्टेटमेंट को सीधे अपने विजेट गुणों के अंदर नहीं लिख सकते हैं जैसे कि आप अपनी विधि या फ़ंक्शन में लिख रहे थे। लेकिन ऐसे कुछ तरीके हैं जिनसे आप अपने विजेट के अंदर एक सशर्त विवरण लिख सकते हैं। आप विजेट प्रॉपर्टी के भीतर सशर्त प्रतिपादन प्राप्त करने के लिए टर्नरी ऑपरेटर (स्थिति? expr1: expr2) का उपयोग कर सकते हैं, जैसा कि आपने बताया है कि आपने क्या प्रयास किया है। विजेट गुणों के अंदर उपयोग करने के लिए कुछ अन्य वैकल्पिक समाधानों का उपयोग करने के लिए यहां संदर्भ लिंक दिया गया है।
यदि कुछ और हो तो फड़फड़ाएं: शीर्ष 3 तरीके जिन्हें आपको जानना आवश्यक है (कोड) [January 2024] – फ़्लटरबीड्स[^]

コメント

タイトルとURLをコピーしました