Created
April 4, 2022 06:04
-
-
Save rido-ramadan/ff396ba48f415c1641cd48889419a22a to your computer and use it in GitHub Desktop.
Single Conditional Widget
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import 'package:flutter/material.dart'; | |
| class ConditionalWidget extends StatefulWidget { | |
| const ConditionalWidget({Key? key}) : super(key: key); | |
| @override | |
| State<ConditionalWidget> createState() => _ConditionalWidgetState(); | |
| } | |
| class _ConditionalWidgetState extends State<ConditionalWidget> { | |
| bool _isShown = true; | |
| @override | |
| Widget build(BuildContext context) { | |
| return Scaffold( | |
| appBar: AppBar( | |
| title: const Text('Conditional Widget'), | |
| ), | |
| body: Center( | |
| child: Column( | |
| mainAxisSize: MainAxisSize.min, | |
| children: [ | |
| // Static | |
| const Text('Button will disappear when you press it'), | |
| const SizedBox(height: 16), | |
| // Single conditional | |
| if (_isShown) | |
| ElevatedButton( | |
| child: const Text('Hide Button'), | |
| onPressed: _hideButton, | |
| ), | |
| ], | |
| ), | |
| ), | |
| ); | |
| } | |
| void _hideButton() { | |
| setState(() { | |
| _isShown = false; | |
| }); | |
| } | |
| } | |
| void main() => runApp(const MaterialApp(home: ConditionalWidget())); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment