Click here to Skip to main content
15,905,420 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My flutter share button is running extremely slow. Reason I think is that it has to screenshot the image after getting from firebase for each specific news.(My Assumption)


Share button:

IconButton(
                    onPressed: () async {
                      final controller = ScreenshotController();
                      final bytes = await controller.captureFromWidget(
                        Material(
                          child: buildCard(
                              widget.data!.docs[widget.index!].get('url'),
                              widget.data!.docs[widget.index!].get('title'),
                              widget.data!.docs[widget.index!].get('body'),
                              widget.data!.docs[widget.index!].get('source'),
                              widget.data!.docs[widget.index!].get('link'),
                              context),
                        ),
                      );
                      final temp = await getTemporaryDirectory();
                      final path = '${temp.path}/image.png';
                      File(path).writeAsBytesSync(bytes);
                      await Share.shareFiles([path],
                          text: 'Check out this awesome news \n\n');
                    },
                    icon: Icon(Icons.share))


BuildCard:

Widget buildCard(String imageUrl, String Title, String body, String source,
      String newsLink, BuildContext context) {
    final deviceHeight = MediaQuery.of(context).size.height;
    final deviceWidth = MediaQuery.of(context).size.width;
    return RepaintBoundary(
      key: cardKey,
      child: Stack(children: [
        Container(
          height: deviceHeight,
          width: deviceWidth,
          color: Color(0xFFf1f0e9),
        ),
        Column(
          children: [
            Image.network(imageUrl),
            Container(
                margin: EdgeInsets.only(
                  left: 15,
                  right: 15,
                  top: 15,
                ),
                child: Text(Title, style: GoogleFonts.openSans(fontSize: 20))),
            SizedBox(
              height: 0.001 * deviceHeight,
            ),
            Container(
              margin: EdgeInsets.all(15),
              child: Text(body,
                  style: GoogleFonts.openSans(
                      fontWeight: FontWeight.w300, fontSize: 17)),
            ),
            Row(
              children: [
                SizedBox(
                  width: 0.04 * deviceWidth,
                ),
                Text('Source:$source ', style: TextStyle(fontSize: 10)),
                SizedBox(
                  width: 0.3 * deviceWidth,
                )
              ],
            ),
            Row(
              children: [
                SizedBox(
                  width: 0.02 * deviceWidth,
                ),
                TextButton(
                    onPressed: () async {
                      var link = newsLink;

                      var url = '$link';
                      if (await canLaunch(url)) {
                        await launch(url,
                            forceSafariVC: true,
                            forceWebView: true,
                            enableJavaScript: true);
                      }
                    },
                    child: Text('Read more')),
                SizedBox(
                  width: 0.3 * deviceWidth,
                ),
              ],
            ),
          ],
        ),
      ]),
    );
  }


What I have tried:

I have tried several ways but can't find a way which is specific to me and help with my code as it should be a dynamic share.
Posted
Comments
[no name] 20-Feb-23 14:07pm    
.writeAsBytesSync is not async. Unless the whole handler is async "void" (fire and forget), there's not much point in mixing sync and async in a button handler: it will just lock up until it finishes.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900