Skip to content

Commit

Permalink
Merge pull request #1 from tushar725mittal/questionnaires
Browse files Browse the repository at this point in the history
  • Loading branch information
2 parents ad1b0cf + b8166e0 commit 7560f64
Show file tree
Hide file tree
Showing 6 changed files with 198 additions and 22 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
# The .vscode folder contains launch configuration and tasks you configure in
# VS Code which you may wish to be included in version control, so this line
# is commented out by default.
#.vscode/
.vscode/

# Flutter/Dart/Pub related
**/doc/api/
Expand Down
10 changes: 5 additions & 5 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'package:fin/pages/home_page.dart';
import 'package:fin/pages/journal.dart';
import 'package:fin/pages/login.dart';
import 'package:fin/pages/profile.dart';
import 'package:fin/pages/questionnaires.dart';
import 'package:fin/pages/questionnaire/questionnaire.dart';
import 'package:fin/pages/recreation.dart';
import 'package:fin/pages/password_reset.dart';
import 'package:fin/pages/signup.dart';
Expand Down Expand Up @@ -62,16 +62,16 @@ class MyApp extends StatelessWidget {
MyRoutes.chatRoomRoute: (_, __) =>
const MaterialPage(child: ChatRoom()),
MyRoutes.questionnairesRoute: (_, __) =>
const MaterialPage(child: Questionnaires()),
const MaterialPage(child: Questionnaire()),
MyRoutes.therapistRoute: (_, __) =>
const MaterialPage(child: Therapist()),
MyRoutes.journalRoute: (_, __) =>
const MaterialPage(child: Journal()),
MyRoutes.recreationRoute: (_, __) =>
const MaterialPage(child: Recreation()),
MyRoutes.innerchatroomRoute: (_, params) =>MaterialPage(
child: InnerChat(
code:params['code'],
MyRoutes.innerchatroomRoute: (_, params) => MaterialPage(
child: InnerChat(
code: params['code'],
)),
},
),
Expand Down
20 changes: 16 additions & 4 deletions lib/pages/home_page.dart
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"),
),
),
);
}
}
}
144 changes: 144 additions & 0 deletions lib/pages/questionnaire/questionnaire.dart
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,
)
],
)),
),
),
),
),
);
}
}
32 changes: 32 additions & 0 deletions lib/pages/questionnaire/radio_group.dart
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(),
);
}
}
12 changes: 0 additions & 12 deletions lib/pages/questionnaires.dart

This file was deleted.

0 comments on commit 7560f64

Please sign in to comment.