Class 如何从颤振中的另一个类访问类方法?

Class 如何从颤振中的另一个类访问类方法?,class,flutter,key,global,Class,Flutter,Key,Global,嗨,伙计们,我是新来的 我做了照片上传功能 但我在做的时候遇到了麻烦 “类pictureBox()”无法识别“getImage()” 这是我的全部代码: class writeprofile extends StatefulWidget { @override _writeprofileState createState() => _writeprofileState(); } class _writeprofileState extends State<writeprof

嗨,伙计们,我是新来的

我做了照片上传功能

但我在做的时候遇到了麻烦

“类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,
              ),
            ),
          ),
        ),
      ),
    );
  }
}
类writeprofile扩展StatefulWidget{
@凌驾
_writeprofileState createState()=>_writeprofileState();
}
类_writeprofileState扩展状态{
文件图像;
@凌驾
小部件构建(构建上下文){
Future getImage()异步{
var image=wait ImagePicker.pickImage(源:ImageSource.gallery);
设置状态(){
_图像=图像;
打印(“图像路径$_图像”);
});
}
未来上传PIC(BuildContext上下文)异步{
字符串filName=basename(_image.path);
StorageReference firebaseStorageRef=FirebaseStorage.instance.ref().child(filName);
StorageUploadTask uploadTask=firebaseStorageRef.putFile(_image);
StorageTaskSnapshot taskSnapshot=等待上载Task.onComplete;
设置状态(){
打印(“个人资料图片上传!!”;
showSnackBar(SnackBar(内容:Text('Profile pic Upload!!'));
});
}
返回脚手架(
车身:建造商(
生成器:(上下文)=>中心(
子:容器(
子:容器(
子:列(
儿童:[
填充物(
填充:仅限边缘设置(顶部:70),
子:文本(
'사진 선택',
样式:TextStyle(
尺寸:30,
fontWeight:fontWeight.w500,
),
),
),
填充物(
填充:仅限边缘设置(顶部:10,底部:50),
子:容器(
身高:1,,
宽度:MediaQuery.of(context).size.width/1.4,
颜色:颜色。黑色,
),
),
容器(
宽度:MediaQuery.of(context).size.width/1.5,
高度:MediaQuery.of(context).size.height/15,
对齐:分馏loffset.center,
装饰:盒子装饰(
颜色:常量颜色。来自RGBO(250,80,120,1),
borderRadius:borderRadius.all(常数半径圆形(30)),
),
子:文本(
"가이드 라인을 읽어주세요 !",
样式:TextStyle(
颜色:颜色,白色,
尺寸:20,
fontWeight:fontWeight.w300,
字母间距:0.3,
),
),
),
填充物(
填充:仅限常量边集(顶部:100),
孩子:排(
儿童:[
PictureBox(),
PictureBox(),
PictureBox(),
],
),
),
填充物(
填充:仅限常量边集(顶部:10),
孩子:排(
儿童:[
PictureBox(),
PictureBox(),
PictureBox(),
],
),
),
墨水池(
onTap:(){uploadPic(上下文);},
孩子:填充(
填充:仅限边缘设置(顶部:50),
子:容器(
宽度:MediaQuery.of(context).size.width/3,
高度:MediaQuery.of(context).size.height/20,
对齐:分馏loffset.center,
装饰:盒子装饰(
颜色:常量颜色。来自RGBO(250,80,100,1),
borderRadius:borderRadius.all(常数半径圆形(30)),
),
子:文本(
“下一步”,
样式:TextStyle(
颜色:颜色,白色,
尺寸:20,
fontWeight:fontWeight.w500,
字母间距:0.3,
),
),
),
),
),
],
),
),
),
),
),
);
}
}
类PictureBox扩展StatefulWidget{
@凌驾
_PictureBoxState createState();
}
类_PictureBoxState扩展状态{
@凌驾
小部件构建(构建上下文){
返回容器(
孩子:InkWell(
onTap:(){getImage(context);},
孩子:填充(
填充:仅限边缘设置(左:10),
子:容器(
宽度:MediaQuery.of(context).size.width/3.3,
高度:MediaQuery.of(context).size.height/7,
颜色:颜色。黑色,
儿童:中心(
子项:(\u image!=null)?image.file(\u image,fit:BoxFit.fill)
:图标(
Icons.camera\u alt,
颜色:颜色。黑色,
),
),
),
),
),
);
}
}
这是我的代码,我想在PictureBox中使用“Future getImage()”如何在PictureBox中运行getImage()

如何在课堂图片中使用getImage

我有点理解全局密钥,我尝试了很多方法,但仍然无法修复我的代码

 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;
                                    });
                                  },
                                ),
                              ],
                            ),
                          );
                        });
                  },
                ),
              ),
            ),
          ),
        ],
      ),
上传图像代码

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);
上传图像代码

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);

试试这个,我刚刚调整了你的代码。还可以看看命名类的CamelCase约定

<