7
votes
       File profileImg;

  Widget profilePic() {
        return Stack(
            children: <Widget>[
              new Image.file(profileImg),
              Positioned(
                left: 50.0,
                right: 50.0,
                bottom: 40.0,
                height: 64.0,

                child: RaisedGradientButton(
                  onPressed: () async {
                  File image= await ImagePicker.pickImage(source:ImageSource.gallery);
                  image=profileImg;
                   print(image.path);
                  },
                  child: new Text(
                    "Upload",
                    style: TextStyle(fontSize: 20.0, color: Colors.white),
                  ),
                  gradient: LinearGradient(
                    colors: <Color>[
                      const Color(0xFF000000),
                      const Color(0xFF000000),
                      const Color(0xFF40079B)
                    ],
                  ),
                ), // child widget
              ),
            ]);
      }

I have created one image and at the bottom of the image i have one button to select image from the gallery.Before selecting the image from gallery i want to set background color for the image after selecting the image from gallery i want to set the selected image in the imageview

1

1 Answers

5
votes

A Minimal Example Of Using Image_picker & FutureBuilder for showing the picked Gallery Image.

Future<File> profileImg;

@override
  Widget build(BuildContext context) {
    return new Scaffold(
        appBar: AppBar(
          title: Text("Gallery Image Picker"),
        ),
        body: profilePic());
  }

Widget profilePic() {
    return ListView(children: <Widget>[
      FutureBuilder(
        builder: (context, data) {
          if (data.hasData) {
            return Container(
              height: 200.0,
              child: Image.file(
                data.data,
                fit: BoxFit.contain,
                height: 200.0,
              ),
              color: Colors.blue,
            );
          }
          return Container(
            height: 200.0,
            child: Image.network('https://via.placeholder.com/150'),
            color: Colors.blue,
          );
        },
        future: profileImg,
      ),
      RaisedButton(
        color: Colors.blue,
        onPressed: () {
          profileImg = ImagePicker.pickImage(source: ImageSource.gallery)
              .whenComplete(() {
            setState(() {});
          });
        },
        child: new Text(
          "Pick Gallery Image",
          style: TextStyle(fontSize: 20.0, color: Colors.white),
        ),
      ),
    ]);
  }