login_page.dart 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import 'dart:async';
  2. import 'package:flutter/material.dart';
  3. import '../model/constants.dart';
  4. import 'package:flutter/cupertino.dart';
  5. import 'dart:convert';
  6. import '../util/DataUtils.dart';
  7. // import 'package:flutter_webview_plugin/flutter_webview_plugin.dart';
  8. class LoginPage extends StatefulWidget {
  9. @override
  10. State<StatefulWidget> createState() => LoginPageState();
  11. }
  12. class LoginPageState extends State<LoginPage> {
  13. int count = 0;
  14. final int MAX_COUNT = 5;
  15. bool loading = true;
  16. GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey();
  17. late StreamSubscription<String> _onUrlChanged;
  18. // late StreamSubscription<WebViewStateChanged> _onStateChanged;
  19. // FlutterWebviewPlugin flutterWebViewPlugin = FlutterWebviewPlugin();
  20. @override
  21. void initState() {
  22. super.initState();
  23. // 监听WebView的加载事件,该监听器已不起作用,不回调
  24. // _onStateChanged = flutterWebViewPlugin.onStateChanged.listen((state) {});
  25. // _onUrlChanged = flutterWebViewPlugin.onUrlChanged.listen((url) {
  26. // setState(() {
  27. // loading = false;
  28. // });
  29. // if (url != null && url.length > 0 && url.contains("osc/osc.php?code=")) {
  30. // // android中onUrlChanged回调时页面已加载完成,由于onStateChanged回调不能用,这里延迟1秒去拿js中的数据,没拿到就再延迟1秒,最多取5次
  31. // Timer(const Duration(seconds: 1), parseResult);
  32. // }
  33. // });
  34. }
  35. // 解析webview中的数据
  36. void parseResult() {
  37. if (count > MAX_COUNT) {
  38. return;
  39. }
  40. // flutterWebViewPlugin.evalJavascript("get();").then((result) {
  41. // ++count;
  42. // // result json字符串,包含token信息
  43. // if (result != null && result.length > 0) {
  44. // // 拿到了js中的数据
  45. // try {
  46. // // what the fuck?? need twice decode??
  47. // var map = json.decode(result); // s is String
  48. // if (map is String) {
  49. // map = json.decode(map); // map is Map
  50. // }
  51. // if (map != null) {
  52. // // 登录成功,取到了token,关闭当前页面
  53. // DataUtils.saveLoginInfo(map);
  54. // Navigator.pop(context, "refresh");
  55. // }
  56. // } catch (e) {
  57. // print("parse login result error: $e");
  58. // }
  59. // } else {
  60. // // 没拿到js中的数据,延迟一秒再拿
  61. // Timer(const Duration(seconds: 1), parseResult);
  62. // }
  63. // });
  64. }
  65. @override
  66. Widget build(BuildContext context) {
  67. List<Widget> titleContent = [];
  68. titleContent.add(Text(
  69. "登录开源中国",
  70. style: TextStyle(color: Colors.white),
  71. ));
  72. if (loading) {
  73. titleContent.add(CupertinoActivityIndicator());
  74. }
  75. titleContent.add(Container(width: 50.0));
  76. return Container();
  77. // return WebviewScaffold(
  78. // key: _scaffoldKey,
  79. // url: Constants.LOGIN_URL,
  80. // appBar: AppBar(
  81. // title: Row(
  82. // mainAxisAlignment: MainAxisAlignment.center,
  83. // children: titleContent,
  84. // ),
  85. // iconTheme: IconThemeData(color: Colors.white),
  86. // ),
  87. // withZoom: true,
  88. // withLocalStorage: true,
  89. // withJavascript: true,
  90. // );
  91. }
  92. @override
  93. void dispose() {
  94. // Every listener should be canceled, the same should be done with this stream.
  95. _onUrlChanged.cancel();
  96. // _onStateChanged.cancel();
  97. // flutterWebViewPlugin.dispose();
  98. super.dispose();
  99. }
  100. }