Tuesday, March 9, 2021

Flutter: Send Data between Pages

 



Method 1

 import 'package:flutter/material.dart';  
 void main() {  
  runApp(MaterialApp(  
   title: 'Flutter',  
   home: FirstScreen(),  
  ));  
 }  
 class FirstScreen extends StatefulWidget {  
  @override  
  _FirstScreenState createState() {  
   return _FirstScreenState();  
  }  
 }  
 class _FirstScreenState extends State<FirstScreen> {  
  // this allows us to access the TextField text  
  TextEditingController textFieldController = TextEditingController();  
  @override  
  Widget build(BuildContext context) {  
   return Scaffold(  
    appBar: AppBar(title: Text('First screen')),  
    body: Column(  
     mainAxisAlignment: MainAxisAlignment.center,  
     children: [  
      Padding(  
       padding: const EdgeInsets.all(32.0),  
       child: TextField(  
        controller: textFieldController,  
        style: TextStyle(  
         fontSize: 24,  
         color: Colors.black,  
        ),  
       ),  
      ),  
      RaisedButton(  
       child: Text(  
        'Go to second screen',  
        style: TextStyle(fontSize: 24),  
       ),  
       onPressed: () {  
        _sendDataToSecondScreen(context);  
       },  
      )  
     ],  
    ),  
   );  
  }  
  // get the text in the TextField and start the Second Screen  
  void _sendDataToSecondScreen(BuildContext context) {  
   String textToSend = textFieldController.text;  
   Navigator.push(  
     context,  
     MaterialPageRoute(  
      builder: (context) => SecondScreen(text: textToSend,),  
     ));  
  }  
 }  
 class SecondScreen extends StatelessWidget {  
  final String text;  
  // receive data from the FirstScreen as a parameter  
  SecondScreen({Key key, @required this.text}) : super(key: key);  
  @override  
  Widget build(BuildContext context) {  
   return Scaffold(  
    appBar: AppBar(title: Text('Second screen')),  
    body: Center(  
     child: Text(  
      text,  
      style: TextStyle(fontSize: 24),  
     ),  
    ),  
   );  
  }  
 }  



Method 2