Open In App

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:

Let us dive into the article by creating a simple app with two pages. We have used VS code IDE for the development.



Key Terminologies Used:

Implementation:

Step 1: Creating a new Flutter app:



Note: If you are using Android Studio IDE, then use the path File-> New-> New Flutter Project.

Step 2: Adding Textfield to get data from users:

Syntax:

TextEditingController object_name = TextEditingController();
child: TextField(
            // controller attribute. 
               controller: controller_object_name,
               decoration: InputDecoration(
                  border: OutlineInputBorder(),
                  labelText: "Enter your Phone Number"
               ),
       ),

Step 3: Sending data and Navigating to the next page:

 ElevatedButton(
                onPressed: () {
                //Navigator for next page.
                  Navigator.of(context).push(
                    MaterialPageRoute(
                    //Builder for the nextpage class's constructor.
                        builder: (context) => nextpage(
                              name: _name.text,
                              email: _email.text,
                              phoneno: _phoneno.text,
                            )),
                          );
                    },
                child: Text("SEND")
                ),

main.dart




import 'package:flutter/material.dart';
import 'package:geeksforgeeks/nextpage.dart';
  
void main() {
  runApp(const MyApp());
}
  
class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);
  
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.green,
      ),
      home: const MyHomePage(title: 'GeeksForGeeks'),
    );
  }
}
  
class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);
  final String title;
  @override
  State<MyHomePage> createState() => _MyHomePageState();
}
  
class _MyHomePageState extends State<MyHomePage> {
    
  // To listen to the changes in the textfield.
  TextEditingController _name = TextEditingController();
  TextEditingController _email = TextEditingController();
  TextEditingController _phoneno = TextEditingController();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Padding(
              padding: const EdgeInsets.all(25),
              child: TextField(
                  
                // To set the appropriate controller to the text field.
                controller: _name,
                decoration: InputDecoration(
                    border: OutlineInputBorder(), labelText: "Enter your Name"),
              ),
            ),
            Padding(
              padding: const EdgeInsets.all(25),
              child: TextField(
                controller: _email,
                decoration: InputDecoration(
                    border: OutlineInputBorder(),
                    labelText: "Enter your Email"),
              ),
            ),
            Padding(
              padding: const EdgeInsets.all(25),
              child: TextField(
                controller: _phoneno,
                decoration: InputDecoration(
                    border: OutlineInputBorder(),
                    labelText: "Enter your Phone Number"),
              ),
            ),
              
               // Button to go to nextpage.
            ElevatedButton(
                onPressed: () {
                    
                  // Navigator to the next page.
                  Navigator.of(context).push(
                    MaterialPageRoute(
                        
                        // Builder for the nextpage class's constructor.
                        builder: (context) => nextpage(
                            
                              // Data as arguments to send to next page.
                              name: _name.text,
                              email: _email.text,
                              phoneno: _phoneno.text,
                            )),
                  );
                },
                child: Text("SEND"))
          ],
        ),
      ),
    );
  }
}

Step 4: Create new dart File:

String name, email, phoneno;
    nextpage({required this.name, required this.email, required this.phoneno});
TextEditingController object_name = TextEditingController(text: data);

nextpage.dart




import 'package:flutter/material.dart';
  
class nextpage extends StatelessWidget {
  String name, email, phoneno;
    
  // Constructor to get the data from the previous page.
  nextpage({required this.name, required this.email, required this.phoneno});
  
  @override
  Widget build(BuildContext context) {
      
    // To listen to the changes in the textfield.
    TextEditingController _name = TextEditingController(text: name);
    TextEditingController _email = TextEditingController(text: email);
    TextEditingController _phoneno = TextEditingController(text: phoneno);
  
    return Scaffold(
      appBar: AppBar(
        title: Text('Next Page'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Padding(
              padding: const EdgeInsets.all(25),
              child: TextField(
                  
                // To set the appropriate controller to the text field.
                controller: _name,
                decoration: InputDecoration(
                  border: OutlineInputBorder(),
                  labelText: "Name",
                ),
              ),
            ),
            Padding(
              padding: const EdgeInsets.all(25),
              child: TextField(
                controller: _email,
                decoration: InputDecoration(
                  border: OutlineInputBorder(),
                  labelText: "Email",
                ),
              ),
            ),
            Padding(
              padding: const EdgeInsets.all(25),
              child: TextField(
                controller: _phoneno,
                decoration: InputDecoration(
                  border: OutlineInputBorder(),
                  labelText: "Number",
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

Output:

Resulted App


Article Tags :