3

我正在使用 flutter_native_admob 在我的颤振应用中显示广告,链接

很明显,为了防止广告在列表视图中一次又一次地加载,只需保存控制器并在显示广告时使用它,这将防止广告在列表视图中重新加载。

我正在这样做,但它无助于解决问题。

    class ActivityPage extends StatefulWidget {
  static const routeName = "/activityPage";

  @override
  _ActivityPageState createState() => _ActivityPageState();
}

class _ActivityPageState extends State<ActivityPage> {
  final GlobalKey<SummaryState> _summaryKey = GlobalKey<SummaryState>();

  final controller = NativeAdmobController();
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    PaymentProvider paymentProvider = Provider.of<PaymentProvider>(context);
    return ChangeNotifierProvider(
      create: (context) => MonthDayDataprovider(
        calendarIndex: (DateTime.now().year * 12 + DateTime.now().month),
        day: DateTime.now().day,
      ),
      child: Scaffold(
        appBar: AppBar(
          brightness: Brightness.light,
          backgroundColor: Color(0xFFFAFAFA),
          title: Text(
            "Activity",
            style: Theme.of(context).textTheme.headline6.copyWith(
                  color: Color(0xFFCE2234),
                ),
          ),
        ),
        backgroundColor: Color(0xFFFAFAFA),
        body: ListView(
          children: <Widget>[
            CalorieStatsCard(),
            BMICard(),
            (paymentProvider.subscription == true ||
                    paymentProvider.removeAds == true)
                ? null
                : NativeBannerAds(
                    controller: this.controller,
                  ),
            Calender(),
            Summary(
              key: this._summaryKey,
            ),
            (paymentProvider.subscription == true ||
                    paymentProvider.removeAds == true)
                ? null
                : NativeFullAds(
                    controller: this.controller,
                  )
          ].where((Object o) => o != null).toList(),
        ),
      ),
    );
  }
}

class NativeBannerAds extends StatefulWidget {
  final NativeAdmobController controller;

  const NativeBannerAds({
    Key key,
    @required this.controller,
  }) : super(key: key);

  @override
  _NativeBannerAdsState createState() => _NativeBannerAdsState();
}

class _NativeBannerAdsState extends State<NativeBannerAds> {
  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.white,
      height: 85,
      padding: EdgeInsets.all(12),
      child: NativeAdmob(
        // Your ad unit id
        adUnitID: 'ca-app-pub-xxxxxxxxxxxx/xxxxxxxxxxxxx',
        controller: this.widget.controller,
        error: Center(child: Text("Failed to load the ad")),
        type: NativeAdmobType.banner,
        options: NativeAdmobOptions(
          showMediaContent: true,
          headlineTextStyle: NativeTextStyle(
            backgroundColor: Colors.red,
            color: Colors.blue,
          ),
        ),
      ),
    );
  }
}

class NativeFullAds extends StatefulWidget {
  final NativeAdmobController controller;

  const NativeFullAds({
    Key key,
    @required this.controller,
  }) : super(key: key);
  @override
  _NativeFullAdsState createState() => _NativeFullAdsState();
}

class _NativeFullAdsState extends State<NativeFullAds> {
  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.white,
      height: 375,
      padding: EdgeInsets.all(8),
      child: NativeAdmob(
        // Your ad unit id
        adUnitID: 'ca-app-pub-xxxxxxxxxxxxx/xxxxxxxxxxxx',
        controller: this.widget.controller,
        error: Material(
          child: Center(
            child: Text("Failed to load the ad"),
          ),
        ),
        type: NativeAdmobType.full,
        options: NativeAdmobOptions(
          showMediaContent: true,
          headlineTextStyle: NativeTextStyle(
            backgroundColor: Colors.red,
            color: Colors.blue,
          ),
        ),
      ),
    );
  }
}

在这里,我将控制器保存到父小部件并将其传递给子小部件,但这并没有解决问题,滚动时广告仍会继续加载。

如何解决这个问题呢?

在此处输入图像描述

4

0 回答 0