feat: start local server for iOS
This commit is contained in:
@@ -14,6 +14,7 @@ class _RichEditorState extends State<RichEditor> {
|
||||
WebViewController? _controller;
|
||||
String text = "";
|
||||
final Key _mapKey = UniqueKey();
|
||||
String assetPath = 'packages/rich_editor/assets/editor/editor.html';
|
||||
|
||||
int port = 5321;
|
||||
LocalServer? localServer;
|
||||
@@ -22,6 +23,41 @@ class _RichEditorState extends State<RichEditor> {
|
||||
void initState() {
|
||||
super.initState();
|
||||
if (Platform.isAndroid) WebView.platform = SurfaceAndroidWebView();
|
||||
if (!Platform.isAndroid) {
|
||||
initServer();
|
||||
}
|
||||
}
|
||||
|
||||
initServer() {
|
||||
localServer = LocalServer(port);
|
||||
localServer!.start(handleRequest);
|
||||
}
|
||||
|
||||
void handleRequest(HttpRequest request) {
|
||||
try {
|
||||
if (request.method == 'GET' &&
|
||||
request.uri.queryParameters['query'] == "getRawTeXHTML") {
|
||||
} else {}
|
||||
} catch (e) {
|
||||
print('Exception in handleRequest: $e');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
if (_controller != null) {
|
||||
_controller = null;
|
||||
}
|
||||
if (!Platform.isAndroid) {
|
||||
localServer!.close();
|
||||
}
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
_loadHtmlFromAssets() async {
|
||||
final filePath = assetPath;
|
||||
_controller!.loadUrl("http://localhost:$port/$filePath");
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -31,14 +67,24 @@ class _RichEditorState extends State<RichEditor> {
|
||||
GroupedTab(controller: _controller),
|
||||
Flexible(
|
||||
child: WebView(
|
||||
initialUrl:
|
||||
'file:///android_asset/flutter_assets/packages/rich_editor/assets/editor/editor.html',
|
||||
key: _mapKey,
|
||||
// initialUrl:
|
||||
// 'file:///android_asset/flutter_assets/packages/rich_editor/assets/editor/editor.html',
|
||||
onWebViewCreated: (WebViewController controller) {
|
||||
_controller = controller;
|
||||
print('WebView created');
|
||||
setState(() {});
|
||||
if (!Platform.isAndroid) {
|
||||
print('Loading');
|
||||
_loadHtmlFromAssets();
|
||||
} else {
|
||||
_controller!.loadUrl('file:///android_asset/flutter_assets/$assetPath');
|
||||
}
|
||||
},
|
||||
javascriptMode: JavascriptMode.unrestricted,
|
||||
onWebResourceError: (e) {
|
||||
print("error ${e.description}");
|
||||
},
|
||||
),
|
||||
// child: InAppWebView(
|
||||
// initialFile: 'packages/rich_editor/assets/editor/index.html',
|
||||
|
||||
Reference in New Issue
Block a user