Skip to content

Instantly share code, notes, and snippets.

@rido-ramadan
Created April 4, 2022 06:36
Show Gist options
  • Select an option

  • Save rido-ramadan/3c60a6afe93e1b18153324fe3874e3ae to your computer and use it in GitHub Desktop.

Select an option

Save rido-ramadan/3c60a6afe93e1b18153324fe3874e3ae to your computer and use it in GitHub Desktop.
Multiple Conditional Widgets
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: [
// Multiple conditional
if (_isShown) ... [
const Text('All text and button will disappear when button is pressed'),
const SizedBox(height: 16),
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