|
| 1 | +//Firstly add the dependency survey_kit: ^0.0.11 (latest one) |
| 2 | + |
| 3 | +import 'package:flutter/material.dart'; |
| 4 | +import 'package:survey_kit/survey_kit.dart'; |
| 5 | + |
| 6 | +class Surveys_custom extends StatefulWidget { |
| 7 | + Surveys_custom({Key key}) : super(key: key); |
| 8 | + |
| 9 | + @override |
| 10 | + _Surveys_customState createState() => _Surveys_customState(); |
| 11 | +} |
| 12 | + |
| 13 | +class _Surveys_customState extends State<Surveys_custom> { |
| 14 | + @override |
| 15 | + Widget build(BuildContext context) { |
| 16 | + return Scaffold( |
| 17 | + body: Container( |
| 18 | + color: Colors.white, |
| 19 | + child: Align( |
| 20 | + alignment: Alignment.center, |
| 21 | + child: Container( |
| 22 | + height: double.infinity, |
| 23 | + width: 600, |
| 24 | + child: SurveyKit( |
| 25 | + onResult: (SurveyResult result) { |
| 26 | + print(result.finishReason); |
| 27 | + }, |
| 28 | + task: getSampleTask(), |
| 29 | + themeData: Theme.of(context).copyWith( |
| 30 | + colorScheme: ColorScheme.fromSwatch( |
| 31 | + primarySwatch: Colors.cyan, |
| 32 | + ).copyWith( |
| 33 | + onPrimary: Colors.white, |
| 34 | + ), |
| 35 | + primaryColor: Colors.purpleAccent, |
| 36 | + backgroundColor: Colors.white, |
| 37 | + appBarTheme: const AppBarTheme( |
| 38 | + color: Colors.white, |
| 39 | + iconTheme: IconThemeData( |
| 40 | + color: Colors.purpleAccent, |
| 41 | + ), |
| 42 | + textTheme: TextTheme( |
| 43 | + button: TextStyle( |
| 44 | + color: Colors.cyan, |
| 45 | + ), |
| 46 | + ), |
| 47 | + ), |
| 48 | + iconTheme: const IconThemeData( |
| 49 | + color: Colors.purpleAccent, |
| 50 | + ), |
| 51 | + outlinedButtonTheme: OutlinedButtonThemeData( |
| 52 | + style: ButtonStyle( |
| 53 | + minimumSize: MaterialStateProperty.all( |
| 54 | + Size(150.0, 60.0), |
| 55 | + ), |
| 56 | + side: MaterialStateProperty.resolveWith( |
| 57 | + (Set<MaterialState> state) { |
| 58 | + if (state.contains(MaterialState.disabled)) { |
| 59 | + return BorderSide( |
| 60 | + color: Colors.grey, |
| 61 | + ); |
| 62 | + } |
| 63 | + return BorderSide( |
| 64 | + color: Colors.purpleAccent, |
| 65 | + ); |
| 66 | + }, |
| 67 | + ), |
| 68 | + shape: MaterialStateProperty.all( |
| 69 | + RoundedRectangleBorder( |
| 70 | + borderRadius: BorderRadius.circular(8.0), |
| 71 | + ), |
| 72 | + ), |
| 73 | + textStyle: MaterialStateProperty.resolveWith( |
| 74 | + (Set<MaterialState> state) { |
| 75 | + if (state.contains(MaterialState.disabled)) { |
| 76 | + return Theme.of(context).textTheme.button?.copyWith( |
| 77 | + color: Colors.grey, |
| 78 | + ); |
| 79 | + } |
| 80 | + return Theme.of(context).textTheme.button?.copyWith( |
| 81 | + color: Colors.purpleAccent, |
| 82 | + ); |
| 83 | + }, |
| 84 | + ), |
| 85 | + ), |
| 86 | + ), |
| 87 | + textButtonTheme: TextButtonThemeData( |
| 88 | + style: ButtonStyle( |
| 89 | + textStyle: MaterialStateProperty.all( |
| 90 | + Theme.of(context).textTheme.button?.copyWith( |
| 91 | + color: Colors.cyan, |
| 92 | + ), |
| 93 | + ), |
| 94 | + ), |
| 95 | + ), |
| 96 | + ), |
| 97 | + ), |
| 98 | + ), |
| 99 | + ), |
| 100 | + ), |
| 101 | + ); |
| 102 | + } |
| 103 | + |
| 104 | + Task getSampleTask() { |
| 105 | + var task = NavigableTask( |
| 106 | + id: TaskIdentifier(), |
| 107 | + steps: [ |
| 108 | + InstructionStep( |
| 109 | + title: 'Welcome to the\nDeal Brand\nGoogle Survey', |
| 110 | + text: 'Get ready for a bunch of super interesting questions!', |
| 111 | + buttonText: 'Let\'s go!', |
| 112 | + ), |
| 113 | + QuestionStep( |
| 114 | + title: 'How old are you?', |
| 115 | + answerFormat: IntegerAnswerFormat( |
| 116 | + defaultValue: 25, |
| 117 | + hint: 'Please enter your age', |
| 118 | + |
| 119 | + ), |
| 120 | + ), |
| 121 | + QuestionStep( |
| 122 | + title: 'Shpping?', |
| 123 | + text: 'Do you prefer online shopping?', |
| 124 | + answerFormat: BooleanAnswerFormat( |
| 125 | + positiveAnswer: 'Yes', |
| 126 | + negativeAnswer: 'No', |
| 127 | + result: BooleanResult.POSITIVE, |
| 128 | + ), |
| 129 | + ), |
| 130 | + // QuestionStep( |
| 131 | + // title: 'Tell us about you', |
| 132 | + // text: |
| 133 | + // 'Tell us about yourself and why you want to improve your health.', |
| 134 | + // answerFormat: TextAnswerFormat( |
| 135 | + // maxLines: 5, |
| 136 | + // ), |
| 137 | + // ), |
| 138 | + QuestionStep( |
| 139 | + title: 'Rate the app', |
| 140 | + answerFormat: ScaleAnswerFormat( |
| 141 | + step: 3, |
| 142 | + minimumValue: 1, |
| 143 | + maximumValue: 5, |
| 144 | + defaultValue: 3, |
| 145 | + minimumValueDescription: '1', |
| 146 | + maximumValueDescription: '5', |
| 147 | + ), |
| 148 | + ), |
| 149 | + QuestionStep( |
| 150 | + title: 'Favourite Sport', |
| 151 | + text: 'Which of the following sports do you watch ?', |
| 152 | + answerFormat: MultipleChoiceAnswerFormat( |
| 153 | + textChoices: [ |
| 154 | + TextChoice(text: 'Football', value: 'Football'), |
| 155 | + TextChoice(text: 'Basket ball', value: 'Basket ball'), |
| 156 | + TextChoice(text: 'Gymnastics', value: 'Gymnastics'), |
| 157 | + TextChoice(text: 'Swimming', value: 'Swimming'), |
| 158 | + TextChoice(text: 'Auto Racing', value: 'autoracing'), |
| 159 | + TextChoice(text: 'Baseball', value: 'Baseball'), |
| 160 | + ], |
| 161 | + ), |
| 162 | + ), |
| 163 | + QuestionStep( |
| 164 | + title: 'Done?', |
| 165 | + text: 'We are done, do you mind to tell us more about yourself...?', |
| 166 | + answerFormat: SingleChoiceAnswerFormat( |
| 167 | + textChoices: [ |
| 168 | + TextChoice(text: 'Yes', value: 'Yes'), |
| 169 | + TextChoice(text: 'No', value: 'No'), |
| 170 | + ], |
| 171 | + ), |
| 172 | + ), |
| 173 | + QuestionStep( |
| 174 | + title: 'When are your office hours?', |
| 175 | + answerFormat: TimeAnswerFormat( |
| 176 | + defaultValue: TimeOfDay( |
| 177 | + hour: 12, |
| 178 | + minute: 0, |
| 179 | + ), |
| 180 | + ), |
| 181 | + ), |
| 182 | + QuestionStep( |
| 183 | + title: 'When was your last holiday?', |
| 184 | + answerFormat: DateAnswerFormat( |
| 185 | + minDate: DateTime.utc(1970), |
| 186 | + defaultDate: DateTime.now(), |
| 187 | + maxDate: DateTime.now(), |
| 188 | + ), |
| 189 | + ), |
| 190 | + CompletionStep( |
| 191 | + id: StepIdentifier(id: '321'), |
| 192 | + text: 'Thanks for taking the survey, we will contact you soon!', |
| 193 | + title: 'Done!', |
| 194 | + buttonText: 'Submit survey', |
| 195 | + ), |
| 196 | + ], |
| 197 | + ); |
| 198 | + task.addNavigationRule( |
| 199 | + forTriggerStepIdentifier: task.steps[6].id, |
| 200 | + navigationRule: ConditionalNavigationRule( |
| 201 | + resultToStepIdentifierMapper: (input) { |
| 202 | + switch (input) { |
| 203 | + case "Yes": |
| 204 | + return task.steps[0].id; |
| 205 | + case "No": |
| 206 | + return task.steps[7].id; |
| 207 | + default: |
| 208 | + return null; |
| 209 | + } |
| 210 | + }, |
| 211 | + ), |
| 212 | + ); |
| 213 | + return task; |
| 214 | + } |
| 215 | +} |
0 commit comments