New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Screenshot package stops working when using Google Ads #729
Comments
Hi @chinmaysarupria
Thank you! |
Hi @huycozy Here are the details:
I am unable to post output of |
@chinmaysarupria thanks for providing sample code but it doesn't work even with simple Sample codeimport 'dart:typed_data';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:screenshot/screenshot.dart';
import 'package:share_plus/share_plus.dart';
import 'package:path_provider/path_provider.dart';
import 'package:google_mobile_ads/google_mobile_ads.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
MobileAds.instance..initialize()..updateRequestConfiguration(
RequestConfiguration(
testDeviceIds: <String>[
'20FB23B5E117EC42488FBC0A024F306F',
],
)
);
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
debugShowCheckedModeBanner: false,
home: const HomeScreen(),
);
}
}
class HomeScreen extends StatefulWidget {
const HomeScreen({super.key});
@override
State<HomeScreen> createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
Future<void> _shareScreenshot(
BuildContext context, ScreenshotController controller) async {
double pixelRatio = MediaQuery.of(context).devicePixelRatio;
await controller
.capture(
delay: const Duration(milliseconds: 10),
pixelRatio: pixelRatio,
)
.then(
(Uint8List? image) async {
if (image != null) {
final directory = await getApplicationDocumentsDirectory();
final imagePath = await File('${directory.path}/image.png').create();
await imagePath.writeAsBytes(image);
await Share.shareFiles([imagePath.path]);
}
},
);
}
bool _isBannerAdReady = false;
BannerAd? _bannerAd;
@override
void initState() {
super.initState();
_bannerAd = BannerAd(
adUnitId: 'ca-app-pub-3940256099942544/6300978111', // test banner id
size: AdSize.banner,
request: const AdRequest(),
listener: BannerAdListener(
onAdLoaded: (ad) => setState(() {
_isBannerAdReady = true;
}),
onAdFailedToLoad: (ad, error) {
_isBannerAdReady = false;
ad.dispose();
},
),
);
// If load method is commented out then screenshot package will start working again
_bannerAd!.load();
}
@override
Widget build(BuildContext context) {
final ScreenshotController screenshotController = ScreenshotController();
return Scaffold(
appBar: AppBar(
title: const Text('Ads + Screenshot'),
actions: [
InkWell(
onTap: () => _shareScreenshot(context, screenshotController),
child: const Padding(
padding: EdgeInsets.all(16.0),
child: Icon(Icons.share),
),
),
],
),
body: Stack(
children: [
SizedBox(
width: double.infinity,
child: Screenshot(
controller: screenshotController,
child: Card(
margin: const EdgeInsets.all(32.0),
child: Padding(
padding: const EdgeInsets.all(32.0),
child: Column(
children: const [
Text('Ads + Screenshot'),
],
),
),
),
),
),
Align(
alignment: Alignment.bottomCenter,
child: Container(
color: Colors.red,
width: double.maxFinite,
height: 32,
),
),
// if (_isBannerAdReady)
// Align(
// alignment: Alignment.bottomCenter,
// child: SizedBox(
// width: _bannerAd!.size.width.toDouble(),
// height: _bannerAd!.size.height.toDouble(),
// child: AdWidget(ad: _bannerAd!),
// ),
// ),
],
),
);
}
}
Please double-check your implementation. In case it's screenshot package issue, please file a new issue in that repository for better support. |
@huycozy Yes, it will not work as the load method is being called. I had also added a comment just above it that if load method is commented out screenshot will work and if it is not then screenshot will stop working. _bannerAd!.load(); // Simply comment out this line and your code will also start working |
@chinmaysarupria I can reproduce this issue on both Android and iOS when running your sample code at ads_screenshot. When commenting out Labeling the issue for further insights from the team. Thank you! |
@huycozy I was trying a few things to see if it can be resolved. This is what I did: in my code, I was initializing screenshot controller in build method. Widget build(BuildContext context) {
final ScreenshotController screenshotController = ScreenshotController();
...
} I moved it outside final ScreenshotController screenshotController = ScreenshotController();
Widget build(BuildContext context) {
...
} and the issue got resolved. Screenshots started working again. I still do not know why it is working this way but this is what I found:
|
We’re closing this issue due to inactivity. If you’re still impacted, please create a new issue via the Developer Forum. |
I have an app in which I have integrated google ads as well as screenshot package to allow users to share a screenshot of a particular widget. Both packages independently work well. But screenshot package stops working if google ads are used.
In screenshot package, one has to wrap any widget with screenshot controller and needs to call capture method on this controller to generate an image or a snapshot of that widget which can then be shared. But
load
method onBannerAd
class is blocking image generation on screenshot package.Here is my code:
If I comment out this load method, then the screenshot works fine. Here is the code for screenshot package:
Plugin Version
2.3.0
Steps to Reproduce
Integrate
Expected results: Google ads and screenshot working fine
Actual results: Google ads
load
method breaking something on screenshot packageThe text was updated successfully, but these errors were encountered: