|  | 
|  | 1 | +import 'dart:convert'; | 
|  | 2 | + | 
|  | 3 | +import 'package:flutter_background_geolocation/flutter_background_geolocation.dart' | 
|  | 4 | +    as bg; | 
|  | 5 | +import 'package:flutter/material.dart'; | 
|  | 6 | + | 
|  | 7 | +import '../app_localizations.dart'; | 
|  | 8 | +import '../components/banner_image.dart'; | 
|  | 9 | +import '../components/survey_tile.dart'; | 
|  | 10 | +import '../mocks/mock_survey.dart'; | 
|  | 11 | +import '../models/survey.dart'; | 
|  | 12 | +import '../models/custom_locations.dart'; | 
|  | 13 | +import '../ui/web_view.dart'; | 
|  | 14 | +import '../styles.dart'; | 
|  | 15 | + | 
|  | 16 | +const BannerImageHeight = 300.0; | 
|  | 17 | +const BodyVerticalPadding = 20.0; | 
|  | 18 | +const FooterHeight = 100.0; | 
|  | 19 | + | 
|  | 20 | +class SurveyDetail extends StatefulWidget { | 
|  | 21 | +  final int surveyID; | 
|  | 22 | + | 
|  | 23 | +  SurveyDetail(this.surveyID); | 
|  | 24 | + | 
|  | 25 | +  @override | 
|  | 26 | +  _SurveyDetailState createState() => _SurveyDetailState(surveyID); | 
|  | 27 | +} | 
|  | 28 | + | 
|  | 29 | +class _SurveyDetailState extends State<SurveyDetail> { | 
|  | 30 | +  final int surveyID; | 
|  | 31 | +  Survey survey = Survey.blank(); | 
|  | 32 | +  bool consent = false; | 
|  | 33 | + | 
|  | 34 | +  _SurveyDetailState(this.surveyID); | 
|  | 35 | + | 
|  | 36 | +  @override | 
|  | 37 | +  void initState() { | 
|  | 38 | +    super.initState(); | 
|  | 39 | +    loadData(); | 
|  | 40 | +  } | 
|  | 41 | + | 
|  | 42 | +  @override | 
|  | 43 | +  Widget build(BuildContext context) { | 
|  | 44 | +    return Scaffold( | 
|  | 45 | +      appBar: AppBar( | 
|  | 46 | +          title: Text( | 
|  | 47 | +              AppLocalizations.of(context)!.translate("about_the_project"))), | 
|  | 48 | +      body: Stack( | 
|  | 49 | +        children: [ | 
|  | 50 | +          _renderBody(context, survey), | 
|  | 51 | +          _renderFooter(context), | 
|  | 52 | +          //_renderConsentForm(), | 
|  | 53 | +        ], | 
|  | 54 | +      ), | 
|  | 55 | +    ); | 
|  | 56 | +  } | 
|  | 57 | + | 
|  | 58 | +  loadData() { | 
|  | 59 | +    final survey = MockSurvey.fetchByID(this.surveyID); | 
|  | 60 | + | 
|  | 61 | +    if (mounted) { | 
|  | 62 | +      setState(() { | 
|  | 63 | +        this.survey = survey; | 
|  | 64 | +      }); | 
|  | 65 | +    } | 
|  | 66 | +  } | 
|  | 67 | + | 
|  | 68 | +  Widget _renderBody(BuildContext context, Survey survey) { | 
|  | 69 | +    var result = <Widget>[]; | 
|  | 70 | +    result.add(BannerImage(url: survey.imageUrl, height: BannerImageHeight)); | 
|  | 71 | +    result.add(_renderHeader()); | 
|  | 72 | +    result.add(_renderConsentForm()); | 
|  | 73 | +    result.add(_renderBottomSpacer()); | 
|  | 74 | +    return SingleChildScrollView( | 
|  | 75 | +        child: Column( | 
|  | 76 | +            mainAxisAlignment: MainAxisAlignment.start, | 
|  | 77 | +            crossAxisAlignment: CrossAxisAlignment.stretch, | 
|  | 78 | +            children: result)); | 
|  | 79 | +  } | 
|  | 80 | + | 
|  | 81 | +  Widget _renderHeader() { | 
|  | 82 | +    return Container( | 
|  | 83 | +      padding: EdgeInsets.symmetric( | 
|  | 84 | +          vertical: BodyVerticalPadding, | 
|  | 85 | +          horizontal: Styles.horizontalPaddingDefault), | 
|  | 86 | +      child: SurveyTile(survey: survey, darkTheme: false), | 
|  | 87 | +    ); | 
|  | 88 | +  } | 
|  | 89 | + | 
|  | 90 | +  Widget _renderFooter(BuildContext contexty) { | 
|  | 91 | +    return Column( | 
|  | 92 | +      mainAxisAlignment: MainAxisAlignment.end, | 
|  | 93 | +      crossAxisAlignment: CrossAxisAlignment.stretch, | 
|  | 94 | +      children: [ | 
|  | 95 | +        Container( | 
|  | 96 | +          decoration: BoxDecoration(color: Colors.white.withOpacity(0.5)), | 
|  | 97 | +          height: FooterHeight, | 
|  | 98 | +          child: Container( | 
|  | 99 | +            padding: EdgeInsets.symmetric(vertical: 20.0, horizontal: 30.0), | 
|  | 100 | +            child: _renderTakeSurveyButton(), | 
|  | 101 | +          ), | 
|  | 102 | +        ) | 
|  | 103 | +      ], | 
|  | 104 | +    ); | 
|  | 105 | +  } | 
|  | 106 | + | 
|  | 107 | +  Widget _renderConsentForm() { | 
|  | 108 | +    String title = AppLocalizations.of(context)!.translate("consent_form"); | 
|  | 109 | +    String text = AppLocalizations.of(context)!.translate("do_you_agree_to_share_your_anonymous_location_with") + "${survey.name}?"; | 
|  | 110 | + | 
|  | 111 | +    return Container( | 
|  | 112 | +      height: SurveyTileHeight, | 
|  | 113 | +      padding: EdgeInsets.symmetric( | 
|  | 114 | +          //vertical: BodyVerticalPadding, | 
|  | 115 | +          horizontal: Styles.horizontalPaddingDefault), | 
|  | 116 | +      child: Column( | 
|  | 117 | +        mainAxisAlignment: MainAxisAlignment.spaceEvenly, | 
|  | 118 | +        crossAxisAlignment: CrossAxisAlignment.start, | 
|  | 119 | +        children: [ | 
|  | 120 | +          Text('$title', | 
|  | 121 | +              overflow: TextOverflow.ellipsis, | 
|  | 122 | +              maxLines: 1, | 
|  | 123 | +              style: Styles.surveyTileTitleLight), | 
|  | 124 | +          Row( | 
|  | 125 | +            children: [ | 
|  | 126 | +              Checkbox( | 
|  | 127 | +                value: consent, | 
|  | 128 | +                onChanged: (bool? newValue) { | 
|  | 129 | +                  setState(() { | 
|  | 130 | +                    consent = newValue!; | 
|  | 131 | +                  }); | 
|  | 132 | +                }, | 
|  | 133 | +              ), | 
|  | 134 | +              Expanded( | 
|  | 135 | +                child: Text('$text', | 
|  | 136 | +                    overflow: TextOverflow.ellipsis, | 
|  | 137 | +                    maxLines: 3, | 
|  | 138 | +                    style: Styles.surveyTileCaption), | 
|  | 139 | +              ) | 
|  | 140 | +            ], | 
|  | 141 | +          ), | 
|  | 142 | +        ], | 
|  | 143 | +      ), | 
|  | 144 | +    ); | 
|  | 145 | +  } | 
|  | 146 | + | 
|  | 147 | +  Widget _renderTakeSurveyButton() { | 
|  | 148 | +    return TextButton( | 
|  | 149 | +      //color: Styles.accentColor, | 
|  | 150 | +      //textColor: Styles.textColorBright, | 
|  | 151 | +      style: ButtonStyle( | 
|  | 152 | +        backgroundColor: MaterialStateProperty.all(Colors.blue), | 
|  | 153 | +      ), | 
|  | 154 | +      onPressed: () => { | 
|  | 155 | +        _navigationToSurvey(context), | 
|  | 156 | +      }, | 
|  | 157 | +      child: Text( | 
|  | 158 | +        'Take Survey'.toUpperCase(), | 
|  | 159 | +        style: Styles.textCTAButton, | 
|  | 160 | +      ), | 
|  | 161 | +    ); | 
|  | 162 | +  } | 
|  | 163 | + | 
|  | 164 | +  Future<void> _navigationToSurvey(BuildContext context) async { | 
|  | 165 | +    String locationHistoryJSON = ""; | 
|  | 166 | + | 
|  | 167 | +    // If we have consent, send location history. Otherwise, send empty string | 
|  | 168 | +    if (consent) { | 
|  | 169 | +      List allLocations = await bg.BackgroundGeolocation.locations; | 
|  | 170 | +      List<ShareLocation> customLocation = []; | 
|  | 171 | + | 
|  | 172 | +      // We get only timestamp and coordinates into our custom class | 
|  | 173 | +      for (var thisLocation in allLocations) { | 
|  | 174 | +        ShareLocation _loc = new ShareLocation( | 
|  | 175 | +            bg.Location(thisLocation).timestamp, | 
|  | 176 | +            bg.Location(thisLocation).coords.latitude, | 
|  | 177 | +            bg.Location(thisLocation).coords.longitude); | 
|  | 178 | +        customLocation.add(_loc); | 
|  | 179 | +      } | 
|  | 180 | + | 
|  | 181 | +      locationHistoryJSON = jsonEncode(customLocation); | 
|  | 182 | +      locationHistoryJSON = locationHistoryJSON.replaceAll("\"", | 
|  | 183 | +          "'"); //We replace " into ' to avoid a javascript exception when we post it in the webview's form | 
|  | 184 | +    } else { | 
|  | 185 | +      locationHistoryJSON = "I do not agree to share my location history."; | 
|  | 186 | +    } | 
|  | 187 | + | 
|  | 188 | +    Navigator.push( | 
|  | 189 | +        context, | 
|  | 190 | +        MaterialPageRoute( | 
|  | 191 | +            builder: (context) => | 
|  | 192 | +                MyWebView(survey.webUrl, locationHistoryJSON))); | 
|  | 193 | +  } | 
|  | 194 | + | 
|  | 195 | +  Widget _renderBottomSpacer() { | 
|  | 196 | +    return Container(height: FooterHeight); | 
|  | 197 | +  } | 
|  | 198 | +} | 
0 commit comments