islamdidarmd / flutter_adblocker_webview

A webview implementation of in Flutter that blocks most of the ads that appear inside of the webpages
https://pub.dev/packages/adblocker_webview
BSD 3-Clause "New" or "Revised" License
10 stars 7 forks source link

AdBlockerWebview Not Blocking Ads in My Implementation #23

Closed trungtin2901 closed 2 months ago

trungtin2901 commented 2 months ago

Hi @islamdidarmd ,

I'm having trouble getting the AdBlockerWebview to block ads in my Flutter application. I've set up my project according to the instructions, but it doesn't seem to block any ads. Below is my implementation using three Dart files: main.dart, url_input.dart, and browser.dart.

main.dart

import 'package:adblocker_webview/adblocker_webview.dart';
import 'package:flutter/material.dart';
import 'browser.dart';
import 'url_input.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'AdBlocker Browser',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'AdBlocker Browser Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final _adBlockerWebviewController = AdBlockerWebviewController.instance;
  bool _showBrowser = false;
  String _url = "";
  late final Future<void> _adblockInitializer;

  @override
  void initState() {
    super.initState();
    _adblockInitializer = _adBlockerWebviewController.initialize();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text(widget.title)),
      floatingActionButton: _showBrowser
          ? FloatingActionButton(
              child: const Icon(Icons.edit),
              onPressed: () {
                setState(() {
                  _showBrowser = false;
                });
              },
            )
          : null,
      body: _showBrowser
          ? FutureBuilder<void>(
              future: _adblockInitializer,
              builder: (_, snapshot) {
                if (snapshot.connectionState == ConnectionState.waiting) {
                  return Center(child: CircularProgressIndicator());
                } else if (snapshot.connectionState == ConnectionState.done) {
                  return Browser(
                    url: _url,
                    adBlockerWebviewController: _adBlockerWebviewController,
                    shouldBlockAds: true, // Always block ads
                    onExit: () {
                      setState(() {
                        _showBrowser = false;
                      });
                    },
                  );
                } else {
                  return const SizedBox();
                }
              },
            )
          : UrlInput(
              onSubmit: (url) {
                setState(() {
                  _url = url;
                  _showBrowser = true;
                });
              },
            ),
    );
  }
}

url_input.dart

import 'package:flutter/material.dart';

class UrlInput extends StatefulWidget {
  final Function(String) onSubmit;

  const UrlInput({
    Key? key,
    required this.onSubmit,
  }) : super(key: key);

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

class _UrlInputState extends State<UrlInput> {
  final TextEditingController _controller = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.all(16.0),
      child: Column(
        children: [
          TextField(
            controller: _controller,
            decoration: const InputDecoration(
              labelText: 'Enter URL',
            ),
            onSubmitted: widget.onSubmit,
          ),
          ElevatedButton(
            onPressed: () {
              widget.onSubmit(_controller.text);
            },
            child: const Text('Go'),
          ),
        ],
      ),
    );
  }
}

browser.dart

import 'package:flutter/material.dart';
import 'package:adblocker_webview/adblocker_webview.dart';

class Browser extends StatelessWidget {
  final String url;
  final AdBlockerWebviewController adBlockerWebviewController;
  final bool shouldBlockAds;
  final VoidCallback onExit;

  const Browser({
    Key? key,
    required this.url,
    required this.adBlockerWebviewController,
    required this.shouldBlockAds,
    required this.onExit,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        AdBlockerWebview(
          url: Uri.parse(url),
          adBlockerWebviewController: adBlockerWebviewController,
          shouldBlockAds: shouldBlockAds,
        ),
        Positioned(
          top: 20,
          right: 20,
          child: FloatingActionButton(
            onPressed: onExit,
            child: Icon(Icons.exit_to_app),
            backgroundColor: Colors.red,
          ),
        ),
      ],
    );
  }
}
islamdidarmd commented 2 months ago

Thank you for raising this issue. I will check it out.

islamdidarmd commented 2 months ago

Hi @trungtin2901, I tried the code you provided and this implementation is blocking ads.

Can you please provide which website you were trying to load and in which device?

islamdidarmd commented 2 months ago

I am closing this issue as there is no response. I suppose this package is working as intended. Feel free to let know if the issue persist. I will open this issue.