import 'package:flutter/material.dart';
import 'package:get/get.dart';
// Entry point of the Flutter application
void main() {
runApp(MyApp());
}
// Root widget of the application
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return GetMaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue, // Theme color
),
home: Page1(), // Initial screen of the app
debugShowCheckedModeBanner: false,
);
}
}
// First page of the application
class Page1 extends StatefulWidget {
@override
_Page1State createState() => _Page1State();
}
class _Page1State extends State<Page1> {
String? x; // Variable to store data returned from Page2
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("GeekforGeeks GFG"),
centerTitle: true, // Centers the title
backgroundColor: Colors.green,
foregroundColor: Colors.white,
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center, // Centers content vertically
children: [
Text(
"Page 1",
textScaleFactor: 2, // Scales the text size
),
Container(
child: ElevatedButton(
style: ElevatedButton.styleFrom(
backgroundColor: Colors.green,
foregroundColor: Colors.white,
),
child: Text("Navigate to next screen"),
onPressed: () async {
// Navigates to Page2 and waits for the result
x = await Get.to(Page2());
setState(() {
// Updates the UI with the returned data
});
},
),
),
Text(
x ?? x.toString(), // Displays the returned data or null
textScaleFactor: 2, // Scales the text size
),
],
),
),
);
}
}
// Second page of the application
class Page2 extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("GeekforGeeks GFG"),
backgroundColor: Colors.green,
foregroundColor: Colors.white,
),
body: Center(
child: Column(
mainAxisAlignment:MainAxisAlignment.center, // Centers content vertically
children: [
Text(
"Page 2",
textScaleFactor: 2, // Scales the text size
),
Container(
child: ElevatedButton(
style: ElevatedButton.styleFrom(
backgroundColor: Colors.green,
foregroundColor: Colors.white,
),
child: Text("Navigate to previous screen"),
onPressed:
() => Get.back(
result:
"Data after returning to first page", // Data sent back to Page1
),
),
),
],
),
),
);
}
}