嗨,大家好,飞镖飞镖
我可以进行照片上传功能
但是我在制造它时遇到了麻烦。
'class pictureBox()'无法识别'getImage()',,
这是我的整个代码:
class writeprofile extends StatefulWidget {
@override
_writeprofileState createState() => _writeprofileState();
}
class _writeprofileState extends State<writeprofile> {
File _image;
@override
Widget build(BuildContext context) {
Future getImage() async{
var image= await ImagePicker.pickImage(source: ImageSource.gallery);
setState(() {
_image = image;
print('Image Path $_image');
});
}
Future uploadPic(BuildContext context) async{
String filName = basename(_image.path);
StorageReference firebaseStorageRef = FirebaseStorage.instance.ref().child(filName);
StorageUploadTask uploadTask = firebaseStorageRef.putFile(_image);
StorageTaskSnapshot taskSnapshot = await uploadTask.onComplete;
setState(() {
print("Profile pic upload !!");
Scaffold.of(context).showSnackBar(SnackBar(content: Text('Profile pic Upload !!')));
});
}
return Scaffold(
body: Builder(
builder: (context)=> Center(
child: Container(
child: Container(
child: Column(
children: <Widget>[
Padding(
padding: EdgeInsets.only(top: 70),
child: Text(
'사진 선택',
style: TextStyle(
fontSize: 30,
fontWeight: FontWeight.w500,
),
),
),
Padding(
padding: EdgeInsets.only(top: 10, bottom: 50),
child: Container(
height: 1,
width: MediaQuery.of(context).size.width / 1.4,
color: Colors.black26,
),
),
Container(
width: MediaQuery.of(context).size.width / 1.5,
height: MediaQuery.of(context).size.height / 15,
alignment: FractionalOffset.center,
decoration: BoxDecoration(
color: const Color.fromRGBO(250, 80, 120, 1),
borderRadius: BorderRadius.all(const Radius.circular(30)),
),
child: Text(
"가이드 라인을 읽어주세요 !",
style: TextStyle(
color: Colors.white,
fontSize: 20,
fontWeight: FontWeight.w300,
letterSpacing: 0.3,
),
),
),
Padding(
padding: const EdgeInsets.only(top: 100),
child: Row(
children: <Widget>[
PictureBox(),
PictureBox(),
PictureBox(),
],
),
),
Padding(
padding: const EdgeInsets.only(top: 10),
child: Row(
children: <Widget>[
PictureBox(),
PictureBox(),
PictureBox(),
],
),
),
InkWell(
onTap: (){uploadPic(context);},
child: Padding(
padding: EdgeInsets.only(top: 50),
child: Container(
width: MediaQuery.of(context).size.width / 3,
height: MediaQuery.of(context).size.height /20,
alignment: FractionalOffset.center,
decoration: BoxDecoration(
color: const Color.fromRGBO(250, 80, 100, 1),
borderRadius: BorderRadius.all(const Radius.circular(30)),
),
child: Text(
"Next",
style: TextStyle(
color: Colors.white,
fontSize: 20,
fontWeight: FontWeight.w500,
letterSpacing: 0.3,
),
),
),
),
),
],
),
),
),
),
),
);
}
}
class PictureBox extends StatefulWidget {
@override
_PictureBoxState createState() => _PictureBoxState();
}
class _PictureBoxState extends State<PictureBox>{
@override
Widget build(BuildContext context) {
return Container(
child: InkWell(
onTap: (){getImage(context);},
child: Padding(
padding: EdgeInsets.only(left: 10),
child:Container(
width: MediaQuery.of(context).size.width / 3.3,
height: MediaQuery.of(context).size.height / 7,
color: Colors.black12,
child: Center(
child: (_image!=null)? Image.file(_image, fit:BoxFit.fill)
:Icon(
Icons.camera_alt,
color: Colors.black26,
),
),
),
),
),
);
}
}
这是我的代码,我想使用PictureBox中的'Future getImage()'如何在PictureBox中运行getImage()?
如何在Picture类中使用getImage?
我对全局密钥有些了解,我尝试了很多方法,但是仍然无法修复我的代码。
答案 0 :(得分:0)
Padding(
padding: const EdgeInsets.only(top: 30.0, bottom: 30),
child: ClipRRect(
borderRadius: new BorderRadius.circular(100.0),
child: sampleImage == null
? Container(
width: 150.0,
height: 150.0,
decoration: new BoxDecoration(
color: Colors.white,
border: Border.all(
color: Color.fromRGBO(197, 214, 226, 1.0),
width: 3,
style: BorderStyle.solid),
shape: BoxShape.circle,
image: DecorationImage(
image: AssetImage(
"images/profile.png",
),
fit: BoxFit.cover,
colorFilter:
ColorFilter.srgbToLinearGamma())))
: Container(
width: 150.0,
height: 150.0,
decoration: BoxDecoration(
border: Border.all(
color: Color.fromRGBO(197, 214, 226, 1.0),
width: 3,
style: BorderStyle.solid),
shape: BoxShape.circle,
image: DecorationImage(
image: FileImage(sampleImage),
fit: BoxFit.cover,
)),
)),
),
Positioned(
bottom: 25,
right: 0,
child: CircleAvatar(
backgroundColor: Colors.white,
maxRadius: 25,
child: Container(
decoration: BoxDecoration(
shape: BoxShape.circle,
border: Border.all(
color: Color.fromRGBO(197, 214, 226, 1.0),
width: 3,
style: BorderStyle.solid),
),
child: IconButton(
icon: Icon(
Icons.photo_camera,
color: Colors.blue,
size: 30,
),
hoverColor: Colors.blue,
onPressed: () {
showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: Column(
children: <Widget>[
ListTile(
title: Text('Camera'),
onTap: () async {
Navigator.pop(context);
var tempImage =
await ImagePicker.pickImage(
source: ImageSource.camera);
setState(() {
sampleImage = tempImage;
});
},
),
ListTile(
title: Text('Gallery'),
onTap: () async {
Navigator.pop(context);
var tempImage =
await ImagePicker.pickImage(
source: ImageSource.gallery);
setState(() {
sampleImage = tempImage;
});
},
),
],
),
);
});
},
),
),
),
),
],
),
,请根据您的要求使用下面的库
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'dart:io';
import 'package:image_picker/image_picker.dart';
import 'util/CustomIcon.dart';
上传图片代码
onTap: () async {
Navigator.pop(context);
var tempImage =
await ImagePicker.pickImage(
source: ImageSource.gallery,
imageQuality: 50);
sampleImage = tempImage;
StorageReference storageReference =
FirebaseStorage.instance
.ref()
.child("Chatbox")
.child(
"$userId${widget.name.userId}${DateTime.now()}");
if (sampleImage != null) {
// Navigator.push(context,MaterialPageRoute(builder: (context)=>PhotoFather(abc: tempImage,)));
StorageUploadTask uploadTask =
storageReference
.putFile(sampleImage);
答案 1 :(得分:0)
尝试一下,我刚刚调整了您的代码。也可以参考CamelCase约定为类命名。
class WriteProfile extends StatefulWidget {
@override
_WriteProfileState createState() => _WriteProfileState();
}
class _WriteProfileState extends State< WriteProfile > {
File _image;
Future getImage(BuildContext context) async {
var image = await ImagePicker.pickImage(source: ImageSource.gallery);
setState(() {
_image = image;
print('Image Path $_image');
});
}
Future uploadPic(BuildContext context) async {
String filName = basename(_image.path);
StorageReference firebaseStorageRef =
FirebaseStorage.instance.ref().child(filName);
StorageUploadTask uploadTask = firebaseStorageRef.putFile(_image);
StorageTaskSnapshot taskSnapshot = await uploadTask.onComplete;
setState(() {
print("Profile pic upload !!");
Scaffold.of(context)
.showSnackBar(SnackBar(content: Text('Profile pic Upload !!')));
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Builder(
builder: (context) => Center(
child: Container(
child: Container(
child: Column(
children: <Widget>[
Padding(
padding: EdgeInsets.only(top: 70),
child: Text(
'사진 선택',
style: TextStyle(
fontSize: 30,
fontWeight: FontWeight.w500,
),
),
),
Padding(
padding: EdgeInsets.only(top: 10, bottom: 50),
child: Container(
height: 1,
width: MediaQuery.of(context).size.width / 1.4,
color: Colors.black26,
),
),
Container(
width: MediaQuery.of(context).size.width / 1.5,
height: MediaQuery.of(context).size.height / 15,
alignment: FractionalOffset.center,
decoration: BoxDecoration(
color: const Color.fromRGBO(250, 80, 120, 1),
borderRadius: BorderRadius.all(const Radius.circular(30)),
),
child: Text(
"가이드 라인을 읽어주세요 !",
style: TextStyle(
color: Colors.white,
fontSize: 20,
fontWeight: FontWeight.w300,
letterSpacing: 0.3,
),
),
),
Padding(
padding: const EdgeInsets.only(top: 100),
child: Row(
children: <Widget>[
_buildPictureBox(),
_buildPictureBox(),
_buildPictureBox(),
],
),
),
Padding(
padding: const EdgeInsets.only(top: 10),
child: Row(
children: <Widget>[
_buildPictureBox(),
_buildPictureBox(),
_buildPictureBox(),
],
),
),
InkWell(
onTap: () {
uploadPic(context);
},
child: Padding(
padding: EdgeInsets.only(top: 50),
child: Container(
width: MediaQuery.of(context).size.width / 3,
height: MediaQuery.of(context).size.height / 20,
alignment: FractionalOffset.center,
decoration: BoxDecoration(
color: const Color.fromRGBO(250, 80, 100, 1),
borderRadius:
BorderRadius.all(const Radius.circular(30)),
),
child: Text(
"Next",
style: TextStyle(
color: const Colors.white,
fontSize: 20,
fontWeight: FontWeight.w500,
letterSpacing: 0.3,
),
),
),
),
),
],
),
),
),
),
),
);
}
Widget _buildPictureBox() {
return Container(
child: InkWell(
onTap: () {
getImage(context);
},
child: Padding(
padding: EdgeInsets.only(left: 10),
child: Container(
width: MediaQuery.of(context).size.width / 3.3,
height: MediaQuery.of(context).size.height / 7,
color: Colors.black12,
child: Center(
child: (_image != null)
? Image.file(_image, fit: BoxFit.fill)
: Icon(
Icons.camera_alt,
color: Colors.black26,
),
),
),
),
),
);
}
}