Skip to content

Instantly share code, notes, and snippets.

@lordvidex
Last active March 26, 2020 17:06
Show Gist options
  • Save lordvidex/a23d58235fc86ad5199b82c42e18c841 to your computer and use it in GitHub Desktop.
Save lordvidex/a23d58235fc86ad5199b82c42e18c841 to your computer and use it in GitHub Desktop.
Evans Owamoyo::task Day 0 track 2
import 'package:flutter/material.dart';
import 'dart:math';
Map<int, int> _answer = {};
String update = '', reason = '';
const List<String> questions = [
'How do you view ECX as a whole?',
'What track of ECX are you in?',
'Why did you join ECX?',
'What updates will you like to see?',
'Do you have a presentation for ECX Grand Finale?'
];
Map<int, List<String>> options = {
0: ['Excellent', 'Very Good', 'Good', 'Poor', 'Very Poor'],
1: [
'Back End',
'Front End',
'Mobile development',
'Engineering design',
'Soft skills',
'UI/UX design'
],
2: [
'You love coding',
'You want to learn a new skill?',
'You want some money',
'Other'
],
3: ['No updates', 'I want an update'],
4: ['Yes', 'No']
};
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext con) {
return MaterialApp(
routes: {
'fire': (_) => ResultPage(),
},
debugShowCheckedModeBanner: false,
home: Scaffold(
appBar: AppBar(
title: Text('Survey Form'),
centerTitle: true,
elevation: 0,
flexibleSpace: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: <Color>[Colors.red, Colors.blue])),
),
),
body: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: <Color>[Colors.red, Colors.blue])),
child: Center(
child: SingleChildScrollView(child: MyWidget(con)),
),
),
),
);
}
}
class MyWidget extends StatefulWidget {
final BuildContext root;
MyWidget(this.root);
@override
_MyWidgetState createState() => _MyWidgetState();
}
int page = 0;
class _MyWidgetState extends State<MyWidget> {
PageController _pageController;
_forward(BuildContext context) {
if (_answer[page] == null) {
showDialog(
context: context,
builder: (ctx) => AlertDialog(
actions: <Widget>[
FlatButton(
child: Text('OK'), onPressed: () => Navigator.of(ctx).pop())
],
title: Text('Error!'),
content: Text('Please select a value among the available options'),
),
);
return;
}
page++;
if (page == questions.length) {
page--;
Navigator.of(context).pushReplacementNamed('fire');
} else {
page = min(page, questions.length - 1);
_pageController.jumpToPage(page);
}
}
_backward() {
page--;
page = max(0, page);
_pageController.jumpToPage(page);
}
@override
void initState() {
_pageController = PageController();
super.initState();
}
@override
Widget build(BuildContext context) {
final mediaquery = MediaQuery.of(context).size;
return Column(children: <Widget>[
Align(
alignment: Alignment.centerLeft,
child: Container(
margin: EdgeInsets.only(left: 16),
child: Text('ECX Survey:', style: Theme.of(context).textTheme.title),
),
),
Center(
child: Container(
height: mediaquery.height * 0.7,
width: mediaquery.width * 0.8,
child: PageView.builder(
physics: NeverScrollableScrollPhysics(),
controller: _pageController,
itemBuilder: (ctx, int) {
return QuestionCard(int, questions[int], options[int]);
},
itemCount: questions.length),
),
),
Padding(
padding: EdgeInsets.symmetric(horizontal: 16),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
RaisedButton(onPressed: _backward, child: Text('Previous')),
RaisedButton(
onPressed: () => _forward(
context,
),
child: Text('Next'))
]),
),
]);
}
}
class QuestionCard extends StatefulWidget {
final int index;
final String question;
final List<String> options;
final bool isModifiable;
QuestionCard(this.index, this.question, this.options,
[this.isModifiable = false]);
_QuestionCard createState() => _QuestionCard();
}
class _QuestionCard extends State<QuestionCard> {
int selected;
TextEditingController _textC;
initState() {
_textC = TextEditingController();
selected = _answer[widget.index] ?? -1;
if (selected == widget.options.length - 1 &&
(widget.index == 2 || widget.index == 3)) {
widget.index == 2 ? _textC.text = reason : _textC.text = update;
}
super.initState();
}
void valandSub() {
if ((widget.index == 2 || widget.index == 3) &&
selected == widget.options.length - 1) {
if (_textC.text == '' || _textC.text == null) {
return;
}
widget.index == 2 ? reason = _textC.text : update = _textC.text;
}
}
@override
Widget build(BuildContext context) {
return Card(
child: Padding(
padding: EdgeInsets.all(16),
child: Column(children: <Widget>[
Text(
widget.question,
style: TextStyle(
fontWeight: FontWeight.bold, fontSize: 24, color: Colors.black),
),
...widget.options
.map((value) => RadioListTile(
groupValue: selected,
value: widget.options.indexOf(value),
onChanged: (r) => setState(() {
selected = r;
_answer[widget.index] = selected;
}),
title: Text(value),
))
.toList(),
if ((widget.index == 2 || widget.index == 3) &&
selected == widget.options.length - 1)
TextField(
controller: _textC,
onChanged: (str) =>
widget.index == 2 ? reason = str : update = str,
),
]),
),
);
}
}
class ResultPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Survey Form Result'),
centerTitle: true,
elevation: 0,
flexibleSpace: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: <Color>[Colors.red, Colors.blue])),
),
),
body: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: <Color>[Colors.red, Colors.blue])),
child: Center(
child: ListView.builder(
itemBuilder: (ctx, index) => AnswerItem(index),
itemCount: questions.length),
),
),
);
}
}
class AnswerItem extends StatelessWidget {
final int index;
AnswerItem(this.index);
@override
Widget build(BuildContext context) {
return ListTile(
title: Text(
questions[index],
style: TextStyle(color: Colors.white),
),
trailing: Text(
(index == 2 && _answer[index] == options[index].length - 1
? reason
: index == 3 && _answer[index] == options[index].length - 1
? update
: options[index][_answer[index]]),
style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold)),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment