breakthrough at changing the contents of widget

This commit is contained in:
Juan Marulanda De Los Rios 2024-08-25 22:29:23 -04:00
parent 8baf399608
commit 47f24fe849
2 changed files with 47 additions and 30 deletions

View File

@ -5,6 +5,7 @@ import 'dart:ui_web' as ui;
import 'dart:html' as html;
import 'augment.dart';
import 'dart:js' as js;
// import 'package:flutter_web_plugins/flutter_web_plugins.dart';
class MailAddress {
final String? name;
@ -341,47 +342,59 @@ class EmailView extends StatefulWidget {
class _EmailViewState extends State<EmailView> {
late Key iframeKey;
late String currentContent;
late String viewTypeId;
@override
void initState() {
super.initState();
print(widget.id);
iframeKey = Key("iframe-${widget.id}");
ui.platformViewRegistry.registerViewFactory(
// 'html-view33'
iframeKey.toString(),
(int viewId) => html.IFrameElement()
..width = '100%'
..height = '100%'
..srcdoc = widget.emailContent
..style.border = 'none',
// ..style.width = '100%',
// ..style.height = '100%'
);
String currentContent = widget.emailContent;
viewTypeId = "iframe-${DateTime.now().millisecondsSinceEpoch}";
_registerViewFactory(currentContent);
}
void _registerViewFactory(String currentContent) {
setState(() {
viewTypeId = 'iframe-${DateTime.now().millisecondsSinceEpoch}';
ui.platformViewRegistry.registerViewFactory(
viewTypeId,
(int viewId) => html.IFrameElement()
..width = '100%'
..height = '100%'
..srcdoc = currentContent
..style.border = 'none');
});
}
@override
Widget build(BuildContext context) {
void _handleNumbering() {
// Check if the iframe content window is accessible
try {
js.context.callMethod('applyNumberingVisibility');
} catch (e) {
print('Error accessing iframe method: $e');
// Optionally, use postMessage for cross-origin communication
// js.context['iframeElement'].contentWindow.postMessage(jsify({'action': 'toggleNumbering'}), '*');
}
}
return Scaffold(
appBar: AppBar(
title: Text(widget.name),
),
body: Column(
children: [
EmailToolbar(onButtonPressed: _handleNumbering),
EmailToolbar(
onButtonPressed: () {
_registerViewFactory(r"""
<h1>Welcome to My Website</h1>
<p>This is a simple HTML page.</p>
<h2>What is HTML?</h2>
<p>HTML (HyperText Markup Language) is the most basic building block of the Web. It defines the meaning and structure of web content. Other technologies besides HTML are generally used to describe a web page's appearance/presentation (CSS) or functionality/behavior (JavaScript).</p>
<h3>Here's a simple list:</h3>
<ul>
<li>HTML elements are the building blocks of HTML pages</li>
<li>HTML uses tags like <code>&lt;tag&gt;</code> to organize and format content</li>
<li>CSS is used with HTML to style pages</li>
</ul>
<p>Copyright © 2023</p>
""");
print("change");
// widget.emailContent = r"
// "
},
),
Row(
// title of email
children: [
@ -420,9 +433,8 @@ class _EmailViewState extends State<EmailView> {
Expanded(
child: HtmlElementView(
key: iframeKey,
viewType: iframeKey.toString(),
// 'html-view33',
key: UniqueKey(),
viewType: viewTypeId,
),
),
],

View File

@ -23,6 +23,11 @@ class EmailToolbar extends StatelessWidget {
onPressed: onButtonPressed,
child: Text('Reload'),
),
Spacer(),
ElevatedButton(
onPressed: onButtonPressed,
child: Text('Options'),
)
],
);