How To Ternary Conditional Operator In Flutter Dart

admin_img Posted By Bajarangi soft , Posted On 05-11-2020

Ternary operator is the only available operator which handles 2 operands. Ternary conditional operator has 2 code execution sides and each will be know as True block and False Block. Ternary operator works on condition and returns result in True False Boolean format. If the condition is True then it will execute the first code block and if the condition is false then it will execute the second code block.

How To Ternary Conditional Operator In Flutter Dart

Ternary Conditional Operator
Complete Code For Ternary Conditional Operator In Flutter
main.dart

import 'package:flutter/material.dart';

void main(){
  runApp(MyApp());
}
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: MyHomePage(),
    );
  }
}
class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> with SingleTickerProviderStateMixin {
  int value_A = 40 ;
  int value_B = 50 ;
  bool value_c = true ;
  void checkWithCondition() {
    (value_A > value_B) ? print('Value A is Big') : print('Value B is Big') ;
  }
  void checkWithBoolean(){
    value_c ? print('Value C is True') : print('Value C is False');
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.black,
      appBar: AppBar(
        backgroundColor: Colors.lightGreen[600],
        title: Text('Ternary Operator'),
      ),
            body: Center(
                child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: <Widget>[
                      Container(
                          child: RaisedButton(
                            onPressed: () => checkWithCondition(),
                            child: Text('Call Ternary Operator With Condition'),
                            textColor: Colors.white,
                            color: Colors.lightGreen[600],
                          )
                      ),

                      Container(
                          child: RaisedButton(
                            onPressed: () => checkWithBoolean(),
                            child: Text('Call Ternary Operator With Boolean Value'),
                            textColor: Colors.white,
                            color: Colors.lightGreen[600],
                          )
                      ),

                    ])
            )
    );
  }
}

Related Post