In Flutter, controlling the visibility of widgets based on conditions is essential for creating dynamic and responsive user interfaces. The Visibility widget allows you to show or hide a single widget by setting its visible
property to true
or false
based on a condition. This is particularly useful for optimizing screen space and enhancing user experience by displaying only relevant information.
For example, you can hide a loading spinner once data is loaded or show a message only when a user is logged in. This flexibility ensures that your app adapts to different scenarios, making it more interactive and user-friendly.
flutter create visibility_demo
.cd visibility_demo
.main.dart
:
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key}) : super(key: key);
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
bool _isVisible = true;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Visibility Demo'),
),
body: Center(
child: Visibility(
visible: _isVisible,
child: Container(
width: 100,
height: 100,
color: Colors.blue,
),
),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
setState(() {
_isVisible = !_isVisible;
});
},
child: Icon(_isVisible ? Icons.visibility : Icons.visibility_off),
),
);
}
}
flutter run
in the terminal to start the app.This setup will create a simple Flutter app where a blue box’s visibility toggles when the floating action button is pressed.
Here’s how you can implement conditional visibility for a single widget using the Visibility
widget in Flutter:
Set up a new Flutter project:
flutter create visibility_example
cd visibility_example
Import necessary packages:
import 'package:flutter/material.dart';
Define a boolean variable to track visibility:
bool isWidgetVisible = true;
Create the widget you want to conditionally show or hide:
Widget myWidget = Container(
width: 200,
height: 200,
color: Colors.blue,
);
Use the Visibility
widget to conditionally show or hide the widget:
Visibility(
visible: isWidgetVisible,
child: myWidget,
);
Implement a button to toggle the visibility:
ElevatedButton(
onPressed: () {
setState(() {
isWidgetVisible = !isWidgetVisible;
});
},
child: Text('Toggle Widget Visibility'),
);
Combine everything in a StatefulWidget:
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
bool isWidgetVisible = true;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Visibility Example'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Visibility(
visible: isWidgetVisible,
child: Container(
width: 200,
height: 200,
color: Colors.blue,
),
),
SizedBox(height: 20),
ElevatedButton(
onPressed: () {
setState(() {
isWidgetVisible = !isWidgetVisible;
});
},
child: Text('Toggle Widget Visibility'),
),
],
),
),
);
}
}
This code sets up a simple Flutter app where a blue container’s visibility can be toggled using a button.
To control the visibility of a widget in Flutter using boolean conditions, you can use the Visibility
widget. Here’s a concise example:
Import necessary packages:
import 'package:flutter/material.dart';
Define a boolean variable to track visibility:
bool isWidgetVisible = true;
Create the widget you want to conditionally show or hide:
Widget myWidget = Container(
width: 200,
height: 200,
color: Colors.blue,
);
Use the Visibility
widget to control visibility:
Visibility(
visible: isWidgetVisible,
child: myWidget,
);
Implement a button to toggle visibility:
ElevatedButton(
onPressed: () {
setState(() {
isWidgetVisible = !isWidgetVisible;
});
},
child: Text('Toggle Widget Visibility'),
);
Complete example in a stateful widget:
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
bool isWidgetVisible = true;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Visibility Example'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Visibility(
visible: isWidgetVisible,
child: Container(
width: 200,
height: 200,
color: Colors.blue,
),
),
SizedBox(height: 20),
ElevatedButton(
onPressed: () {
setState(() {
isWidgetVisible = !isWidgetVisible;
});
},
child: Text('Toggle Widget Visibility'),
),
],
),
),
);
}
}
This code demonstrates how to use a boolean condition to control the visibility of a widget in Flutter. The Visibility
widget’s visible
property is set based on the boolean variable isWidgetVisible
, which is toggled by pressing the button.
Here’s how you can create toggle buttons to dynamically change the visibility of a widget in Flutter:
Set up a boolean variable to track the visibility state:
bool isWidgetVisible = true;
Use the Visibility
widget to conditionally show or hide the widget:
Visibility(
visible: isWidgetVisible,
child: Container(
width: 200,
height: 200,
color: Colors.blue,
),
);
Create a button to toggle the visibility:
ElevatedButton(
onPressed: () {
setState(() {
isWidgetVisible = !isWidgetVisible;
});
},
child: Text('Toggle Widget Visibility'),
);
Combine everything in your widget tree:
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Visibility(
visible: isWidgetVisible,
child: Container(
width: 200,
height: 200,
color: Colors.blue,
),
),
ElevatedButton(
onPressed: () {
setState(() {
isWidgetVisible = !isWidgetVisible;
});
},
child: Text('Toggle Widget Visibility'),
),
],
),
),
);
}
This setup allows you to toggle the visibility of a single widget using a button.
Here are the steps:
Set up a new Flutter project:
flutter create visibility_test_app
Import necessary packages:
import 'package:flutter/material.dart';
Define a boolean variable to track visibility:
bool isWidgetVisible = true;
Wrap the widget you want to hide/show with the Visibility
widget:
Visibility(
visible: isWidgetVisible,
child: YourWidget(),
);
Create a button to toggle visibility:
ElevatedButton(
onPressed: () {
setState(() {
isWidgetVisible = !isWidgetVisible;
});
},
child: Text('Toggle Visibility'),
);
Run the app and test the visibility toggle.
That’s it! This will allow you to hide or show a widget based on a condition in your Flutter app.
You can use the Visibility
widget from the material library.
The Visibility
widget takes two properties: visible
and child
. The visible
property is a boolean that determines whether the child widget should be displayed or not.
Visibility( visible: isWidgetVisible, child: YourWidget(),);
In this code, isWidgetVisible
is a boolean variable that tracks whether the widget should be visible or not. When isWidgetVisible
is true, the widget will be displayed; when it’s false, the widget will be hidden.
ElevatedButton( onPressed: () { setState(() { isWidgetVisible = !isWidgetVisible; }); }, child: Text('Toggle Visibility'),);
This code will toggle the visibility of the widget every time the button is pressed.
Overall, using the Visibility
widget with conditions is a powerful way to dynamically control the visibility of widgets in Flutter and create more interactive and user-friendly interfaces.