Skip to content

Instantly share code, notes, and snippets.

@mukhtharcm
Last active March 15, 2021 12:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mukhtharcm/0f7f56b1ac8d25939bc53e2397edcac6 to your computer and use it in GitHub Desktop.
Save mukhtharcm/0f7f56b1ac8d25939bc53e2397edcac6 to your computer and use it in GitHub Desktop.
Example for using ternary operator in Flutter.
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Center(
child: MyWidget(),
),
),
);
}
}
class MyWidget extends StatefulWidget {
@override
_MyWidgetState createState() => _MyWidgetState();
}
class _MyWidgetState extends State<MyWidget> {
bool isLoading = true;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
isLoading ? CircularProgressIndicator() : Text("Loading Completed"),
SizedBox(height: 100),
Text("Click Button to Toggle Loading"),
SizedBox(height: 50),
ElevatedButton(
onPressed: () {
setState(() {
isLoading = !isLoading;
});
},
child: Text("Toggle Loading"),
)
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
setState(() {
isLoading = !isLoading;
});
},
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment