Last active
November 26, 2018 11:08
-
-
Save shyjuzz/a627373c447bcfe02bb86aae9145762e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
class EventAddParticipantsUI extends StatefulWidget { | |
final registration_event.ResponseObject participant; | |
EventAddParticipantsUI(this.participant); | |
@override | |
EventAddParticipantsUIState createState() => | |
new EventAddParticipantsUIState(); | |
} | |
class EventAddParticipantsUIState extends State<EventAddParticipantsUI> { | |
list_event.ListEventQuestionsResponse _eventQuestions; | |
List<list_event.ResponseObject> _participantsInformation; | |
bool _isLoading; | |
bool _isError; | |
final _controller = new PageController(); | |
static const _kDuration = const Duration(milliseconds: 300); | |
static const _kCurve = Curves.ease; | |
List<MyDataModel> _dataModel; | |
List<Widget> _pages; | |
@override | |
void initState() { | |
super.initState(); | |
_isLoading = true; | |
_dataModel = new List<MyDataModel>(); | |
_pages = new List<Widget>(); | |
_isError = false; | |
_getData(); | |
} | |
void _getData() async { | |
try { | |
_eventQuestions = await api.getEventQuestions("PARTICIPANT", 12961); | |
if (_eventQuestions != null && _eventQuestions.responseObject != null) { | |
_participantQuestions = _eventQuestions.responseObject | |
.where((item) => item.dataContext == 'QN') | |
.toList(); | |
if (_participantQuestions != null) { | |
for (int i = 0; i < _participantQuestions.length; i++) { | |
Widget control; | |
var myModel = new MyDataModel(_participantQuestions[i]); | |
switch (myModel.response.qnMap.inputDataType.typeCode) { | |
case 'RADIO_BUTTON': | |
control = new RadioWidget(myModel); | |
break; | |
case 'TEXT': | |
control = new TextWidget(myModel); | |
break; | |
} | |
_dataModel.add(myModel); | |
_pages.add(control); | |
print('Control added $control'); | |
} | |
} | |
} | |
if (mounted) { | |
setState(() { | |
_isLoading = false; | |
}); | |
} | |
} catch (e, s) { | |
if (mounted) { | |
setState(() { | |
_isError = true; | |
_isLoading = false; | |
}); | |
print('error $e'); | |
} | |
} | |
} | |
@override | |
Widget build(BuildContext context) { | |
if (_isLoading) { | |
return Center( | |
child: CircularProgressIndicator(), | |
); | |
} | |
if (_isError) { | |
return Center( | |
child: Text('Server error!'), | |
); | |
} | |
var pageView = new PageView.builder( | |
controller: _controller, | |
itemCount: _pages.length, | |
itemBuilder: (BuildContext context, int index) { | |
return _pages[index % _pages.length]; | |
}, | |
); | |
var content = new Stack( | |
children: <Widget>[ | |
// new EventTop(""), | |
pageView, | |
new Align( | |
alignment: Alignment.bottomCenter, | |
child: new Container( | |
width: MediaQuery.of(context).size.width, | |
color: Colors.pink, | |
child: FlatButton( | |
textColor: Colors.white, | |
color: Colors.transparent, | |
onPressed: () { | |
if (_controller.page == _pages.length - 1) { | |
Scaffold.of(context) | |
.showSnackBar(new SnackBar(content: Text('Finish'))); | |
} else | |
_controller.nextPage(duration: _kDuration, curve: _kCurve); | |
//await new Future.delayed(const Duration(seconds: 2)); | |
}, | |
child: Text('Next'), | |
), | |
), | |
) | |
], | |
); | |
return content; | |
} | |
class RadioWidget extends StatefulWidget { | |
// final QnMap _item; | |
final MyDataModel _model; | |
RadioWidget(this._model); | |
@override | |
_RadioWidgetState createState() => _RadioWidgetState(); | |
} | |
class _RadioWidgetState extends State<RadioWidget> { | |
int _selected = 0; | |
List<String> _options = new List<String>(); | |
@override | |
void initState() { | |
var qnPickLists = widget._model.response.qnMap.qnPickLists as List<Object>; | |
for (int j = 0; j < qnPickLists.length; j++) { | |
var qnPickListsItem = qnPickLists[j] as LinkedHashMap<String, Object>; | |
var answer = qnPickListsItem["answerChoice"] as String; | |
_options.add(answer); | |
} | |
if (widget._model.userAnswers != null) { | |
_selected = _options.indexOf(widget._model.userAnswers[0]); | |
} | |
super.initState(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
var tiles = new List<Widget>(); | |
// var qnPickLists = widget._item.qnPickLists as List<Object>; | |
for (int j = 0; j < _options.length; j++) { | |
// var qnPickListsItem = qnPickLists[j] as LinkedHashMap<String, Object>; | |
// var answer = qnPickListsItem["answerChoice"] as String; | |
tiles.add(new RadioListTile( | |
title: Text(_options[j]), | |
value: j, | |
groupValue: _selected, | |
onChanged: (val) { | |
// widget._item. | |
widget._model.userAnswers = new List<String>(); | |
widget._model.userAnswers.add(_options[val]); | |
setState(() { | |
_selected = val; | |
}); | |
})); | |
} | |
return Container( | |
padding: new EdgeInsets.all(10.0), | |
child: Column( | |
children: <Widget>[ | |
Text(widget._model.response.qnMap.question), | |
Column( | |
children: tiles ?? <Widget>[Container()], | |
) | |
], | |
)); | |
} | |
} | |
class TextWidget extends StatefulWidget { | |
final MyDataModel _model; | |
TextWidget(this._model); | |
@override | |
TextWidgetState createState() { | |
return new TextWidgetState(); | |
} | |
} | |
class TextWidgetState extends State<TextWidget> with AutomaticKeepAliveClientMixin<TextWidget> { | |
@override | |
bool get wantKeepAlive => true; | |
// TextEditingController _controller; | |
@override | |
void dispose() { | |
print('dispose called'); | |
super.dispose(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
print(widget._model.userAnswers); | |
// _controller = new TextEditingController( | |
// text: widget._model.userAnswers == null ? '' : widget._model.userAnswers[0]); | |
print('TextWidget Build Claled'); | |
var _focusNode = new FocusNode(); | |
var _textField = new TextField( | |
// controller: _controller, | |
focusNode: _focusNode, | |
onChanged: (str) { | |
if (widget._model.userAnswers == null) { | |
widget._model.userAnswers = new List<String>(); | |
widget._model.userAnswers.add(str); | |
} else { | |
widget._model.userAnswers[0] = str; | |
} | |
print(widget._model.userAnswers); | |
}, | |
); | |
_focusNode.addListener(() { | |
print('listen'); | |
if (!_focusNode.hasFocus) { | |
print('save value'); | |
} | |
}); | |
return Container( | |
padding: new EdgeInsets.all(10.0), | |
child: Column( | |
children: <Widget>[ | |
Text(widget._model.response.qnMap.question), | |
_textField, | |
// Text(widget._item.question), | |
Column( | |
// children: tiles, | |
) | |
], | |
)); | |
} | |
} | |
class MyDataModel { | |
List<String> userAnswers; | |
list_event.ResponseObject response; | |
MyDataModel(this.response); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment