【发布时间】:2019-05-15 11:28:29
【问题描述】:
我在我的应用中为图像创建了一个占位符,单击时我希望能够打开相机,并显示在占位符中单击的图像。我为此使用了 image_picker 包。我用 GestureDetector 包裹了占位符容器,但是在点击容器时没有任何反应。我该如何解决这个问题?
在包含无状态小部件的主文件中,我添加了有状态小部件 addImage()
Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
addImage(), // my stateful widget
TextField(
)
),
Row(
//other implementation here
],
)
]
)
我的 addImage 有状态小部件如下所示
import 'dart:async';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
class addImage extends StatefulWidget{
@override
_addImageState createState() => _addImageState();
}
class _addImageState extends State<addImage> {
File _image;
Future getImagefromCamera() async{
var image = await ImagePicker.pickImage(source: ImageSource.camera);
setState(() {
_image = image;
});
}
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: getImagefromCamera,
child: _image == null ? Container(decoration: BoxDecoration(color: Colors.red[50],border: Border.all(color: Colors.red[200], width: 1.0),borderRadius: BorderRadius.circular(10.0)),
child: Column(
children: <Widget>[
SizedBox(height:30.0),
Icon(Icons.camera_alt, color: Colors.red),
SizedBox(height: 10.0),
Text('Take Image of the Item', style: TextStyle(color: Colors.red)),
SizedBox(height: 30.0)
],
)) : Image.file(_image),
);
}
}
我什至在 pubspec.yaml 中添加了依赖项,但是仍然不起作用
image_picker: ^0.4.5
【问题讨论】:
标签: flutter