-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from tushar725mittal/questionnaires
- Loading branch information
Showing
6 changed files
with
198 additions
and
22 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,24 @@ | ||
import 'package:fin/utils/routes.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:velocity_x/velocity_x.dart'; | ||
|
||
class HomePage extends StatelessWidget { | ||
const HomePage({ Key? key }) : super(key: key); | ||
const HomePage({Key? key}) : super(key: key); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Container( | ||
child: Center(child: Text("This is home page"),), | ||
return Scaffold( | ||
appBar: AppBar( | ||
title: const Text("Friend In Need 2.0"), | ||
), | ||
body: Center( | ||
child: ElevatedButton( | ||
onPressed: () { | ||
context.vxNav.push(Uri.parse(MyRoutes.questionnairesRoute)); | ||
}, | ||
child: const Text("Questions"), | ||
), | ||
), | ||
); | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
import 'package:cloud_firestore/cloud_firestore.dart'; | ||
import 'package:fin/pages/questionnaire/radio_group.dart'; | ||
import 'package:firebase_auth/firebase_auth.dart'; | ||
import 'package:flutter/material.dart'; | ||
|
||
class Questionnaire extends StatelessWidget { | ||
const Questionnaire({Key? key}) : super(key: key); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Scaffold( | ||
appBar: AppBar( | ||
title: const Text("Questionnaire"), | ||
), | ||
body: const QuestionnaireForm(), | ||
); | ||
} | ||
} | ||
|
||
class QuestionnaireForm extends StatefulWidget { | ||
const QuestionnaireForm({Key? key}) : super(key: key); | ||
|
||
@override | ||
State<QuestionnaireForm> createState() => _QuestionnaireFormState(); | ||
} | ||
|
||
class _QuestionnaireFormState extends State<QuestionnaireForm> { | ||
final _key = GlobalKey<FormState>(); | ||
final questions = { | ||
'How is life?': [ | ||
'Yes', | ||
'Maybe', | ||
'No', | ||
], | ||
'Are you sad?': [ | ||
'Yes', | ||
'Maybe', | ||
'No', | ||
], | ||
'Are you very sad?': [ | ||
'Yes', | ||
'Maybe', | ||
'No', | ||
], | ||
}; | ||
|
||
Map<int, String?> selectedOptions = {}; | ||
|
||
void _submit() async { | ||
var _ref = FirebaseFirestore.instance; | ||
var user = FirebaseAuth.instance.currentUser; | ||
|
||
Map<String, String?> response = {}; | ||
|
||
for (int i = 0; i < questions.length; i++) { | ||
String question = questions.keys.elementAt(i); | ||
response[question] = selectedOptions[i]; | ||
} | ||
|
||
if (user != null) { | ||
ScaffoldMessenger.of(context).showSnackBar( | ||
const SnackBar( | ||
content: Text("Submitting..."), | ||
), | ||
); | ||
await _ref | ||
.collection('users') | ||
.doc(user.uid) | ||
.collection('questionnaireResponses') | ||
.add(response); | ||
|
||
ScaffoldMessenger.of(context).hideCurrentSnackBar(); | ||
ScaffoldMessenger.of(context).showSnackBar( | ||
SnackBar( | ||
content: const Text("Submitted. Thank you!"), | ||
action: SnackBarAction( | ||
label: "Dismiss", | ||
onPressed: () => | ||
ScaffoldMessenger.of(context).hideCurrentSnackBar()), | ||
), | ||
); | ||
} else { | ||
ScaffoldMessenger.of(context).showSnackBar( | ||
const SnackBar( | ||
content: Text("You must login before submitting the form!"), | ||
), | ||
); | ||
} | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Form( | ||
key: _key, | ||
child: Scrollbar( | ||
child: ListView.separated( | ||
itemCount: questions.length + 1, | ||
separatorBuilder: (context, separatorIndex) => const Divider( | ||
indent: 20, | ||
endIndent: 20, | ||
), | ||
itemBuilder: (context, questionIndex) => | ||
questionIndex != questions.length | ||
? Column( | ||
crossAxisAlignment: CrossAxisAlignment.start, | ||
children: <Widget>[ | ||
ListTile( | ||
title: Text("Q${questionIndex + 1}. " + | ||
questions.keys.elementAt(questionIndex)), | ||
), | ||
RadioGroup( | ||
options: questions.values.elementAt(questionIndex), | ||
currentlySelected: selectedOptions[questionIndex], | ||
onChanged: (value) { | ||
setState(() { | ||
selectedOptions[questionIndex] = value; | ||
}); | ||
}, | ||
), | ||
], | ||
) | ||
: Padding( | ||
padding: const EdgeInsets.symmetric(vertical: 20), | ||
child: Center( | ||
child: ElevatedButton( | ||
onPressed: _submit, | ||
child: Row( | ||
mainAxisSize: MainAxisSize.min, | ||
children: const [ | ||
Text("Submit"), | ||
SizedBox(width: 7), | ||
Icon( | ||
Icons.arrow_forward, | ||
size: 20, | ||
) | ||
], | ||
)), | ||
), | ||
), | ||
), | ||
), | ||
); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import 'package:flutter/material.dart'; | ||
|
||
class RadioGroup extends StatelessWidget { | ||
final List<String> options; | ||
final String? currentlySelected; | ||
final Function(String?) onChanged; | ||
|
||
const RadioGroup({ | ||
Key? key, | ||
required this.options, | ||
required this.currentlySelected, | ||
required this.onChanged, | ||
}) : super(key: key); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Column( | ||
children: options | ||
.map( | ||
(option) => RadioListTile( | ||
title: Text(option), | ||
value: option, | ||
groupValue: currentlySelected, | ||
onChanged: (String? value) { | ||
onChanged(value!); | ||
}, | ||
), | ||
) | ||
.toList(), | ||
); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.