Advertisement
Guest User

Untitled

a guest
Sep 19th, 2019
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.21 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2. import 'package:image_picker/image_picker.dart';
  3. import 'dart:io';
  4. import '../../models/product.dart';
  5.  
  6. class ImageInput extends StatefulWidget{
  7.  
  8. final Function setImage;
  9. final Product product;
  10.  
  11. ImageInput(this.setImage, this.product);
  12.  
  13. @override
  14. State<StatefulWidget> createState(){
  15. return _ImageInputState();
  16. }
  17. }
  18.  
  19. class _ImageInputState extends State<ImageInput>{
  20.  
  21.  
  22.  
  23. File _imageFile;
  24.  
  25. void _getImage(BuildContext context, ImageSource source) async {
  26. ImagePicker.pickImage(source: source, maxWidth: 400.0).then((File image){
  27. setState((){
  28. _imageFile = image;
  29. });
  30. widget.setImage(image);
  31. Navigator.pop(context);
  32. });
  33. }
  34.  
  35. void _openImagePicker(BuildContext context){
  36. showModalBottomSheet(context: context, builder: (BuildContext context){
  37. return Container(
  38. height: 150.0,
  39. padding: EdgeInsets.all(10.0),
  40. child: Column(
  41. children: <Widget>[
  42. Text(
  43. "Pick an Image",
  44. style: TextStyle(
  45. fontWeight: FontWeight.bold,
  46. ),
  47. ),
  48. SizedBox(height: 10.0),
  49. FlatButton(
  50. textColor: Theme.of(context).accentColor,
  51. child: Text(
  52. "Use Camera",
  53. ),
  54. onPressed: (){
  55. _getImage(context, ImageSource.camera);
  56. },
  57. ),
  58. FlatButton(
  59. textColor: Theme.of(context).accentColor,
  60. child: Text(
  61. "Use Gallery",
  62. ),
  63. onPressed: (){
  64. _getImage(context, ImageSource.gallery);
  65. },
  66. )
  67. ]
  68. )
  69. );
  70. });
  71. }
  72.  
  73. @override
  74. Widget build(BuildContext context){
  75.  
  76. final buttonColor = Theme.of(context).accentColor;
  77. Widget previewImage = Text("Please select an image,");
  78.  
  79. if (_imageFile != null){
  80. previewImage = Image.file(
  81. _imageFile,
  82. fit: BoxFit.cover,
  83. height: 300.0,
  84. width: MediaQuery.of(context).size.width,
  85. alignment: Alignment.center,
  86. );
  87. }else if(widget.product != null){
  88. previewImage = Image.network(
  89. widget.product.image,
  90. fit: BoxFit.cover,
  91. height: 300.0,
  92. width: MediaQuery.of(context).size.width,
  93. alignment: Alignment.center,
  94. );
  95. }
  96.  
  97.  
  98. return Column(
  99. children: <Widget>[
  100. OutlineButton(
  101. borderSide: BorderSide(
  102. color: buttonColor,
  103. width: 2.0,
  104. ),
  105. onPressed: (){
  106. _openImagePicker(context);
  107. },
  108. child: Row(
  109. mainAxisAlignment: MainAxisAlignment.center,
  110. children: <Widget>[
  111. Icon(
  112. Icons.camera_alt,
  113. color: buttonColor,
  114. ),
  115. SizedBox(width: 5.0,),
  116. Text(
  117. "Add Image",
  118. style: TextStyle(color: buttonColor),
  119. ),
  120. ]
  121. ),
  122. ),
  123. SizedBox(height: 10.0),
  124. previewImage,
  125. ],
  126. );
  127. }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement