Created
October 18, 2020 07:44
-
-
Save nyx-code/74bf62f0a53668e5d1b531163834d1c7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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