Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter set image after selecting from gallery

Tags:

flutter

       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

like image 466
Vishali Avatar asked Jan 10 '19 09:01

Vishali


People also ask

How do I select an image from a list in Flutter?

You can make the image selectable by adding GestureDetector and changing the _selectedImageIndex using setState on each tap.


1 Answers

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),
        ),
      ),
    ]);
  }
like image 141
anmol.majhail Avatar answered Oct 19 '22 16:10

anmol.majhail