Skip to content

Instantly share code, notes, and snippets.

@Vatsal596
Last active March 28, 2023 18:22
Show Gist options
  • Save Vatsal596/3407cd2c85c277d18a098ae07215e991 to your computer and use it in GitHub Desktop.
Save Vatsal596/3407cd2c85c277d18a098ae07215e991 to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
import 'package:flutter_application_5/SecondPage.dart';
import 'package:flutter_application_5/SharedData.dart';
import 'package:flutter_application_5/student.dart';
void main() {
runApp(MaterialApp(
home: DemoClass()
));
}
class DemoClass extends StatelessWidget {
const DemoClass({super.key});
@override
Widget build(BuildContext context) {
return DefaultTabController(
length: 3,
child: Scaffold(
appBar: AppBar(
backgroundColor: Colors.red,
title: Text("TabDemo"),
bottom: TabBar(tabs: [
Tab(
icon: Icon(Icons.add),
text: "Add",
),
Tab(
icon: Icon(Icons.home),
text: "Home",
),
Tab(
icon: Icon(Icons.view_agenda_outlined),
text: "View",
),
]),
),
body: TabBarView(children: [
DemoTabPage1(),
DemoPage2(),
ViewPage(),
]),
),
);
}
}
class MyApp1 extends StatelessWidget {
MyApp1({Key? key}) : super(key: key);
TextEditingController txtName = new TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Demo"),
),
body: Column(
children: [
TextField(
controller: txtName,
),
SizedBox(
height: 10,
),
ElevatedButton(
onPressed: () {
SharedData.stGlobal.add(txtName.text);
},
child: Text("Save")
)
],
),
floatingActionButton: FloatingActionButton(
onPressed: () {
SharedData.name = txtName.text;
Navigator.push(context, MaterialPageRoute(
builder: (context) {
return SecondPage();
},
));
},
child: Icon(Icons.next_plan_outlined),
),
);
}
}
class DemoTabPage1 extends StatelessWidget {
DemoTabPage1({super.key});
TextEditingController txtName = new TextEditingController();
TextEditingController txtNo = new TextEditingController();
TextEditingController txtMarks1 = new TextEditingController();
TextEditingController txtMarks2 = new TextEditingController();
@override
Widget build(BuildContext context) {
return Container(
child: Column(
children: [
TextField(
controller: txtName,
decoration: InputDecoration(labelText: "Enter Name"),
),
TextField(
controller: txtNo,
decoration: InputDecoration(labelText: "Enter No"),
),
TextField(
controller: txtMarks1,
decoration: InputDecoration(labelText: "Enter Marks"),
),
TextField(
controller: txtMarks2,
decoration: InputDecoration(labelText: "Enter Marks2"),
),
ElevatedButton(
child: Text("Save"),
onPressed: () {
Student newStu = new Student();
newStu.name = txtName.text;
newStu.no = txtNo.text;
newStu.marks1 = int.parse(txtMarks1.text);
newStu.marks2 = int.parse(txtMarks2.text);
SharedData.stStudent.add(newStu);
print(SharedData.stStudent.length);
},
),
],
),
);
}
}
class DemoPage2 extends StatefulWidget {
const DemoPage2({Key? key}) : super(key: key);
@override
State<DemoPage2> createState() => DemoPage2State();
}
class DemoPage2State extends State<DemoPage2> {
String str = "";
TextEditingController txtNo = new TextEditingController();
@override
Widget build(BuildContext context) {
return Container(
child: Column(
children: [
TextField(
controller: txtNo,
decoration: InputDecoration(labelText: "Enter No"),
),
ElevatedButton(
onPressed: () {
for (int cntr = 0;
cntr < SharedData.stStudent.length;
cntr++) {
if (SharedData.stStudent[cntr].no == txtNo.text) {
print(SharedData.stStudent[cntr].name);
str = SharedData.stStudent[cntr].name;
SharedData.stStudent.removeAt(cntr);
}
}
setState(() {});
},
child: Text("Show Marks")
),
Text(str)
],
),
);
}
}
class ViewPage extends StatelessWidget {
const ViewPage({super.key});
@override
Widget build(BuildContext context) {
return Container(
child: ListView.builder(
itemCount: SharedData.stStudent.length,
itemBuilder:(context, index) {
return ListTile(
onTap: () {
SharedData.selectedRecord = index;
Navigator.push(context, MaterialPageRoute(
builder: (context) {
return DetailView();
},
));
},
title: Text(SharedData.stStudent[index].name),
);
} ,
),
);
}
}
class DetailView extends StatelessWidget {
const DetailView({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Detail View")
),
body: Column(children: [
Text(SharedData.stStudent[SharedData.selectedRecord].name),
Text(SharedData.stStudent[SharedData.selectedRecord].no),
Text(SharedData.stStudent[SharedData.selectedRecord].marks1.toString()),
Text(SharedData.stStudent[SharedData.selectedRecord].marks2.toString()),
]),
);
}
}
import 'package:flutter/material.dart';
import 'package:flutter_application_5/SharedData.dart';
class SecondPage extends StatelessWidget {
const SecondPage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.red,
appBar: AppBar(
title: Text("Second Page"),
),
body: ListView.builder(
itemCount: SharedData.stGlobal.length,
itemBuilder: (context, index) {
return ListTile(
title: (
Text(SharedData.stGlobal[index])
),
);
} ,
),
);
}
}
import 'package:flutter_application_5/student.dart';
class SharedData {
static String name = "";
static List<String> stGlobal = [];
static int selectedRecord = 0;
static List<Student> stStudent = [];
}
class Student {
String name = "";
String no = "";
int marks1 = 0;
int marks2 = 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment