iframe 1.0.0
iframe: ^1.0.0 copied to clipboard
A comprehensive Flutter plugin that provides cross-platform iframe functionality for Android, iOS, Web, Linux, Windows, and macOS.
example/lib/main.dart
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:iframe/iframe.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _platformVersion = 'Unknown';
String _iframeStatus = 'Not created';
String _currentUrl = '';
final TextEditingController _urlController = TextEditingController(text: 'https://flutter.dev');
final TextEditingController _scriptController = TextEditingController(text: 'document.title');
@override
void initState() {
super.initState();
initPlatformState();
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState() async {
String platformVersion;
// Platform messages may fail, so we use a try/catch PlatformException.
// We also handle the message potentially returning null.
try {
platformVersion =
await Iframe.getIframePlatformVersion() ?? 'Unknown platform version';
} on PlatformException {
platformVersion = 'Failed to get platform version.';
}
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) return;
setState(() {
_platformVersion = platformVersion;
});
}
Future<void> _createIframe() async {
try {
final result = await Iframe.createIframe(
url: _urlController.text,
width: 400,
height: 300,
);
setState(() {
_iframeStatus = result ?? 'Iframe created';
});
} on PlatformException catch (e) {
setState(() {
_iframeStatus = 'Failed to create iframe: ${e.message}';
});
}
}
Future<void> _navigate() async {
try {
final result = await Iframe.navigateTo(_urlController.text);
setState(() {
_iframeStatus = result ?? 'Navigated';
});
} on PlatformException catch (e) {
setState(() {
_iframeStatus = 'Failed to navigate: ${e.message}';
});
}
}
Future<void> _reload() async {
try {
final result = await Iframe.reloadIframe();
setState(() {
_iframeStatus = result ?? 'Reloaded';
});
} on PlatformException catch (e) {
setState(() {
_iframeStatus = 'Failed to reload: ${e.message}';
});
}
}
Future<void> _getCurrentUrl() async {
try {
final url = await Iframe.getCurrentIframeUrl();
setState(() {
_currentUrl = url ?? 'Unknown';
});
} on PlatformException catch (e) {
setState(() {
_currentUrl = 'Failed to get URL: ${e.message}';
});
}
}
Future<void> _executeScript() async {
try {
final result = await Iframe.executeIframeScript(_scriptController.text);
setState(() {
_iframeStatus = result ?? 'Script executed';
});
} on PlatformException catch (e) {
setState(() {
_iframeStatus = 'Failed to execute script: ${e.message}';
});
}
}
Future<void> _toggleVisibility() async {
try {
// This is a simple toggle - in a real app, you would track visibility state
await Iframe.setIframeVisible(true);
setState(() {
_iframeStatus = 'Visibility toggled';
});
} on PlatformException catch (e) {
setState(() {
_iframeStatus = 'Failed to toggle visibility: ${e.message}';
});
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Iframe Plugin Example'),
),
body: SingleChildScrollView(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('Running on: $_platformVersion\n'),
const Divider(),
Text('Iframe Status: $_iframeStatus'),
const SizedBox(height: 10),
Text('Current URL: $_currentUrl'),
const SizedBox(height: 20),
TextField(
controller: _urlController,
decoration: const InputDecoration(
labelText: 'URL',
border: OutlineInputBorder(),
),
),
const SizedBox(height: 10),
Row(
children: [
ElevatedButton(
onPressed: _createIframe,
child: const Text('Create Iframe'),
),
const SizedBox(width: 10),
ElevatedButton(
onPressed: _navigate,
child: const Text('Navigate'),
),
const SizedBox(width: 10),
ElevatedButton(
onPressed: _reload,
child: const Text('Reload'),
),
],
),
const SizedBox(height: 10),
Row(
children: [
ElevatedButton(
onPressed: _getCurrentUrl,
child: const Text('Get Current URL'),
),
const SizedBox(width: 10),
ElevatedButton(
onPressed: _toggleVisibility,
child: const Text('Toggle Visibility'),
),
],
),
const SizedBox(height: 20),
TextField(
controller: _scriptController,
decoration: const InputDecoration(
labelText: 'JavaScript to Execute',
border: OutlineInputBorder(),
),
),
const SizedBox(height: 10),
ElevatedButton(
onPressed: _executeScript,
child: const Text('Execute Script'),
),
const SizedBox(height: 20),
const Text(
'Note: Due to security restrictions, some features may not work on all platforms or with all websites.',
style: TextStyle(fontSize: 12, color: Colors.grey),
),
],
),
),
),
);
}
}