From e1f8ad1466e531da2358de22be08a828eaf93179 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 25 Apr 2020 17:28:13 +0200 Subject: [PATCH] Do not render survey pie chart which have not response --- lib/models/survey.dart | 3 +++ lib/ui/widgets/survey_widget.dart | 23 +++++++++++++++++------ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/lib/models/survey.dart b/lib/models/survey.dart index 6f5bf11..3bb7b32 100644 --- a/lib/models/survey.dart +++ b/lib/models/survey.dart @@ -33,6 +33,9 @@ class Survey { bool get hasResponded => this.userChoice != null && this.userChoice > 0; + bool get hasResponses => + this.choices.where((f) => f.responses > 0).length > 0; + SurveyChoice get userResponse { if (!hasResponded) return null; diff --git a/lib/ui/widgets/survey_widget.dart b/lib/ui/widgets/survey_widget.dart index b31e4a5..12204a1 100644 --- a/lib/ui/widgets/survey_widget.dart +++ b/lib/ui/widgets/survey_widget.dart @@ -42,9 +42,7 @@ class _SurveyWidgetState extends State { style: TextStyle(fontWeight: FontWeight.bold), ), _buildUserResponse(), - PieChart( - dataMap: _buildDataMap(), - ), + survey.hasResponses ? _buildChart() : _buildNoResponseNotice(), ], ); } @@ -98,15 +96,28 @@ class _SurveyWidgetState extends State { items: survey.choices .map( (f) => DropdownMenuItem( - value: f, - child: Text(f.name), - ), + value: f, + child: Text(f.name), + ), ) .toList(), onChanged: _respondToSurvey, ); } + Widget _buildChart() { + return PieChart( + dataMap: _buildDataMap(), + ); + } + + Widget _buildNoResponseNotice() { + return Padding( + padding: const EdgeInsets.all(16.0), + child: Text(tr("No response yet to this survey.")), + ); + } + /// Respond to survey Future _respondToSurvey(SurveyChoice choice) async { // Send the response to the server