1. import 'package:flutter/material.dart';
    2. import 'package:shared_preferences/shared_preferences.dart';
    3. import 'package:webview_flutter/webview_flutter.dart';
    4. class GroupWebViewBox extends StatefulWidget {
    5. final String url;
    6. GroupWebViewBox({Key key, @required this.url}):super(key: key);
    7. @override
    8. _GroupWebViewBoxState createState() => _GroupWebViewBoxState();
    9. }
    10. class _GroupWebViewBoxState extends State<GroupWebViewBox> {
    11. bool isLoading = true;
    12. WebViewController _webViewController;
    13. /*写入token到Cookie*/
    14. // setcookie() async {
    15. // if (_bloc.state.user != null) {
    16. // _webViewController.evaluateJavascript('document.cookie="token=${_bloc.state.user.token};"');
    17. // _webViewController.evaluateJavascript('document.cookie="app.domain=$host;"');
    18. // }
    19. // }
    20. /*写入token到localStorage*/
    21. setlocalStorage() async {
    22. final prefs = await SharedPreferences.getInstance();
    23. var token = prefs.getString('token');
    24. _webViewController.evaluateJavascript(
    25. """
    26. function setToken() {
    27. setTimeout(function() {
    28. if(localStorage) {
    29. localStorage.setItem('token','$token');
    30. }else {
    31. setToken();
    32. }
    33. }, 100);
    34. }
    35. setToken();
    36. """
    37. );
    38. }
    39. @override
    40. void initState() {
    41. super.initState();
    42. }
    43. @override
    44. Widget build(BuildContext context) {
    45. return Scaffold(
    46. appBar: AppBar(
    47. centerTitle: true,
    48. title: Text('xxx', style: TextStyle(color: Color(0xff4a4a4a), fontSize: 16, fontWeight: FontWeight.w500)),
    49. leading: goBackArr(context),
    50. bottom: PreferredSize(
    51. preferredSize: Size.fromHeight(2),
    52. child: Offstage(
    53. offstage: !isLoading,
    54. child: SizedBox(
    55. height: 2,
    56. width: MediaQuery.of(context).size.width,
    57. child: LinearProgressIndicator(),
    58. ),
    59. ),
    60. ),
    61. ),
    62. body: WebView(
    63. onWebViewCreated: (WebViewController web) {
    64. _webViewController = web;
    65. setState(() {
    66. isLoading = true;
    67. });
    68. },
    69. onPageFinished: (String value) {
    70. setState(() {
    71. isLoading = false;
    72. });
    73. setlocalStorage();
    74. },
    75. initialUrl: widget.url,
    76. javascriptMode: JavascriptMode.unrestricted,
    77. ),
    78. );
    79. }
    80. }