The Wayback Machine - https://web.archive.org/web/20240913172715/https://www.geeksforgeeks.org/flutter-send-data-to-screen/
Open In App

Flutter – Send Data to Screen

Last Updated : 12 Jun, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Interaction with the UI is an integral part of any application. But more often than not, the information needs to be sent from one screen to another. For instance, say you need to pass data regarding a selected or tapped component of the UI to another route(ie, page). In this article, we will explore in detail the process of sending data to another screen by building a simple application.

For the purpose of better understanding, we will build a Task memo app that lists all the pending tasks on the home screen and when clicked on any of the tasks a respective detailed description of the task is shown on another page. Here we will be passing the data from the Home screen (the task that is tapped on) to a description screen.

To build the task memo app follow the below steps:

  • Create a Task class
  • List the tasks on the home screen
  • Design a screen that displays the description of the task
  • Pass data to the tapped task to the description screen

Let’s look into the task in detail.

Creating a Task class:

A simple way to define the task class is shown below:

Dart
class Task {
  final String task_name;
  final String description;

  Task(this.task_name, this.description);
}

Listing the tasks:

Use the ListView to generate the list of tasks. For the sake of simplicity we will be creating a list of 10 tasks as follows:

Dart
final tasks = List<Task>.generate(
  10,
  (i) => Task(
    'Task $i',
    'Task Description $i',
  ),
);

ListView.builder(
  itemCount: tasks.length,
  itemBuilder: (context, index) {
    return ListTile(
      title: Text(tasks[index].task_name),
    );
  },
);

 Now create a home screen where the list can be displayed using a StatelessWidget as follows:

Dart
class TodosScreen extends StatelessWidget {
  final List<Task> tasks;

  TodosScreen({Key key, @required this.tasks}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('GeeksForGeeks'),
        backgroundColor: Color.green,
      ),

      body: ListView.builder(
        itemCount: tasks.length,
        itemBuilder: (context, index) {
          return ListTile(
            title: Text(tasks[index].task_name)
          );
        },
      ),
    );
  }
}

Designing the Description screen:

Again use the StatelessWidget to create a route for the description page. The titlebar of the screen should show the task_name (name of the task) and the body should consist of the description of the task as shown below:

Dart
class DetailScreen extends StatelessWidget {

  final Task task;

  DetailScreen({Key key, @required this.todo}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(task.task_name),
      ),
      body: Padding(
        padding: EdgeInsets.all(16.0),
        child: Text(task.description),
      ),
    );
  }
}

Passing Data to the Description page:

Here we will assign a callback function to the onTap() function that uses the Navigator.push() method of the Navigator class to pass the data to the description screen as shown below:

Dart
ListView.builder(
  itemCount: todos.length,
  itemBuilder: (context, index) {
    return ListTile(
      title: Text(tasks[index].task_name),
      onTap: () {
        Navigator.push(
          context,
          MaterialPageRoute(
            builder: (context) => DetailScreen(task: tasks[index]),
          ),
        );
      },
    );
  },
);

Complete Source Code:

Dart
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

class Task {
  final String task_name;
  final String description;

  Task(this.task_name, this.description);
}

void main() {
  runApp(MaterialApp(
    title: 'Passing Data',
    home: TodosScreen(
      
      // generate list
      tasks: List.generate(
        10,
            (i) => Task(
          'Task $i',
          'Task Description $i',
        ),
      ),
    ),
  ));
}

// Home screen
class TodosScreen extends StatelessWidget {
  final List<Task> tasks;

  TodosScreen({Key key, @required this.tasks}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('GeeksForGeeks'),
        backgroundColor: Colors.green,
      ),
      // List builder
      body: ListView.builder(
        itemCount: tasks.length,
        itemBuilder: (context, index) {
          return ListTile(
            title: Text(tasks[index].task_name),
            onTap: () {
              Navigator.push(
                context,
                MaterialPageRoute(
                  builder: (context) => DetailScreen(task: tasks[index]),
                ),
              );
            },
          );
        },
      ),
    );
  }
}

// detail screen
class DetailScreen extends StatelessWidget {

  final Task task;
  DetailScreen({Key key, @required this.task}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(task.task_name),
        backgroundColor: Colors.green,
      ),
      body: Padding(
        padding: EdgeInsets.all(16.0),
        child: Text(task.description),
      ),
    );
  }
}

Output:



Previous Article
Next Article

Similar Reads

Flutter - Send Data to Screen using RouteSettings
Interaction with the UI is an integral part of any application. But more often than not, the information needs to be sent from one screen to another. For instance, say you need to pass data regarding a selected or tapped component of the UI to another route(ie, page). In this article, we will explore in detail the process of sending data to another
4 min read
Flutter - Pass Data One Screen To Another Screen
In this article we gonna know how you can pass the data from one screen to another screen in Flutter. In this example we took two screens one and screen two, In the first screen, you enter the data, and that data you can see on your second screen. This is the main object of this application. If we told you about the design of the app then on the fi
4 min read
Flutter - Return Data from Screen
Interaction with UI is an essential part of any application. During the same, there might raise a need to return data from the screen. This kind of interaction can range from selecting an option to navigating to different routes through the various buttons in the UI. In this article, we will explore the process of returning data from a screen in a
5 min read
Flutter - Sharing Data Among Flutter Pages
In this article, we are going to find the solution for the problem statement "Import/send data from one page to another in flutter". Before going into the topic, there are some pre-requisites. Pre-requisites:Basics of dart programming language.Setting up Flutter in VS code or Setting up Flutter in Android Studio.Creating first flutter app | Hello W
4 min read
Flutter - Animated Splash Screen
The Animated Splash screen is used for a startup screen in a Flutter application. More or less all applications use them generally to show the logo of the institution and its creators awareness. This although holds no functionality but it can be great to increase product awareness and promotion. Let's look deep into the implementation of the Animat
3 min read
How to Add Splash Screen in Flutter App?
We all have heard of Flutter right, it's a cross-platform application development tool. Flutter can be used to make Android, IOS, and Web applications with just one code base (Dart programming language). In this article let's see how we can add a splash screen to our applications. What is Splash Screen? When we open an application on our phones, th
3 min read
Flutter - Navigation to Previous Screen using GetX Library Function
When we are using any app then we do navigation to navigate between screens. Sometimes we want to return to the previous screen so we normally use Navigator.pop(context). This is using context and sometimes we find shortcuts to do the task easily. For that, we have Get.back() in flutter. We can send the result to the previous route and do the opera
3 min read
Flutter - Navigation to Next Screen using GetX Library Function
When we want to navigate between the screens in flutter then we use Navigator in Flutter. It uses context and builders for navigation. Sometimes, we pass the data to the next screen and fetch data on the next screen. When we use Navigator in flutter then its syntax is very large and it's not a good practice every time when we navigate to other scre
3 min read
Flutter - Determine the Height and Width of the Screen
Recently we created a Flutter Application and it's running well but when we installed the same application on another big-screen device it gives an error. When we switch the device to a new device the container is too long and throws an error. That means the application must be responsive to any screen. Flutter gives us to make a responsive screen
3 min read
Flutter - Navigate From One Screen to Another
Flutter apps may have multiple screens or pages. Pages are groups of functionalities. The user navigates between different pages to use different functionalities. Concepts like pages are called routes in Flutter. We can use Navigator.push() to navigate to a new route and Navigator.pop() to navigate to the previous route. Routes are managed by the N
3 min read
Flutter - Hold Splash Screen for Specific Time
In every application, we have the Splash Screen, But we can't set the time for the Splash. But In this article, we will see How to make the time-based Splash screen. A sample video is given below to get an idea about what we are going to do in this article. [video mp4="https://media.geeksforgeeks.org/wp-content/uploads/20221210214825/timebasedsplas
3 min read
How to Change Screen Orientation in Flutter on Click of a Button?
Flutter is an open-source UI toolkit developed and maintained by Google. It is used by big companies like Airbnb, Alibaba, and Google itself to serve billions of users around the globe. While developing the UX of an application, there are many situations where you want the device to be in a specific orientation for a better user experience. In this
2 min read
Flutter - OnBoarding Screen
Onboarding Screen is one of the most popular that you can see in most of the apps after loading of Splash Screen. The Onboarding Screen gives a short overview of an app. The onboarding Screen consists of three to four layouts which slide as we click on Next. In this article, we are going to see how to implement an Onboarding Screen in Flutter App.
7 min read
Flutter - Pass a Function as an Argument From One Screen to Another
In Flutter, you can pass a function as an argument from one screen (widget) to another screen (widget) by defining a callback function and passing it as an argument when navigating or pushing to the new screen. Here's a step-by-step guide on how to do this: Required Tools To build this app, you need the following items installed on your machine: Vi
4 min read
Flutter - Implement CrossFading Animation Between Two Screen
CrossFading Animation is an animation technique used to smoothly transition between two images or scenes. The primary concept is to fade out one image or scene while simultaneously fading in another, creating a seamless transition between them. Implementing a cross-fading animation between two screens in Flutter can provide a smooth transition betw
4 min read
Flutter - Onboarding Screen Using flutter_overboard Package
In Flutter, Onboarding screens, also known as welcome screens or introduction screens are a collection of screens or pages that are displayed to users when they first launch a mobile app or a software application. The primary purpose of onboarding screens is to introduce users to the key features, functionality, and benefits of the app, helping the
5 min read
Is Flutter Worth Learning? Top 7 Reasons to Learn Flutter
In today's era, the usage of smartphones has increased exponentially and so has the use of mobile applications and websites. Meanwhile, considering future career prospects, learning web development and mobile app development is strongly recommended for all individuals. And when we come to mobile app development, there are two most-popular open-sour
5 min read
Flutter Quill - Rich Text Editor in Flutter Application
This post will explain how to integrate a rich text editor into your Flutter application so that it may enable rich text editing. There are many more things we can do with Flutter Quill, but they can get a little involved. The example below is a pretty basic demonstration of what we mean. Let's first discuss what flutter quill is and why we need it
5 min read
Flutter - Animated AlertDialog in Flutter
Animating an AlertDialog in Flutter involves using the Flutter animations framework to create custom animations for showing and hiding the dialogue. In this article, we are going to add an animation to an AlertDialog. A sample video is given below to get an idea about what we are going to do in this article. [video mp4="https://media.geeksforgeeks.
4 min read
Flutter - Different Types of Loading using Flutter EasyLoading
Flutter applications involve incorporating loading indicators to provide users with feedback during background operations. Flutter has a package for simplifying the process named flutter_easyloading package. This package easiest way to integrate loading indicators with various styles. In this article, we will implement different types of loading in
5 min read
Retrieve Data From TextFields in Flutter
In this article, we'll learn how to retrieve data from TextFields. TextField() widget is the most common widget used in flutter apps to take user input. We'll talk about two major methods used to extract text from TextField. Using Variables:The TextField widget has various callback properties through which we can extract text. Majorly, onChanged is
4 min read
Flutter - Updating Data on the Internet
In today's world, most applications heavily rely on fetching and updating information from the servers through the internet. In Flutter, such services are provided by the http package. In this article, we will explore the same. To update the data on the Internet follow the below steps: Import the http packageUpdate data t using the http packageConv
5 min read
Flutter - Fetching Data From the Internet
In today's world, most applications heavily rely on fetching information from the servers through the internet. In Flutter, such services are provided by the http package. In this article we will explore the same. To fetch data from the internet follow the below steps: Import the http packageMake a network request using the http packageConvert the
3 min read
Flutter - Deleting Data On The Internet
In this article, we will explore the process of deleting data on the internet. To, do so we need to follow 3 crucial steps: Import the http packageDelete the data on the serverUpdate the screen after deletion Now, we will explore them in detail. Importing The http Package: To install the http package use the below command in your command prompt: pu
3 min read
Flutter - Sending Data To The Internet
Interacting with the Internet is crucial for most apps to function. In Flutter the http package is used to send the data to the internet. In this article, we will explore the same topic in detail. To send data to the internet through your application follow the below steps: Import the http packageSend data to the server through the http packageChan
4 min read
Flutter - Read JSON Data from Assets Folder
A JSON object contains data in the form of key/value pair. The keys are strings and the values are the JSON types. Keys and values are separated by a colon. Each entry (key/value pair) is separated by a comma. We will learn how to read the data already stored in your project folder in your app with just a simple function. Sometimes it happens that
3 min read
Flutter - Read and Write Data on Firebase
Firebase helps developers to manage their mobile app easily. It is a service provided by Google. Firebase has various functionalities available to help developers manage and grow their mobile apps. In this article, we will learn how to write and read data into/from firebase. We will be using flutter for this. To do this job we need to follow the 3-
4 min read
How to Import Data From One Page to Another in Flutter?
Flutter is Google’s Mobile SDK to build native iOS and Android, Desktop (Windows, Linux, macOS), Web apps from a single codebase. When building applications with Flutter everything towards Widgets – the blocks with which the flutter apps are built. They are structural elements that ship with a bunch of material design-specific functionalities and n
5 min read
Flutter - Fetching JSON Data using HTTP
In this article, we will learn how to fetch data from the internet or JSON file using the HTTP package in a flutter. What is HTTP? The HTTP is a composable, future-based library for making HTTP requests. This package contains a set of high-level functions and classes that make it easy to consume HTTP resources. It's multi-platform and supports mobi
4 min read
Persist Data with SQLite in Flutter
SQLite is an open supply computer database, it's wont to produce a piece of information, perform completely different operations like add, delete, and take away knowledge. SQLite doesn't need a server or backend code, all the info is saved to a computer file within the device, or we will say it's native to storage. Installing SQLite Plugin to Flutt
4 min read