Как использовать getImage () в другом классе во флаттере? - PullRequest
0 голосов
/ 23 сентября 2019

Как я могу получить getImage в class Picture?

Я немного понимаю global key, я пробовал много способов, но все еще не могу исправить свой код.

Мойполный код:

import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:firebase_storage/firebase_storage.dart';
import 'package:path/path.dart';
import 'dart:io';
import 'package:socialapp/page/writeprofile2.dart';


final key = GlobalKey<writeprofileState>();

class writeprofile extends StatefulWidget {
  const writeprofile({Key key}):super(key:key);

  @override
  writeprofileState createState() => writeprofileState();
}

class writeprofileState extends State<writeprofile> {
  File image;
  final formKey = GlobalKey<FormState>();

  @override
  Widget build(BuildContext context) {
    Future getImage() async{
      var getimage= await ImagePicker.pickImage(source: ImageSource.gallery);
      setState(() {
        image = getimage;
        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(
            key: formKey,
            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: (){Navigator.push(context, MaterialPageRoute(builder: (context) => writeprofile2()));},
                    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();
}
var _writeprofile = writeprofileState();

class _PictureBoxState extends State<PictureBox>{

  @override
  Widget build(BuildContext context) {
    return Container(
      child: InkWell(
        onTap: (){},
        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: (_writeprofile.image!=null)? Image.file(_writeprofile.image, fit:BoxFit.fill)
                  :Icon(
                Icons.camera_alt,
                color: Colors.black26,
              ),
            ),
          ),
        ),
      ),
    );
  }
}
...