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
Flutter - Concept of Key in Widgets
In this article, we will study Keys and when and where to use them. So you may ask what are keys? Well, Keys are the ones to preserve state when widgets move around the widget tree. It is used to preserve the user scroll location or keeping state when modifying a collection. Â Key's aren't needed if
5 min read
Life Cycle of Flutter Widgets
Stateless Widgets: The widgets which remain constant throughout the lifetime of the app are called stateless widgets. We use them when we want structures to stay the same all over the app, for example, the AppBar, color scheme, i.e. generally the User Interface(UI). These widgets are immutable, i.e.
2 min read
Flutter - Row and Column Widgets
Row and Column are the two most important and powerful widgets in Flutter. These widgets let you align children horizontally and vertically as per the requirement. As we know that when we design any UI(User Interface) Â in a flutter, we need to arrange its content in the Row and Column manner so thes
6 min read
Flutter - Fade a Widget In and Out
The AnimatedOpacity widget in Flutter allows you to animate the opacity (transparency) of a child widget. You can use it to smoothly fade in or out a widget. In this article, we are going to implement the AnimatedOpacity Widget in Flutter and see the basic syntax of it. A sample video is given below
4 min read
Flutter - Splitting App into Widgets
Splitting an app into widgets refers to the breaking up of large widgets into smaller, more manageable widgets which can be inferred as smaller chunks of codes. The principal thing here is to understand when to split a widget into smaller ones. We are going to discuss when to split a widget and meth
5 min read
Raised Button widget in Flutter
RaisedButton is the material design button based on a Material widget that elevates when pressed upon in flutter. It is one of the most widely used buttons in the flutter library. Let's understand this button with the help of an example. Disclaimer: As of May 2021 the RaisedButton class in flutter i
5 min read
Difference Between Stateless and Stateful Widget in Flutter
As we all know the flutter app consists of widgets only, these are broadly classified into two types:Â Stateless WidgetStateful WidgetState: Before knowing the difference between the stateless and stateful widget we should take a look at the definition of State of a widget. The State is information
3 min read
Flutter - Widget Tree and Element Tree
As we know, the user interface of an application when it is designed in Flutter contains a set of widgets. So that means the whole application is set to be made out of widgets. A widget describes what the view of the app would look like in the given state. Now, as the user interface contains severa
4 min read
OffStage Widget in Flutter
Flutter provides an inbuilt widget called âOffstageâ, which is been used to hide or show any widget programmatically depending on user action/event. Offstage Widget is a widget that lays the child out as if it was in the tree, but without painting anything, without making the child available for hit
4 min read
Flutter - ListTile Widget
The ListTile widget is used to populate a ListView in Flutter. It contains a title as well as leading or trailing icons. Let's understand this with the help of an example.The Constructor of the ListTile classListTile({ Key key, Widget leading, Widget title, Widget subtitle, Widget trailing, bool isT
4 min read