Button click open another screen and pass value in flutter || pass data from one screen to another in flutter

Whenever we make an app, we have to create multiple screens so that the design can be done on different screens. When screens are made, the question arises to know how to transfer them to those screens, whether you are making the application on any platform like Java, Kotlin, React, wherever you are making the application. But as soon as multiple screens are created, they also have to be transferred to those screens. How can we transfer from one screen to another and at the same time pass some value to the other screen.

Step - 1

Whenever we make a flutter project, by default a file named .dart is created. So paste the code given by us in it, in which text field has been used and whatever value is there inside the text field, that will be passed to the other screen. Whatever package is there by default in flutter should not be removed.

main.dart
  
  
final TextEditingController _phoneController = TextEditingController();
final TextEditingController _emailController = TextEditingController();
  
  
  
  
  void _sendData() {
    String phoneNumber = _phoneController.text;
    String emailid = _emailController.text;
    if (phoneNumber.isNotEmpty ) {
      Navigator.push(
        context,
        MaterialPageRoute(
          builder: (context) => Registerscreen(receivedPhone: phoneNumber, receivedemailid: emailid),
        ),
      );
    } else {
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(content: Text("Please enter a valid 10-digit phone number")),
      );
    }
  }
  
  
  
  
      TextField(
              controller: _phoneController,
              decoration: InputDecoration(
                labelText: "Phone",
                border: OutlineInputBorder(borderRadius: BorderRadius.circular(12.0)),
                filled: true,
                fillColor: Colors.grey[200],
                prefixIcon: Icon(Icons.phone),
              ),
              keyboardType: TextInputType.phone,
            ),

            SizedBox(height: 20),

     TextField(
              controller: _emailController,
              decoration: InputDecoration(
                labelText: "Email",
                border: OutlineInputBorder(borderRadius: BorderRadius.circular(12.0)),
                filled: true,
                fillColor: Colors.grey[200],
                prefixIcon: Icon(Icons.phone),
              ),
              keyboardType: TextInputType.emailAddress,
            ),
  
  
  

ElevatedButton(
              onPressed: (){
                _sendData(); // ✅ Function call properly
              },
              style: ElevatedButton.styleFrom(
                padding: EdgeInsets.symmetric(horizontal: 50, vertical: 15),
                shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
                backgroundColor: Colors.deepPurple,
              ),
              child: Text("Proceed", style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold, color: Colors.white)),
            ),
            
            
   
recieve.dart

Ek dusra screen banaye Jiske andar aap value ko receive karna chahte hain aur button click per Jis per aap jis screen per transfer Hona Chahte Hain vah screen banaye. uska Naam Humne receive.dart Rakha Hua Hai aap uska naam Kuchh Bhi Rakh sakte hain.
  
  final String receivedPhone;
  final String receivedemailid;
  // ✅ Yahan phone number receive hoga

  Registerscreen({Key? key, required this.receivedPhone, required this.receivedemailid }) : super(key: key);
  
  
  Text(
                "OTP sent to ${widget.receivedPhone}",
                textAlign: TextAlign.center,
                style: TextStyle(fontSize: 16, fontWeight: FontWeight.w500),
              ),

              Text(
                "Your email id ${widget.receivedemailid}",
                textAlign: TextAlign.center,
                style: TextStyle(fontSize: 16, fontWeight: FontWeight.w500),
              ),

Code Explanation
  • First of all, import the flutter package on the i.dot screen and then a text field is created in which input is taken from the user.
  • The text field is decorated with different properties.
  • A button is placed above the text field. On clicking the button, the value is passed through the navigator.push and is transferred from one screen to another.
  • To receive the value of main.dart, we have created a screen named receive.dart in which we receive the data from the base in variable form and after receiving it, we display it in text.






Post a Comment

0 Comments