Skip to content

Instantly share code, notes, and snippets.

@nyx-code

nyx-code/main.dart

Created Oct 18, 2020
Embed
What would you like to do?
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Study Planner App',
home: MainScreen(),
);
}
}
List data = [];
class MainScreen extends StatefulWidget {
@override
_MainScreenState createState() => _MainScreenState();
}
class _MainScreenState extends State<MainScreen> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Study Planner App"),
),
body: SafeArea(
child: Padding(
padding: EdgeInsets.all(16),
child: data.length != 0
? ListView.builder(
itemBuilder: (context, index) {
return ListTile(
onTap: () {
showDialog(
context: context,
child: AlertDialog(
content: Text(data[index]["subject"]),
),
);
print(data[index]["subject"]);
},
onLongPress: () {
data.removeAt(index);
setState(() {});
print("delete Subject $index");
},
title: Text(data[index]["subject"]),
subtitle: Text(data[index]["topic"]),
);
},
itemCount: data.length,
)
: Center(child: Text("No data Available")),
)),
floatingActionButton: FloatingActionButton(
onPressed: () {
//TODO: Go to Add screen
Navigator.of(context).push(MaterialPageRoute(builder: (context) {
return AddScreen();
}));
print("Click Add");
},
child: Icon(Icons.add),
),
);
}
}
class AddScreen extends StatefulWidget {
@override
_AddScreenState createState() => _AddScreenState();
}
class _AddScreenState extends State<AddScreen> {
final _formKey = GlobalKey<FormState>();
String subjectName, topicName;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Add Subject"),
),
body: SafeArea(
child: Padding(
padding: EdgeInsets.all(16),
child: Form(
key: _formKey,
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: [
TextFormField(
decoration: InputDecoration(
labelText: "Subject Name",
),
validator: (value) {
if (value.isEmpty) return "Please enter subject name";
return null;
},
onSaved: (value) {
setState(() {
subjectName = value;
});
},
),
TextFormField(
decoration: InputDecoration(
labelText: "Topic Name",
),
validator: (value) {
if (value.isEmpty) return "Please enter topic name";
return null;
},
onSaved: (value) {
setState(() {
topicName = value;
});
},
),
ElevatedButton(
onPressed: () {
//TODO: Submit form
if (_formKey.currentState.validate()) {
_formKey.currentState.save();
data.add({
"subject": subjectName,
"topic": topicName,
});
Navigator.of(context)
.push(MaterialPageRoute(builder: (context) {
return MainScreen();
}));
}
},
child: Text("Submit"),
),
],
),
),
)),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.