import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:webview_flutter/webview_flutter.dart';
class GroupWebViewBox extends StatefulWidget {
final String url;
GroupWebViewBox({Key key, @required this.url}):super(key: key);
@override
_GroupWebViewBoxState createState() => _GroupWebViewBoxState();
}
class _GroupWebViewBoxState extends State<GroupWebViewBox> {
bool isLoading = true;
WebViewController _webViewController;
/*写入token到Cookie*/
// setcookie() async {
// if (_bloc.state.user != null) {
// _webViewController.evaluateJavascript('document.cookie="token=${_bloc.state.user.token};"');
// _webViewController.evaluateJavascript('document.cookie="app.domain=$host;"');
// }
// }
/*写入token到localStorage*/
setlocalStorage() async {
final prefs = await SharedPreferences.getInstance();
var token = prefs.getString('token');
_webViewController.evaluateJavascript(
"""
function setToken() {
setTimeout(function() {
if(localStorage) {
localStorage.setItem('token','$token');
}else {
setToken();
}
}, 100);
}
setToken();
"""
);
}
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
centerTitle: true,
title: Text('xxx', style: TextStyle(color: Color(0xff4a4a4a), fontSize: 16, fontWeight: FontWeight.w500)),
leading: goBackArr(context),
bottom: PreferredSize(
preferredSize: Size.fromHeight(2),
child: Offstage(
offstage: !isLoading,
child: SizedBox(
height: 2,
width: MediaQuery.of(context).size.width,
child: LinearProgressIndicator(),
),
),
),
),
body: WebView(
onWebViewCreated: (WebViewController web) {
_webViewController = web;
setState(() {
isLoading = true;
});
},
onPageFinished: (String value) {
setState(() {
isLoading = false;
});
setlocalStorage();
},
initialUrl: widget.url,
javascriptMode: JavascriptMode.unrestricted,
),
);
}
}