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],
)
),
])
)
);
}
}