0

I have a stateful widget called AddPhotosView that contains all the logic i am trying to achieve, i am using this package photo_manager to display images in the user's devices in a GridView.builder and i am trying to use image_cropper to be able to crop the image the user selects in the from the grid view.

This is the full code of my stateful widget :

class AddPhotosView extends StatefulWidget {
  @override
  State<AddPhotosView> createState() => _AddPhotosView();
}

class _AddPhotosView extends State<AddPhotosView> {
  List<Widget> _mediaList = [];
  int currentPage = 0;
  int lastPage;

  @override
  void initState() {
    super.initState();
    _fetchNewMedia();
  }

  _handleScrollEvent(ScrollNotification scroll) {
    if (scroll.metrics.pixels / scroll.metrics.maxScrollExtent > 0.33) {
      if (currentPage != lastPage) {
        _fetchNewMedia();
      }
    }
  }

  _fetchNewMedia() async {
    var result = await PhotoManager.requestPermissionExtend();
    if (result != null) {
      // success
//load the album list
      List<AssetPathEntity> albums =
          await PhotoManager.getAssetPathList(onlyAll: true);
      print(albums);
      List<AssetEntity> media =
          await albums[0].getAssetListPaged(page: currentPage, size: 60);
      print(media);
      List<Widget> temp = [];
      for (var asset in media) {
        temp.add(
          FutureBuilder(
            future: asset.thumbnailDataWithSize(
              ThumbnailSize(200, 200),
            ),
            builder: (BuildContext context, snapshot) {
              if (snapshot.connectionState == ConnectionState.done)
                return GestureDetector(
                  onTap: () async {
                    File file = await asset.file;
                    print(file.toString());
                  },
                  child: Stack(
                    children: <Widget>[
                      Positioned.fill(
                        child: Image.memory(
                          snapshot.data,
                          fit: BoxFit.cover,
                        ),
                      ),
                      if (asset.type == AssetType.video)
                        Align(
                          alignment: Alignment.bottomRight,
                          child: Padding(
                            padding: EdgeInsets.only(right: 5, bottom: 5),
                            child: Icon(
                              Icons.videocam,
                              color: Colors.white,
                            ),
                          ),
                        ),
                    ],
                  ),
                );
              return Container();
            },
          ),
        );
      }

      setState(() {
        _mediaList.addAll(temp);
        currentPage++;
      });
    } else {
      // fail
      PhotoManager.openSetting();
    }
  }

  @override
  Widget build(BuildContext context) {
    // final controller = Get.put(EServicesController());
    return Scaffold(
      appBar: AppBar(
        toolbarHeight: 50,
        backgroundColor: Colors.white,
        title: Column(
          children: [
            Text(
              "Add Photos".tr,
              style: GoogleFonts.poppins(
                  color: Color(0xff000000),
                  fontSize: 16,
                  fontWeight: FontWeight.w600),
            ),
          ],
        ),
        centerTitle: false,
        elevation: 0.5,
        automaticallyImplyLeading: false,
        leadingWidth: 15,
        leading: new IconButton(
          icon: new Icon(Icons.arrow_back_ios, color: Color(0xff3498DB)),
          onPressed: () => {Get.back()},
        ),
      ),
      body: Column(
        children: [
          Container(
            color: Colors.white,
            child: Padding(
              padding: const EdgeInsets.only(
                  left: 45.0, right: 45, top: 22, bottom: 35),
              child: Container(
                height: 280,
                width: 280,
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(10),
                ),
                child: Stack(
                  fit: StackFit.expand,
                  children: [
                    Image.asset(
                      'assets/icon/image-test.png',
                      fit: BoxFit.cover,
                    ),
                    ColorFiltered(
                      colorFilter: ColorFilter.mode(
                          Colors.white.withOpacity(0.3),
                          BlendMode.srcOut), // This one will create the magic
                      child: Stack(
                        fit: StackFit.expand,
                        children: [
                          Container(
                            decoration: BoxDecoration(
                                color: Colors.black,
                                backgroundBlendMode: BlendMode
                                    .dstOut), // This one will handle background + difference out
                          ),
                          Align(
                            alignment: Alignment.topCenter,
                            child: Container(
                              margin: const EdgeInsets.only(top: 80),
                              height: 200,
                              width: 200,
                              decoration: BoxDecoration(
                                color: Colors.blue,
                                borderRadius: BorderRadius.circular(100),
                              ),
                            ),
                          ),
                        ],
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ),
          Expanded(
            child: NotificationListener<ScrollNotification>(
              onNotification: (ScrollNotification scroll) {
                _handleScrollEvent(scroll);
                return;
              },
              child: Padding(
                padding: const EdgeInsets.all(22.0),
                child: GridView.builder(
                    itemCount: _mediaList.length,
                    gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                        crossAxisCount: 3),
                    itemBuilder: (BuildContext context, int index) {
                      return _mediaList[index];
                    }),
              ),
            ),
          ),
        ],
      ),

      //bottomNavigationBar: SendUpdateButton(),
    );
  }
}

My photo_manager package is working and displaying my images correctly, but i want to more with it.

Want i am trying to achieve :

  • Get the image path of an image selected by the user with the photo_manager package.

  • I also want to be able select multiple images at once or a single image when tapped from the grid view.

  • Then render the image selected by the user in the Container carrying the image.asset() widget.

  • Crop the image using the flutter package image_cropper.

Getting the file path of an image selected by the user is very important as i believe it can help me achieve many other logic.

I have tried getting the image path by wrapping the container carrying the image.asset with

GestureDetector(
                  onTap: () async {
                    File file = await asset.file;
                    print(file.toString());
                  },

But it don't to work :(

Please i would be entirely grateful if someone help me out with what i am trying to achieve as i have been struggling to achieve this for sometime now in my project.

NB: I am using the package: photo_manager because it helps me to customise the UI of where the user's gallery can be displayed, other packages has an out-of-the-box UI which i don't want. So i will be open to using another package for only cropping the image if possible.

tesseract
  • 144
  • 1
  • 12

0 Answers0