Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.91 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2. import 'dart:io';
  3. import 'dart:async';
  4. import 'package:flutter/services.dart';
  5. import 'package:image_picker/image_picker.dart';
  6. import 'package:zigma2/pages/landing_page.dart';
  7. import 'DataProvider.dart';
  8.  
  9. File _image;
  10. Animation<double> _animation;
  11.  
  12. class AdvertCreation extends StatefulWidget {
  13. State createState() => AdvertCreationState();
  14. }
  15.  
  16. class AnimatedLogo extends AnimatedWidget {
  17. AnimatedLogo({Key key, Animation<double> animation})
  18. : super(key: key, listenable: animation);
  19.  
  20. Widget build(BuildContext contet) {
  21. return Container(
  22. constraints: BoxConstraints(
  23. maxHeight: _animation.value,
  24. maxWidth: _animation.value,
  25. minWidth: _animation.value,
  26. minHeight: _animation.value,
  27. ),
  28. child: Image.file(_image),
  29. );
  30. }
  31. }
  32.  
  33. class AdvertCreationState extends State<AdvertCreation>
  34. with TickerProviderStateMixin {
  35. //Image selector
  36.  
  37. Future getImageCamera() async {
  38. var image = await ImagePicker.pickImage(source: ImageSource.camera);
  39. setState(() {
  40. _image = image;
  41. });
  42. }
  43.  
  44. Future getImageGallery() async {
  45. var image = await ImagePicker.pickImage(source: ImageSource.gallery);
  46. setState(() {
  47. _image = image;
  48. });
  49. }
  50.  
  51. final GlobalKey<FormState> _advertKey = GlobalKey<FormState>();
  52. String _title; //Sent
  53. int _price; //Sent
  54. String _author; //Sent
  55. String _isbn; //Sent
  56. String _contactInfo;
  57. int randomInt = 42;
  58. FocusNode myFocusNode;
  59. AnimationController _controller;
  60. bool isLoading = false;
  61.  
  62. @override
  63. void initState() {
  64. super.initState();
  65. _controller =
  66. AnimationController(duration: const Duration(seconds: 10), vsync: this);
  67. _animation = Tween<double>(begin: 0, end: 150).animate(_controller)
  68. ..addStatusListener((status) {})
  69. ..addStatusListener((state) => print('$state'));
  70. _controller.forward();
  71. myFocusNode = FocusNode();
  72. }
  73.  
  74. @override
  75. void dispose() {
  76. myFocusNode.dispose();
  77. _controller.dispose();
  78. super.dispose();
  79. }
  80.  
  81. int stringToInt(price) {
  82. var priceInt = int.parse(price);
  83. assert(priceInt is int);
  84. return priceInt;
  85. }
  86.  
  87. @override
  88. Widget build(BuildContext context) {
  89. return Container(
  90. color: Color(0xFFECE9DF),
  91. child: Scaffold(
  92. resizeToAvoidBottomPadding: true,
  93. backgroundColor: Colors.transparent,
  94. appBar: PreferredSize(
  95. preferredSize: Size.fromHeight(60.0),
  96. child: AppBar(
  97. iconTheme: IconThemeData(color: Color(0xff96070a)),
  98. elevation: 0.0,
  99. backgroundColor: Color(0xff96070a),
  100. title: Text('Lägg till en ny annons',
  101. style: TextStyle(
  102. fontWeight: FontWeight.bold,
  103. color: Color(0xFFFFFFFF),
  104. )),
  105. centerTitle: true,
  106. leading: Container(
  107. child: IconButton(
  108. color: Color(0xFFFFFFFF),
  109. onPressed: () {
  110. Navigator.pop(context);
  111. },
  112. icon: Icon(Icons.arrow_back),
  113. ),
  114. ),
  115. actions: <Widget>[],
  116. ),
  117. ),
  118. body: isLoading
  119. ? Center(
  120. child: CircularProgressIndicator(),
  121. )
  122. : Center(
  123. child: ListView(
  124. shrinkWrap: true,
  125. padding: EdgeInsets.all(15.0),
  126. children: <Widget>[
  127. Row(
  128. mainAxisAlignment: MainAxisAlignment.spaceEvenly,
  129. crossAxisAlignment: CrossAxisAlignment.center,
  130. children: <Widget>[
  131. Container(
  132. alignment: Alignment(0.0, 0.0),
  133. child: _image == null
  134. ? Text('No image Selected')
  135. : AnimatedLogo(
  136. animation: _animation,
  137. ),
  138. ),
  139. FloatingActionButton(
  140. onPressed: getImageCamera,
  141. tooltip: 'Pick Image',
  142. child: Icon(Icons.add_a_photo),
  143. ),
  144. ],
  145. ),
  146. Container(
  147. padding: EdgeInsets.only(
  148. left: 30.0, top: 10.0, right: 30.0, bottom: 10.0),
  149. child: Form(
  150. key: _advertKey,
  151. child: Container(
  152. decoration: BoxDecoration(
  153. shape: BoxShape.rectangle,
  154. color: Color(0xFFFFFFFF),
  155. borderRadius:
  156. BorderRadius.all(Radius.elliptical(20.0, 20.0)),
  157. ),
  158. child: Column(
  159. children: <Widget>[
  160. TextFormField(
  161. focusNode: myFocusNode,
  162. maxLines: 1,
  163. keyboardType: TextInputType.text,
  164. autofocus: false,
  165. decoration: InputDecoration(
  166. hintText: 'Titel',
  167. ),
  168. validator: (value) =>
  169. value.isEmpty ? 'Obligatoriskt Fält' : null,
  170. onSaved: (value) => _title = value,
  171. ),
  172. TextFormField(
  173. maxLines: 1,
  174. keyboardType: TextInputType.number,
  175. autofocus: false,
  176. decoration: InputDecoration(
  177. hintText: 'Pris',
  178. ),
  179. validator: (value) =>
  180. value.isEmpty ? 'Obligatoriskt Fält' : null,
  181. onSaved: (value) => _price = stringToInt(value),
  182. ),
  183. TextFormField(
  184. maxLines: 1,
  185. keyboardType: TextInputType.text,
  186. autofocus: false,
  187. decoration: InputDecoration(
  188. hintText: 'Författare',
  189. ),
  190. validator: (value) =>
  191. value.isEmpty ? 'Obligatoriskt Fält' : null,
  192. onSaved: (value) => _author = value,
  193. ),
  194. TextFormField(
  195. maxLines: 1,
  196. keyboardType: TextInputType.text,
  197. autofocus: false,
  198. decoration: InputDecoration(
  199. hintText: 'ISBN',
  200. ),
  201. validator: (value) =>
  202. value.isEmpty ? 'Obligatoriskt Fält' : null,
  203. onSaved: (value) => _isbn = value,
  204. ),
  205. TextFormField(
  206. maxLines: 1,
  207. keyboardType: TextInputType.text,
  208. autofocus: false,
  209. decoration: InputDecoration(
  210. hintText: 'Kontaktinformation',
  211. ),
  212. validator: (value) =>
  213. value.isEmpty ? 'Obligatoriskt Fält' : null,
  214. onSaved: (value) => _contactInfo = value,
  215. ),
  216. ],
  217. ),
  218. ),
  219. ),
  220. ),
  221. Container(
  222. width: 50,
  223. child: MaterialButton(
  224. color: Color(0xFF008000),
  225. onPressed: () async {
  226. int stsCode;
  227. if (_advertKey.currentState.validate()) {
  228. setState(() {
  229. isLoading = true;
  230. });
  231. _advertKey.currentState.save();
  232. stsCode = await DataProvider
  233. .of(context)
  234. .advertList
  235. .uploadNewAdvert(
  236. _title, _price, _author, _isbn,
  237. _contactInfo, context);
  238. }
  239. if(stsCode == 201){
  240. routeLandingPage();
  241. }
  242. else if(stsCode == 400 || stsCode == 500){
  243. setState(() {
  244. isLoading = false;
  245. });
  246. createAlertDialog();
  247. }
  248. },
  249. child: Text("Ladda upp",
  250. style: TextStyle(color: Color(0xFFFFFFFF))),
  251. ),
  252. ),
  253. ],
  254. ),
  255. ),
  256. ),
  257. );
  258. }
  259.  
  260. createAlertDialog(){
  261.  
  262. }
  263.  
  264. void routeLandingPage() {
  265. Navigator.of(context)
  266. .push(MaterialPageRoute<void>(builder: (_) => LandingPage()));
  267. }
  268. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement