Flutter - Concept of Visible and Invisible Widgets
Last Updated :
27 Mar, 2023
In this article, we will see how we can change the visibility of widgets in flutter. We will learn about various methods for managing the visibility of flutter widgets. There are a couple of ways to manage the visibility of widgets in flutter.
Method 1: Using Visibility class
It has a visible property that manages whether the child is included in the widget subtree or not. When it is set to false, the replacement child generally a zero-sized box is included instead.
Example 1: Showing the child widget by setting the visible parameter true of the Visibility widget.
Dart
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
// TextStyle widget constant
const TextStyle kstyle = TextStyle(fontSize: 60);
// Main class
// Extending StatelessWidget
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text("GeeksForGeeks"),
backgroundColor: Color.fromRGBO(15, 157, 88, 1),
),
body:
// to center the child widget
Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text("one", style: kstyle),
// Visibility widget to manage visibility
Visibility(
// showing the child widget
visible: true,
child: Text(
"two",
style: kstyle,
),
),
Text(
"three",
style: kstyle,
),
],
),
),
),
);
}
}
Output:

Explanation: In the body of this flutter app the Center is the parent widget holding a Column widget which in turn is having two Text widget and a Visibility widget as its children. The Visibility widget is placed in between the two Text widgets and it contains the text 'two'. The visible parameter in the Visibility widget takes a boolean value as the object and here it is set to true, which make the Text widget containing 'two' visible.
Example 2: Hiding the child widget by setting the visible parameter as false
Dart
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
// TextStyle widget constant
const TextStyle kstyle = TextStyle(fontSize: 60);
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text("GeeksForGeeks"),
),
body:
// to center the child widget
Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text("one", style: kstyle),
// Visibility widget to manage visibility
Visibility(
// hiding the child widget
visible: false,
child: Text(
"two",
style: kstyle,
),
),
Text(
"three",
style: kstyle,
),
],
),
),
),
);
}
}
Output:

Explanation: In this case, the second Text widget in the app body is wrapped with the Visibility widget and its visible parameter is set to false which makes disappear from the screen without leaving the space it had occupied in the previous example.
Example 3: In this example, we will see how to leave space for the hidden or invisible widget.
Dart
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
// TextStyle widget constant
const TextStyle kstyle = TextStyle(fontSize: 60);
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text("GeeksForGeeks"),
),
body:
// to center the child widget
Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text("one", style: kstyle),
// Visibility widget to manage visibility
Visibility(
// hiding the child widget
visible: false,
child: Text(
"two",
style: kstyle,
),
),
Text(
"three",
style: kstyle,
),
],
),
),
),
);
}
}
Output:

Explanation: If you look at the output of the second example you will notice that the space the Text widget (child of Visibility class) use to take is not there. Â For instance, if we want to make a widget disappear but the space it use to take should be empty instead of replacing it with the following widget then we can employ maintainSize parameter of the Visibility class which also takes a boolean value as the parameter. In the above maintainSize is set to true.Â
 Method 2: Using Opacity Class
The Opacity widget makes its child partially transparent.Opacity value decides whether the child will be painted or not. The child won't be removed from the widget tree but it will still take up space and have interactivity.
Example 4:
Dart
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
// TextStyle widget constant
const TextStyle kstyle = TextStyle(fontSize: 60);
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text("GeeksForGeeks"),
backgroundColor: Color.fromRGBO(15, 157, 88, 1),
),
body:
// to center the child widget
Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text("one", style: kstyle),
// Opacity widget to manage visibility
Opacity(
// hiding the child widget
opacity: 0,
child: Text(
"two",
style: kstyle,
),
),
Text(
"three",
style: kstyle,
),
],
),
),
),
);
}
}
Output:

Explanation: Similar to the second example the parent widget in the app body is Center which is holding a Column widget. Column widget is holding two Text widgets and in between that there is an Opacity widget whose child is a Text widget ('two' is the object in it). Inside the Opacity class, the parameter opacity is employed and is set to 0, which make the Text widget invisible here, but the place is used to occupy is still there.Â
Method 3: Using Offstage Class
Offset widget lays the child out as if it was in the widget tree, but doesn't paint anything. The child widget neither has any interactivity nor it takes any space.
Example 5:
Dart
import 'package:flutter/material.dart';
void main() {
runApp(const FirstApp());
}
const TextStyle aayush_style = TextStyle(fontSize: 60);
class FirstApp extends StatelessWidget {
const FirstApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
appBar: AppBar(
title: const Text("Aayush"),
backgroundColor: const Color.fromRGBO(15, 157, 88, 1),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: const [
Text("One", style: aayush_style),
// Offstage widget to manage visibility
Offstage(
offstage: false,
child: Text("Two", style: aayush_style,),
),
Text("Three", style: aayush_style)
],
),
),
),
);
}
}
Output:

Explanation: In this example, the second Text widget in the app body is wrapped with Offstage class. In the Offstage widget offstage is a property that taken in a boolean as the object, and in this case, it is set to true which makes the child (Text widget here) invisible as it is not painted on the screen, even the space occupied by the child is not there.
Similar Reads
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Steady State Response In this article, we are going to discuss the steady-state response. We will see what is steady state response in Time domain analysis. We will then discuss some of the standard test signals used in finding the response of a response. We also discuss the first-order response for different signals. We
9 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read
3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read
What is Vacuum Circuit Breaker? A vacuum circuit breaker is a type of breaker that utilizes a vacuum as the medium to extinguish electrical arcs. Within this circuit breaker, there is a vacuum interrupter that houses the stationary and mobile contacts in a permanently sealed enclosure. When the contacts are separated in a high vac
13 min read
AVL Tree Data Structure An AVL tree defined as a self-balancing Binary Search Tree (BST) where the difference between heights of left and right subtrees for any node cannot be more than one. The absolute difference between the heights of the left subtree and the right subtree for any node is known as the balance factor of
4 min read
CTE in SQL In SQL, a Common Table Expression (CTE) is an essential tool for simplifying complex queries and making them more readable. By defining temporary result sets that can be referenced multiple times, a CTE in SQL allows developers to break down complicated logic into manageable parts. CTEs help with hi
6 min read