mirror of
				https://gitlab.com/comunic/comunicmobile
				synced 2025-11-04 12:14:11 +00:00 
			
		
		
		
	Add posts with surveys support
This commit is contained in:
		
							
								
								
									
										121
									
								
								lib/ui/widgets/survey_widget.dart
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										121
									
								
								lib/ui/widgets/survey_widget.dart
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,121 @@
 | 
			
		||||
import 'package:comunic/helpers/survey_helper.dart';
 | 
			
		||||
import 'package:comunic/models/survey.dart';
 | 
			
		||||
import 'package:comunic/models/survey_choice.dart';
 | 
			
		||||
import 'package:comunic/utils/intl_utils.dart';
 | 
			
		||||
import 'package:comunic/utils/ui_utils.dart';
 | 
			
		||||
import 'package:flutter/material.dart';
 | 
			
		||||
import 'package:pie_chart/pie_chart.dart';
 | 
			
		||||
 | 
			
		||||
/// Survey widget
 | 
			
		||||
///
 | 
			
		||||
/// @author Pierre HUBERT
 | 
			
		||||
 | 
			
		||||
class SurveyWidget extends StatefulWidget {
 | 
			
		||||
  final Survey survey;
 | 
			
		||||
 | 
			
		||||
  const SurveyWidget({Key key, @required this.survey})
 | 
			
		||||
      : assert(survey != null),
 | 
			
		||||
        super(key: key);
 | 
			
		||||
 | 
			
		||||
  @override
 | 
			
		||||
  _SurveyWidgetState createState() => _SurveyWidgetState();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
class _SurveyWidgetState extends State<SurveyWidget> {
 | 
			
		||||
  final SurveyHelper _helper = SurveyHelper();
 | 
			
		||||
 | 
			
		||||
  Survey get survey => widget.survey;
 | 
			
		||||
 | 
			
		||||
  Map<String, double> _buildDataMap() {
 | 
			
		||||
    Map<String, double> data = Map();
 | 
			
		||||
    widget.survey.choices.forEach((e) => data.putIfAbsent(
 | 
			
		||||
        e.name + " (" + e.responses.toString() + ")", () => 1.0 * e.responses));
 | 
			
		||||
    return data;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  @override
 | 
			
		||||
  Widget build(BuildContext context) {
 | 
			
		||||
    return Column(
 | 
			
		||||
      children: <Widget>[
 | 
			
		||||
        Text(
 | 
			
		||||
          widget.survey.question,
 | 
			
		||||
          style: TextStyle(fontWeight: FontWeight.bold),
 | 
			
		||||
        ),
 | 
			
		||||
        _buildUserResponse(),
 | 
			
		||||
        PieChart(
 | 
			
		||||
          dataMap: _buildDataMap(),
 | 
			
		||||
        ),
 | 
			
		||||
      ],
 | 
			
		||||
    );
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  Widget _buildUserResponse() {
 | 
			
		||||
    if (survey.hasResponded) return _buildUserRespondedWidget();
 | 
			
		||||
    return _buildUserResponseFormWidget();
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  Widget _buildUserRespondedWidget() {
 | 
			
		||||
    return Row(
 | 
			
		||||
      mainAxisAlignment: MainAxisAlignment.center,
 | 
			
		||||
      children: <Widget>[
 | 
			
		||||
        Flexible(
 | 
			
		||||
          child: Text(
 | 
			
		||||
            tr("Your response: %response%",
 | 
			
		||||
                args: {"response": survey.userResponse.name}),
 | 
			
		||||
            textAlign: TextAlign.center,
 | 
			
		||||
          ),
 | 
			
		||||
        ),
 | 
			
		||||
        MaterialButton(
 | 
			
		||||
          child: Text(tr("Cancel").toUpperCase()),
 | 
			
		||||
          onPressed: _cancelUserResponse,
 | 
			
		||||
        )
 | 
			
		||||
      ],
 | 
			
		||||
    );
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  Future<void> _cancelUserResponse() async {
 | 
			
		||||
    if (!await showConfirmDialog(
 | 
			
		||||
      context: context,
 | 
			
		||||
      title: tr("Cancel response to survey"),
 | 
			
		||||
      message:
 | 
			
		||||
          tr("Do you really want to cancel your response to this survey ?"),
 | 
			
		||||
    )) return;
 | 
			
		||||
 | 
			
		||||
    if (!await _helper.cancelResponse(survey)) {
 | 
			
		||||
      showSimpleSnack(
 | 
			
		||||
          context, tr("Could not cancel your response to the survey !"));
 | 
			
		||||
      return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    setState(() {
 | 
			
		||||
      survey.cancelUserResponse();
 | 
			
		||||
    });
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  Widget _buildUserResponseFormWidget() {
 | 
			
		||||
    return DropdownButton<SurveyChoice>(
 | 
			
		||||
      hint: Text(tr("Respond to survey")),
 | 
			
		||||
      items: survey.choices
 | 
			
		||||
          .map(
 | 
			
		||||
            (f) => DropdownMenuItem<SurveyChoice>(
 | 
			
		||||
                  value: f,
 | 
			
		||||
                  child: Text(f.name),
 | 
			
		||||
                ),
 | 
			
		||||
          )
 | 
			
		||||
          .toList(),
 | 
			
		||||
      onChanged: _respondToSurvey,
 | 
			
		||||
    );
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /// Respond to survey
 | 
			
		||||
  Future<void> _respondToSurvey(SurveyChoice choice) async {
 | 
			
		||||
    // Send the response to the server
 | 
			
		||||
    if (!await _helper.respondToSurvey(survey: survey, choice: choice))
 | 
			
		||||
      return showSimpleSnack(
 | 
			
		||||
          context, tr("Could not send your response to the survey!"));
 | 
			
		||||
 | 
			
		||||
    setState(() {
 | 
			
		||||
      survey.setUserResponse(choice);
 | 
			
		||||
    });
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user