Passing Data into TextField(Second page) from TextField(First page)

 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.brown,
),
home: FirstClass(),
);
}
}

class FirstClass extends StatelessWidget
{
FirstClass({super.key});

TextEditingController controller1 = new TextEditingController();
TextEditingController controller2 = new TextEditingController();

@override
Widget build(BuildContext context)
{
return GestureDetector(
onTap: () {
return FocusManager.instance.primaryFocus?.unfocus();
},
child: Scaffold(
appBar: AppBar(
title: const Text("First Class"),
centerTitle: true,
),
body: Center(
child: Container(
width: 300.0,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
TextField(
controller: controller1,
decoration: const InputDecoration(
hintText: "Enter the Name 1",
labelText: "Name 1",
),
),
const SizedBox(height: 30.0),
TextField(
controller: controller2,
decoration: const InputDecoration(
hintText: "Enter the Name 2",
labelText: "Name 2",
),
),
const SizedBox(height: 30.0),
ElevatedButton(
onPressed: (){
Navigator.push(
context,
MaterialPageRoute(
builder: (context){
return SecondClass(
controller1.text.toString(),
controller2.text.toString(),
);
},
),
);
},
child: const Text("Go to Next Page", style: TextStyle(fontWeight: FontWeight.bold),),
),
],
),
),
),
),
);
}
}

class SecondClass extends StatefulWidget
{
final String number1, number2;
SecondClass(this.number1, this.number2,{super.key});

@override
State<SecondClass> createState() {
return SecondClassState();
}
}

class SecondClassState extends State<SecondClass>
{

TextEditingController txt1 = new TextEditingController();
TextEditingController txt2 = new TextEditingController();

@override
void initState() {
super.initState();
txt1.text = widget.number1.toString();
txt2.text = widget.number2.toString();
}

@override
Widget build(BuildContext context)
{
return Scaffold(
appBar: AppBar(
title: const Text("Second Class"),
),
body: Center(
child: Container(
width: 300.0,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text("Hello : ${widget.number1} and ${widget.number2}",
style: const TextStyle(fontSize: 20.0, fontWeight: FontWeight.bold),
),
const SizedBox(height: 20.0),
TextField(
controller: txt1,
decoration: InputDecoration(
labelText: "Name1",
),
),
const SizedBox(height: 30.0),
TextField(
controller: txt2,
decoration: InputDecoration(
labelText: "Name2",
),
),
const SizedBox(height: 30.0),
ElevatedButton(
onPressed: (){
showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: const Text("DATA SAVED"),
content: const Text("Are you sure ?"),
actions: <Widget>[
TextButton(
onPressed: (){
Navigator.pop(context);
},
child: const Text("CANCEL", style: TextStyle(fontWeight: FontWeight.bold),),
),
TextButton(
onPressed: (){
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text("Data saved..."),),
);
Navigator.push(
context,
MaterialPageRoute(
builder: (context){
return FirstClass();
},
),
);
},
child: const Text("CONFIRM", style: TextStyle(fontWeight: FontWeight.bold),),
),
],
);
},
);
},
child: const Text("SAVE", style: TextStyle(fontWeight: FontWeight.bold),),
),
],
),
),
),
);
}
}

Comments

Popular posts from this blog

Pagination with Bloc Pattern in Flutter

ExpansionPanel with ExpansionPanelList with Complete Collapse Operation in Flutter

Pagination First Practical in Flutter