flutter_dns_adblocker 0.0.1
flutter_dns_adblocker: ^0.0.1 copied to clipboard
A Flutter package for DNS-based ad blocking in WebViews using flutter_inappwebview. Block ads by filtering DNS requests with comprehensive domain blocklists.
import 'package:flutter/material.dart';
import 'package:flutter_dns_adblocker/flutter_dns_adblocker.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'DNS AdBlocker Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const AdBlockerDemo(),
);
}
}
class AdBlockerDemo extends StatefulWidget {
const AdBlockerDemo({super.key});
@override
State<AdBlockerDemo> createState() => _AdBlockerDemoState();
}
class _AdBlockerDemoState extends State<AdBlockerDemo> {
final TextEditingController _urlController = TextEditingController(
text: 'https://www.theverge.com',
);
bool _adBlockingEnabled = true;
int _blockedAdsCount = 0;
bool _isLoading = false;
String? _currentUrl;
// Predefined test URLs
final List<Map<String, String>> _testUrls = [
{'name': 'The Verge', 'url': 'https://www.theverge.com'},
{'name': 'CNN', 'url': 'https://www.cnn.com'},
{'name': 'Forbes', 'url': 'https://www.forbes.com'},
{'name': 'TechCrunch', 'url': 'https://techcrunch.com'},
{'name': 'Google', 'url': 'https://www.google.com'},
];
@override
void dispose() {
_urlController.dispose();
super.dispose();
}
void _loadUrl() {
setState(() {
_currentUrl = _urlController.text;
_blockedAdsCount = 0;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: const Text('DNS AdBlocker Demo'),
),
body: Column(
children: [
// Controls Panel
Container(
padding: const EdgeInsets.all(16),
color: Colors.grey[100],
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// URL Input
Row(
children: [
Expanded(
child: TextField(
controller: _urlController,
decoration: const InputDecoration(
labelText: 'Enter URL',
border: OutlineInputBorder(),
prefixIcon: Icon(Icons.link),
contentPadding: EdgeInsets.symmetric(
horizontal: 12,
vertical: 8,
),
),
onSubmitted: (_) => _loadUrl(),
),
),
const SizedBox(width: 8),
ElevatedButton.icon(
onPressed: _loadUrl,
icon: const Icon(Icons.refresh),
label: const Text('Load'),
),
],
),
const SizedBox(height: 12),
// Quick Links
Wrap(
spacing: 8,
runSpacing: 8,
children: _testUrls.map((urlData) {
return ActionChip(
label: Text(urlData['name']!),
onPressed: () {
_urlController.text = urlData['url']!;
_loadUrl();
},
);
}).toList(),
),
const SizedBox(height: 12),
// Ad Blocking Toggle & Stats
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Row(
children: [
Switch(
value: _adBlockingEnabled,
onChanged: (value) {
setState(() {
_adBlockingEnabled = value;
_blockedAdsCount = 0;
});
},
),
const SizedBox(width: 8),
Text(
'Ad Blocking: ${_adBlockingEnabled ? 'ON' : 'OFF'}',
style: const TextStyle(
fontWeight: FontWeight.bold,
fontSize: 16,
),
),
],
),
Container(
padding: const EdgeInsets.symmetric(
horizontal: 16,
vertical: 8,
),
decoration: BoxDecoration(
color: _adBlockingEnabled ? Colors.green : Colors.grey,
borderRadius: BorderRadius.circular(20),
),
child: Row(
children: [
const Icon(
Icons.block,
color: Colors.white,
size: 20,
),
const SizedBox(width: 8),
Text(
'Blocked: $_blockedAdsCount',
style: const TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
),
),
],
),
),
],
),
// Loading Indicator
if (_isLoading)
const Padding(
padding: EdgeInsets.only(top: 8),
child: LinearProgressIndicator(),
),
],
),
),
// WebView
Expanded(
child: _currentUrl == null
? Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(Icons.web, size: 100, color: Colors.grey[300]),
const SizedBox(height: 16),
Text(
'Enter a URL and tap Load to get started',
style: TextStyle(
fontSize: 16,
color: Colors.grey[600],
),
),
const SizedBox(height: 24),
const Text(
'Try loading ad-heavy sites like news websites\nto see the ad blocker in action!',
textAlign: TextAlign.center,
style: TextStyle(fontSize: 14, color: Colors.grey),
),
],
),
)
: AdBlockingWebView(
key: ValueKey('$_currentUrl-$_adBlockingEnabled'),
initialUrl: _currentUrl,
config: AdBlockerConfig(
enabled: _adBlockingEnabled,
trackBlockedAds: true,
debugMode: true,
),
onAdBlocked: (url, totalBlocked) {
setState(() {
_blockedAdsCount = totalBlocked;
});
},
onLoadStart: (url) {
setState(() {
_isLoading = true;
});
},
onLoadStop: (url) {
setState(() {
_isLoading = false;
});
},
onLoadError: (url, code, message) {
setState(() {
_isLoading = false;
});
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Error loading page: $message'),
backgroundColor: Colors.red,
),
);
},
),
),
],
),
);
}
}