Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Data Types
  • Functions
  • Oops
  • Collections
  • Sets
  • Dart Interview Questions
  • Fluter
  • Android
  • Kotlin
  • Kotlin Android
  • Android with Java
  • Android Studio
Open In App
Next Article:
Drawer Widget in Flutter
Next article icon

Flutter - Fade a Widget In and Out

Last Updated : 16 Oct, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

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 to get an idea about what we are going to do in this article.

Basic Syntax of AnimatedOpacity Widget

AnimatedOpacity(
duration: const Duration(milliseconds: 300), // Duration for the opacity animation
opacity: 0.5, // Opacity value (0.0 to 1.0) to control visibility
child: YourChildWidget(), // The widget you want to animate
)

Required Tools

To build this app, you need the following items installed on your machine:

  • Visual Studio Code / Android Studio
  • Android Emulator / iOS Simulator / Physical Device device.
  • Flutter Installed
  • Flutter plugin for VS Code / Android Studio.

Step By Step Implementation

Step 1: Create a New Project in Android Studio

To set up Flutter Development on Android Studio please refer to Android Studio Setup for Flutter Development, and then create a new project in Android Studio please refer to Creating a Simple Application in Flutter.

Step 2: Import the Package

First of all import material.dart file.

import 'package:flutter/material.dart';

Step 3: Execute the main Method

Here the execution of our app starts.

Dart
void main() {   runApp(MyApp()); } 

Step 4: Create MyApp Class

In this class we are going to implement the MaterialApp , here we are also set the Theme of our App.

Dart
class MyApp extends StatelessWidget {   @override   Widget build(BuildContext context) {     return MaterialApp(       title: 'AnimatedOpacity Example', // App title       theme: ThemeData(         primarySwatch: Colors.green, // App's primary theme color       ),       debugShowCheckedModeBanner: false,       home: AnimatedOpacityExample(), // Main home page     );   } } 

Step 5: Create AnimatedOpacityExample Class

In this class we are going to Implement the AnimatedOpacity widget . Comments are added for better understanding.

AnimatedOpacity(
duration: Duration(seconds: 1), // Animation duration
opacity: _isVisible ? 1.0 : 0.0, // Opacity value based on visibility
child: Container(
width: 200,
height: 200,
color: Colors.blue, // Color of the box
child: Center(
child: Text(
'Fading Box', // Text displayed inside the box
style: TextStyle(
color: Colors.white, // Text color
fontSize: 20.0, // Text font size
),
),
),
),
),
Dart
class AnimatedOpacityExample extends StatefulWidget {   @override   _AnimatedOpacityExampleState createState() => _AnimatedOpacityExampleState(); }  class _AnimatedOpacityExampleState extends State<AnimatedOpacityExample> {   bool _isVisible = true; // A variable to control the visibility of the box    void _toggleVisibility() {     setState(() {       _isVisible = !_isVisible; // Toggle the visibility when the button is pressed     });   }    @override   Widget build(BuildContext context) {     return Scaffold(       appBar: AppBar(         title: Text('AnimatedOpacity Example'), // AppBar title       ),       body: Center(         child: Column(           mainAxisAlignment: MainAxisAlignment.center,           children: <Widget>[             AnimatedOpacity(               duration: Duration(seconds: 1), // Animation duration               opacity: _isVisible ? 1.0 : 0.0, // Opacity value based on visibility               child: Container(                 width: 200,                 height: 200,                 color: Colors.blue, // Color of the box                 child: Center(                   child: Text(                     'Fading Box', // Text displayed inside the box                     style: TextStyle(                       color: Colors.white, // Text color                       fontSize: 20.0, // Text font size                     ),                   ),                 ),               ),             ),             SizedBox(height: 20), // SizedBox for spacing             ElevatedButton(               onPressed: _toggleVisibility, // Call _toggleVisibility when the button is pressed               child: Text(_isVisible ? 'Hide' : 'Show'), // Button text             ),           ],         ),       ),     );   } } 

Here is the full Code of main.dart file

Dart
import 'package:flutter/material.dart';  void main() {   runApp(MyApp()); }  class MyApp extends StatelessWidget {   @override   Widget build(BuildContext context) {     return MaterialApp(       title: 'AnimatedOpacity Example', // App title       theme: ThemeData(         primarySwatch: Colors.green, // App's primary theme color       ),       debugShowCheckedModeBanner: false,       home: AnimatedOpacityExample(), // Main home page     );   } }  class AnimatedOpacityExample extends StatefulWidget {   @override   _AnimatedOpacityExampleState createState() => _AnimatedOpacityExampleState(); }  class _AnimatedOpacityExampleState extends State<AnimatedOpacityExample> {   bool _isVisible = true; // A variable to control the visibility of the box    void _toggleVisibility() {     setState(() {       _isVisible = !_isVisible; // Toggle the visibility when the button is pressed     });   }    @override   Widget build(BuildContext context) {     return Scaffold(       appBar: AppBar(         title: Text('AnimatedOpacity Example'), // AppBar title       ),       body: Center(         child: Column(           mainAxisAlignment: MainAxisAlignment.center,           children: <Widget>[             AnimatedOpacity(               duration: Duration(seconds: 1), // Animation duration               opacity: _isVisible ? 1.0 : 0.0, // Opacity value based on visibility               child: Container(                 width: 200,                 height: 200,                 color: Colors.blue, // Color of the box                 child: Center(                   child: Text(                     'Fading Box', // Text displayed inside the box                     style: TextStyle(                       color: Colors.white, // Text color                       fontSize: 20.0, // Text font size                     ),                   ),                 ),               ),             ),             SizedBox(height: 20), // SizedBox for spacing             ElevatedButton(               onPressed: _toggleVisibility, // Call _toggleVisibility when the button is pressed               child: Text(_isVisible ? 'Hide' : 'Show'), // Button text             ),           ],         ),       ),     );   } } 

Output:



Next Article
Drawer Widget in Flutter
author
chinmaya121221
Improve
Article Tags :
  • Dart
  • Flutter
  • Geeks Premier League
  • Flutter-Widgets
  • Geeks Premier League 2023

Similar Reads

  • Expandable Widget in Flutter
    The expandable widgets do not have a fixed size, they expand on the screen according to the available areas on the screen. Due to size constraints, certain widgets can throw rendering errors, so widgets are made expandable. The one use of expandable widgets is cards that include images, texts, and t
    3 min read
  • Animated Widgets in Flutter
    The animations are considered hard work and take time to learn. Flutter made it easy with its packages. To animate the widgets without much effort, we can wrap them inside different defined animated widgets in the animate_do package. In this article, we will see how with the animate_do package we ca
    4 min read
  • endDrawer Widget in Flutter
    The endDrawer is the panel displayed to the side of the body (Scaffold Widget). It is generally hidden in mobile devices. We can open it by swiping in from right to left, or we can customise it to open on-press of an icon or a button. This widget is similar to the already present Drawer widget in fl
    2 min read
  • Drawer Widget in Flutter
    Drawer widget is used to provide access to different destinations and functionalities provided in your application. It is represented by three horizontal parallel lines on the upper end of the scaffold. It has a horizontal movement from the edge of the Scaffold that navigates the link to different r
    5 min read
  • Flutter - Fading a Widget
    Every application needs to navigate through multiple pages and components in an application. In flutter one way to do so is to make the use of routes (ie, pages). But when there is a need to just remove a part (element) from a page, using routes becomes redundant. This is where Fading comes in. In t
    3 min read
  • What are Widgets Available in Flutter?
    Flutter is a mobile app development framework that allows developers to create beautiful and responsive user interfaces with ease. This is made possible through the use of Flutter widgets, which are the building blocks of every Flutter application. In Flutter, widgets are the fundamental building bl
    4 min read
  • Draggable Widget in Flutter
    In Flutter, a Draggable widget can be used to allow users to interact with a widget by dragging it around the screen. To create a Draggable widget, you can use the Draggable class and pass it to a child widget to be rendered, along with a feedback widget that will be displayed while the user is drag
    4 min read
  • Opacity Widget in Flutter
    The Opacity widget that makes its child partially transparent. This class colors its child into an intermediate buffer and then merges the child back into the scene partially transparent. For values of opacity other than 0.0 and 1.0, this class is relatively expensive as it needs coloring the child
    2 min read
  • Flow Widget in Flutter
    The Flow widget in Flutter is a layout widget that positions children's elements in a flow along with sizing and positions its children proficiently using FlowDelegate. It allows you to create a grid-like layout where the children are positioned according to a given alignment, and they flow from one
    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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences