|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import 'package:flutter_getx_template/src/public/constant.dart'; |
| 3 | +import 'package:flutter_getx_template/src/public/styles.dart'; |
| 4 | +import 'package:get/get.dart'; |
| 5 | +import 'package:translator/translator.dart'; |
| 6 | + |
| 7 | +class DetailsPage extends StatefulWidget { |
| 8 | + final results; |
| 9 | + DetailsPage({this.results}); |
| 10 | + @override |
| 11 | + State<StatefulWidget> createState() => _DetailsPageState(); |
| 12 | +} |
| 13 | + |
| 14 | +class _DetailsPageState extends State<DetailsPage> { |
| 15 | + final translator = GoogleTranslator(); |
| 16 | + |
| 17 | + var value1; |
| 18 | + var value2; |
| 19 | + var value3; |
| 20 | + var value4; |
| 21 | + bool loading = true; |
| 22 | + |
| 23 | + void initContent() async { |
| 24 | + value1 = await translator.translate( |
| 25 | + widget.results[0]['meanings']['light'].join('\n\n'), |
| 26 | + to: 'vi', |
| 27 | + ); |
| 28 | + print(value1); |
| 29 | + value2 = await translator.translate( |
| 30 | + widget.results[1]['meanings']['light'].join('\n\n'), |
| 31 | + to: 'vi', |
| 32 | + ); |
| 33 | + value3 = await translator.translate( |
| 34 | + widget.results[2]['meanings']['light'].join('\n\n'), |
| 35 | + to: 'vi', |
| 36 | + ); |
| 37 | + value4 = await translator |
| 38 | + .translate( |
| 39 | + widget.results[3]['meanings']['light'].join('\n\n'), |
| 40 | + to: 'vi', |
| 41 | + ) |
| 42 | + .then((result) { |
| 43 | + setState(() { |
| 44 | + loading = false; |
| 45 | + }); |
| 46 | + return result; |
| 47 | + }); |
| 48 | + } |
| 49 | + |
| 50 | + @override |
| 51 | + void initState() { |
| 52 | + super.initState(); |
| 53 | + initContent(); |
| 54 | + } |
| 55 | + |
| 56 | + @override |
| 57 | + Widget build(BuildContext context) { |
| 58 | + return Scaffold( |
| 59 | + appBar: AppBar( |
| 60 | + backgroundColor: colorBlack, |
| 61 | + elevation: 4.0, |
| 62 | + centerTitle: true, |
| 63 | + title: Text( |
| 64 | + 'solveCards'.trArgs(), |
| 65 | + style: TextStyle( |
| 66 | + color: mCL, |
| 67 | + fontFamily: 'Lato', |
| 68 | + fontSize: width / 20.0, |
| 69 | + fontWeight: FontWeight.w600, |
| 70 | + ), |
| 71 | + ), |
| 72 | + actions: [ |
| 73 | + GestureDetector( |
| 74 | + onTap: () { |
| 75 | + if (Get.locale == Locale('vi', 'VN')) { |
| 76 | + Get.updateLocale(Locale('en', 'US')); |
| 77 | + } else { |
| 78 | + Get.updateLocale(Locale('vi', 'VN')); |
| 79 | + } |
| 80 | + }, |
| 81 | + child: Container( |
| 82 | + padding: EdgeInsets.symmetric(horizontal: 16.0, vertical: 2.0), |
| 83 | + margin: EdgeInsets.symmetric(vertical: 1.0), |
| 84 | + decoration: BoxDecoration( |
| 85 | + color: Colors.transparent, |
| 86 | + borderRadius: BorderRadius.circular(8.0), |
| 87 | + ), |
| 88 | + alignment: Alignment.center, |
| 89 | + child: Text( |
| 90 | + Get.locale == Locale('vi', 'VN') ? 'vi' : 'en', |
| 91 | + style: TextStyle( |
| 92 | + color: colorPrimary, |
| 93 | + fontFamily: 'Lato', |
| 94 | + fontSize: width / 22.5, |
| 95 | + fontWeight: FontWeight.w600, |
| 96 | + ), |
| 97 | + ), |
| 98 | + ), |
| 99 | + ), |
| 100 | + SizedBox(width: 8.0), |
| 101 | + ], |
| 102 | + ), |
| 103 | + body: loading |
| 104 | + ? Container( |
| 105 | + color: colorBlack, |
| 106 | + height: height, |
| 107 | + width: width, |
| 108 | + child: Center( |
| 109 | + child: CircularProgressIndicator(), |
| 110 | + ), |
| 111 | + ) |
| 112 | + : Container( |
| 113 | + color: colorBlack, |
| 114 | + height: height, |
| 115 | + width: width, |
| 116 | + padding: EdgeInsets.symmetric(horizontal: 20.0), |
| 117 | + child: SingleChildScrollView( |
| 118 | + physics: ClampingScrollPhysics(), |
| 119 | + child: Column( |
| 120 | + crossAxisAlignment: CrossAxisAlignment.start, |
| 121 | + children: [ |
| 122 | + SizedBox(height: 20.0), |
| 123 | + _buildTitle('overview'.trArgs(), widget.results[0]['name']), |
| 124 | + SizedBox(height: 16.0), |
| 125 | + Get.locale == Locale('vi', 'VN') |
| 126 | + ? _buildContentTranslate(value1) |
| 127 | + : _buildContent(widget.results[0]['meanings']['light']), |
| 128 | + SizedBox(height: 16.0), |
| 129 | + _buildTitle('work'.trArgs(), widget.results[1]['name']), |
| 130 | + SizedBox(height: 16.0), |
| 131 | + Get.locale == Locale('vi', 'VN') |
| 132 | + ? _buildContentTranslate(value2) |
| 133 | + : _buildContent(widget.results[1]['meanings']['light']), |
| 134 | + SizedBox(height: 16.0), |
| 135 | + _buildTitle('love'.trArgs(), widget.results[2]['name']), |
| 136 | + SizedBox(height: 16.0), |
| 137 | + Get.locale == Locale('vi', 'VN') |
| 138 | + ? _buildContentTranslate(value3) |
| 139 | + : _buildContent(widget.results[2]['meanings']['light']), |
| 140 | + SizedBox(height: 16.0), |
| 141 | + _buildTitle('finance'.trArgs(), widget.results[3]['name']), |
| 142 | + SizedBox(height: 16.0), |
| 143 | + Get.locale == Locale('vi', 'VN') |
| 144 | + ? _buildContentTranslate(value4) |
| 145 | + : _buildContent(widget.results[3]['meanings']['light']), |
| 146 | + SizedBox(height: 32.0), |
| 147 | + ], |
| 148 | + ), |
| 149 | + ), |
| 150 | + ), |
| 151 | + ); |
| 152 | + } |
| 153 | + |
| 154 | + Widget _buildTitle(title, value) { |
| 155 | + return RichText( |
| 156 | + text: TextSpan( |
| 157 | + children: [ |
| 158 | + TextSpan( |
| 159 | + text: title + ':\t\t', |
| 160 | + style: TextStyle( |
| 161 | + color: colorPrimary, |
| 162 | + fontFamily: 'Lato', |
| 163 | + fontSize: width / 22.5, |
| 164 | + fontWeight: FontWeight.w600, |
| 165 | + ), |
| 166 | + ), |
| 167 | + TextSpan( |
| 168 | + text: value, |
| 169 | + style: TextStyle( |
| 170 | + color: mCL, |
| 171 | + fontFamily: 'Lato', |
| 172 | + fontSize: width / 23.0, |
| 173 | + fontWeight: FontWeight.w600, |
| 174 | + ), |
| 175 | + ), |
| 176 | + ], |
| 177 | + ), |
| 178 | + ); |
| 179 | + } |
| 180 | + |
| 181 | + Widget _buildContent(List content) { |
| 182 | + return Text( |
| 183 | + content.join('\n\n'), |
| 184 | + style: TextStyle( |
| 185 | + color: mCL, |
| 186 | + fontFamily: 'Lato', |
| 187 | + fontSize: width / 25.0, |
| 188 | + fontWeight: FontWeight.w400, |
| 189 | + ), |
| 190 | + ); |
| 191 | + } |
| 192 | + |
| 193 | + Widget _buildContentTranslate(future) { |
| 194 | + return Text( |
| 195 | + future.toString(), |
| 196 | + style: TextStyle( |
| 197 | + color: mCL, |
| 198 | + fontFamily: 'Lato', |
| 199 | + fontSize: width / 25.0, |
| 200 | + fontWeight: FontWeight.w400, |
| 201 | + ), |
| 202 | + ); |
| 203 | + } |
| 204 | +} |
0 commit comments