Data passing from One page to Another

 import "package:flutter/material.dart";

/// EXAMPLE 1 : Using StatelessWidget as shown below
// void main()
// {
// runApp(MyApp());
// }
// class MyApp extends StatelessWidget
// {
// MyApp({super.key});
//
// @override
// Widget build(BuildContext context)
// {
// return MaterialApp(
// title: "Data Passing through TextField",
// debugShowCheckedModeBanner: false,
// theme: ThemeData(
// primarySwatch: Colors.brown,
// ),
// home: FirstDemo(),
// );
// }
// }
//
// class FirstDemo extends StatefulWidget
// {
// @override
// State<FirstDemo> createState()
// {
// return FirstDemoState();
// }
// }
//
// class FirstDemoState extends State<FirstDemo>
// {
// TextEditingController txt1 = new TextEditingController();
// TextEditingController txt2 = new TextEditingController();
// TextEditingController txt3 = new TextEditingController();
//
// @override
// Widget build(BuildContext context)
// {
// return Scaffold(
// appBar: AppBar(
// title: const Text("This is First Page"),
// centerTitle: true,
// ),
// body: Center(
// child: Container(
// width: 330.0,
// child: Column(
// mainAxisAlignment: MainAxisAlignment.center,
// children: [
// TextField(
// controller: txt1,
// decoration: InputDecoration(
// hintText: "Enter the Email",
// labelText: "Email",
// border: OutlineInputBorder(),
// ),
// ),
// const SizedBox(height: 20.0),
// TextField(
// controller: txt2,
// decoration: InputDecoration(
// hintText: "Enter the Username",
// labelText: "Username",
// border: OutlineInputBorder(),
// ),
// ),
// const SizedBox(height: 20.0),
// TextField(
// controller: txt3,
// decoration: InputDecoration(
// hintText: "Enter the Password",
// labelText: "Password",
// border: OutlineInputBorder(),
// ),
// ),
// const SizedBox(height: 30.0),
// ElevatedButton(
// onPressed: (){
// Navigator.push(
// context,
// MaterialPageRoute(
// builder: (context){
// return SecondDemo(
// username: txt1.text,
// email: txt2.text,
// password: txt3.text,
// );
// },
// ),
// );
// },
// child: const Text("Go to Next Page"),
// ),
// ],
// ),
// ),
// ),
// );
// }
// }
//
// class SecondDemo extends StatelessWidget
// {
// final String username, email, password;
// SecondDemo({super.key, required this.username, required this.email, required this.password});
//
// @override
// Widget build(BuildContext context)
// {
// return Scaffold(
// appBar: AppBar(
// title: const Text("This is Second page"),
// centerTitle: true,
// ),
// body: Center(
// child: Column(
// mainAxisAlignment: MainAxisAlignment.center,
// children: [
// Text("Your details are here :",
// style: TextStyle(
// fontSize: 40.0,
// fontWeight: FontWeight.bold,
// ),
// ),
// const SizedBox(height: 30.0),
// Row(
// mainAxisAlignment: MainAxisAlignment.center,
// children: [
// Text("Your Username is : ",
// style: TextStyle(
// fontWeight: FontWeight.bold,
// fontSize: 20.0,
// ),
// ),
// Text("${username}",
// style: TextStyle(fontSize: 20.0,),
// ),
// ],
// ),
// const SizedBox(height: 30.0),
// Row(
// mainAxisAlignment: MainAxisAlignment.center,
// children: [
// Text("Your Email is : ",
// style: TextStyle(
// fontWeight: FontWeight.bold,
// fontSize: 20.0,
// ),
// ),
// Text("${email}",
// style: TextStyle(fontSize: 20.0),
// ),
// ],
// ),
// const SizedBox(height: 30.0),
// Row(
// mainAxisAlignment: MainAxisAlignment.center,
// children: [
// Text("Your Password is : ",
// style: TextStyle(
// fontWeight: FontWeight.bold,
// fontSize: 20.0,
// ),
// ),
// Text("${password}",
// style: TextStyle(fontSize: 20.0),
// ),
// ],
// ),
// ],
// ),
// ),
// );
// }
// }


/// EXAMPLE 2 : Using StatefulWidget as shown below
void main()
{
runApp(MyApp());
}
class MyApp extends StatelessWidget
{
MyApp({super.key});

@override
Widget build(BuildContext context)
{
return MaterialApp(
title: "Data passing",
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.green,
),
home: FirstDemo(),
);
}
}
class FirstDemo extends StatefulWidget
{
@override
State<FirstDemo> createState()
{
return FirstDemoState();
}
}
class FirstDemoState extends State<FirstDemo>
{
TextEditingController txt1 = new TextEditingController();
TextEditingController txt2 = new TextEditingController();
TextEditingController txt3 = new TextEditingController();

@override
Widget build(BuildContext context)
{
return GestureDetector(
onTap: (){
return FocusManager.instance.primaryFocus?.unfocus();
},
child: Scaffold(
appBar: AppBar(
title: const Text("This is First Page"),
centerTitle: true,
),
body: Center(
child: Container(
width: 300.0,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
TextField(
controller: txt1,
decoration: InputDecoration(
hintText: "Enter the Information 1",
labelText: "Info 1",
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(11.0),
),
),
),
const SizedBox(height: 30.0),
TextField(
controller: txt2,
decoration: InputDecoration(
hintText: "Enter the Information 2",
labelText: "Info 2",
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(11.0),
),
),
),
const SizedBox(height: 30.0),
TextField(
controller: txt3,
decoration: InputDecoration(
hintText: "Enter the Information 3",
labelText: "Info 3",
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(11.0),
),
),
),
const SizedBox(height: 30.0),
ElevatedButton(
onPressed: (){
Navigator.push(
context,
MaterialPageRoute(
builder: (context){
return SecondDemo(
txt1.text.toString(),
txt2.text.toString(),
txt3.text.toString()
);
},
),
);
},
child: const Text("Go to Next Page"),
),
],
),
),
),
),
);
}
}

class SecondDemo extends StatefulWidget
{
String info1, info2, info3;
SecondDemo(this.info1, this.info2, this.info3);

@override
State<SecondDemo> createState() => _SecondDemoState();
}

class _SecondDemoState extends State<SecondDemo> {
@override
Widget build(BuildContext context)
{
return Scaffold(
appBar: AppBar(
title: const Text("This is Second Page"),
centerTitle: true,
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text("First Information is : ${widget.info1}",
style: TextStyle(fontSize: 20.0, fontWeight: FontWeight.bold),),
const SizedBox(height: 20.0),
Text("Second Information is : ${widget.info2}",
style: TextStyle(fontSize: 20.0, fontWeight: FontWeight.bold),),
const SizedBox(height: 20.0),
Text("Third Information is : ${widget.info3}",
style: TextStyle(fontSize: 20.0, fontWeight: FontWeight.bold),),
],
),
),
);
}
}

Comments

Popular posts from this blog

Pagination with Bloc Pattern in Flutter

Pagination First Practical in Flutter

ExpansionPanel with ExpansionPanelList with Complete Collapse Operation in Flutter