// ignore_for_file: prefer_const_constructors
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Geeks For Geeks Passing Data',
theme: ThemeData(
// This is the theme of your application.
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primarySwatch: Colors.green,
),
// home: const MyHomePage(title: 'Flutter Demo Home Page'),
home: PassDataDemo(),
);
}
}
class PassDataDemo extends StatefulWidget {
const PassDataDemo({Key? key}) : super(key: key);
@override
State<PassDataDemo> createState() => _PassDataDemoState();
}
class _PassDataDemoState extends State<PassDataDemo> {
final myController = TextEditingController();
@override
void dispose() {
// Clean up the controller
// when the widget is disposed.
myController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Geeks For Geeks"),
),
body: Center(
child: Column(
children: [
SizedBox(
height: 50,
),
Container(
height: 50,
width: 300,
margin: EdgeInsets.all(8),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
border: Border.all(color: Colors.black, width: 1),
),
child: TextField(
controller: myController,
decoration: InputDecoration(
labelText: 'Enter The Data',
border: InputBorder.none,
floatingLabelBehavior: FloatingLabelBehavior.never,
),
),
),
SizedBox(
height: 30,
),
// Step 1 <-- SEE HERE
ElevatedButton(
onPressed: () {
// Step 3 <-- SEE HERE
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
DetailScreen(title: myController.text),
),
);
},
child: const Text(
'Pass Data To Next Screen',
style: TextStyle(fontSize: 24),
),
),
],
),
),
);
}
}
class DetailScreen extends StatefulWidget {
// In the constructor, require a Todo.
const DetailScreen({Key? key, required this.title}) : super(key: key);
// Step 2 <-- SEE HERE
final String title;
@override
State<DetailScreen> createState() => _DetailScreenState();
}
class _DetailScreenState extends State<DetailScreen> {
@override
Widget build(BuildContext context) {
// Use the Todo to create the UI.
return Scaffold(
appBar: AppBar(
title: Text("Screen Two"),
),
body: Center(
child: Column(
children: [
SizedBox(
height: 50,
),
// Step 4 <-- SEE HERE
Text(
'${widget.title}',
style: TextStyle(fontSize: 54),
),
ElevatedButton(
onPressed: () {
Navigator.pop(context);
},
child: Text("Write New Text"),
)
],
),
),
);
}
}