pichillilorenzo / flutter_inappwebview

A Flutter plugin that allows you to add an inline webview, to use a headless webview, and to open an in-app browser window.
https://inappwebview.dev
Apache License 2.0
3.31k stars 1.64k forks source link

inappview with uni_links #424

Closed no1-knows closed 4 years ago

no1-knows commented 4 years ago

Hello! I'm newbie for smartphone app. I would like to open the webpage at inappview which is as same as universal links url on mail app.

It's kind of like below. If I got mail with authentication links like "http://example.com/sign_in/wtCpS0ObF-UMjvzev" and I tap it, then I would like to open my app and show the above page. But my app doesn't change the page.

What is done

Any information would greatly help.

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:uni_links/uni_links.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
Future main() async {
  WidgetsFlutterBinding.ensureInitialized();
  runApp(new MyApp());
}
class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => new _MyAppState();
}
class _MyAppState extends State<MyApp> with SingleTickerProviderStateMixin {
  InAppWebViewController webView;
  String url = "";
  double progress = 0;
  StreamSubscription _sub;
  StreamController<String> _controller = StreamController<String>.broadcast();
  String _latestLink = 'Unknown';
  @override
  void initState() {
    super.initState();
  }
  @override
  dispose() {
    if (_sub != null) _sub.cancel();
    super.dispose();
  }
  Future<Null> initUniLinks() async {
    _sub = getLinksStream().listen((String link) {
      if (!mounted) return;
      setState(() {
        _latestLink = link ?? 'Unknown';
      });
    }, onError: (err) {
      if (!mounted) return;
      setState(() {
        _latestLink = 'Failed to get latest link: $err.';
      });
    });
    if (!mounted) return;
    _controller.add(_latestLink);
  }
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('InAppWebView Example'),
        ),
        body: Container(
            child: Column(children: <Widget>[
          Container(
              padding: EdgeInsets.all(5.0),
              child: progress < 1.0
                  ? LinearProgressIndicator(value: progress)
                  : Container()),
          Expanded(
            child: Container(
              decoration:
                  BoxDecoration(border: Border.all(color: Colors.blueAccent)),
              child: StreamBuilder(
                  stream: _controller.stream,
                  builder: (context, snapshot) {
                    initUniLinks();
                    if (snapshot.data == 'Unknown' || snapshot.data == null) {
                      url = "https://example.com";
                    } else {
                      url = snapshot.data;
                    }
//                    return Text(url);
                    return InAppWebView(
                      initialUrl: url,
                      initialHeaders: {},
                      initialOptions: InAppWebViewGroupOptions(
                          crossPlatform: InAppWebViewOptions(
                        debuggingEnabled: true,
                      )),
                      onWebViewCreated: (InAppWebViewController controller) {
                        webView = controller;
                      },
                      onLoadStart:
                          (InAppWebViewController controller, String url) {
                        setState(() {
                          this.url = url;
                        });
                      },
                      onLoadStop: (InAppWebViewController controller,
                          String url) async {
                        setState(() {
                          this.url = url;
                        });
                      },
                      onProgressChanged:
                          (InAppWebViewController controller, int progress) {
                        setState(() {
                          this.progress = progress / 100;
                        });
                      },
                    );
                  }),
            ),
          ),
        ])),
      ),
    );
  }
}
no1-knows commented 4 years ago

I'm sorry it's normal question. Not bug report....

no1-knows commented 4 years ago

This sample code works with uni_links!!

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:uni_links/uni_links.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';

Future main() async {
  WidgetsFlutterBinding.ensureInitialized();
  runApp(new MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: "newapp",
      debugShowCheckedModeBanner: false,
      home: HomeScreen(),
    );
  }
}

class HomeScreen extends StatefulWidget {
  @override
  _HomeScreenState createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen>
    with SingleTickerProviderStateMixin {
  InAppWebViewController webView;
  String url = "";
  double progress = 0;
  StreamSubscription _sub;

  @override
  void initState() {
    super.initState();
  }

  @override
  dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('InAppWebView Example'),
      ),
      body: Container(
          child: Column(children: <Widget>[
        Container(
            padding: EdgeInsets.all(5.0),
            child: progress < 1.0
                ? LinearProgressIndicator(value: progress)
                : Container()),
        Expanded(
          child: Container(
            decoration: BoxDecoration(border: Border.all(color: Colors.blue)),
            child: StreamBuilder(
                stream: getLinksStream(),
                builder: (context, snapshot) {
                  if (snapshot.hasData) {
                    url = snapshot.data;
                  }else{
                    url = "https://example.com/";
                  }

                  if (url != "") {
                    return InAppWebView(
                      initialUrl: url,
                      initialHeaders: {},
                      initialOptions: InAppWebViewGroupOptions(
                          crossPlatform: InAppWebViewOptions(
                        debuggingEnabled: true,
                      )),
                      onWebViewCreated: (InAppWebViewController controller) {
                        webView = controller;
                      },
                      onLoadStart:
                          (InAppWebViewController controller, String url) {
                        setState(() {
                          this.url = url;
                        });
                      },
                      onLoadStop: (InAppWebViewController controller,
                          String url) async {
                        setState(() {
                          this.url = url;
                        });
                      },
                      onProgressChanged:
                          (InAppWebViewController controller, int progress) {
                        setState(() {
                          this.progress = progress / 100;
                        });
                      },
                    );
                  }
                  return CircularProgressIndicator();
                }),
          ),
        ),
      ])),
    );
  }
}
github-actions[bot] commented 2 weeks ago

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug and a minimal reproduction of the issue.