基础组件库
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

base_webview.dart 2.5 KiB

pirms 4 gadiem
pirms 4 gadiem
pirms 4 gadiem
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import 'package:flutter/cupertino.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:webview_flutter/webview_flutter.dart';
  4. class BaseWebview extends StatefulWidget {
  5. final Map<String, dynamic> model;
  6. const BaseWebview(this.model, {Key key}) : super(key: key);
  7. @override
  8. _BaseWebviewState createState() => _BaseWebviewState();
  9. }
  10. class _BaseWebviewState extends State<BaseWebview> {
  11. String _url;
  12. String _title;
  13. WebViewController _webViewController;
  14. @override
  15. void initState() {
  16. _url = widget.model['url'] ?? '';
  17. super.initState();
  18. }
  19. @override
  20. Widget build(BuildContext context) {
  21. return Scaffold(
  22. appBar: _createNav(),
  23. backgroundColor: Colors.white,
  24. body: WebView(
  25. initialUrl: _url,
  26. javascriptMode: JavascriptMode.unrestricted,
  27. onWebViewCreated: (WebViewController webViewController) {
  28. _webViewController = webViewController;
  29. },
  30. navigationDelegate: (NavigationRequest request) {
  31. return NavigationDecision.navigate;
  32. },
  33. onPageStarted: (String url) {
  34. print('Page started loading: $url');
  35. },
  36. onPageFinished: (String url) {
  37. print('Page finished loading: $url');
  38. _webViewController.getTitle().then((title) {
  39. _title = title;
  40. setState(() {});
  41. });
  42. },
  43. gestureNavigationEnabled: true,
  44. ),
  45. );
  46. }
  47. // 导航栏
  48. Widget _createNav() {
  49. return CupertinoNavigationBar(
  50. border: Border(
  51. bottom: BorderSide(
  52. width: 0.0, // One physical pixel.
  53. style: BorderStyle.none,
  54. ),
  55. ),
  56. backgroundColor: Colors.white,
  57. leading: Navigator.canPop(context)
  58. ? GestureDetector(
  59. child: Container(
  60. padding: EdgeInsets.zero,
  61. child: Icon(
  62. Icons.arrow_back_ios,
  63. size: 20,
  64. ),
  65. ),
  66. onTap: () {
  67. if (Navigator.canPop(context)) {
  68. Navigator.pop(context);
  69. }
  70. },
  71. )
  72. : Container(),
  73. middle: Text(
  74. _title ?? '',
  75. style: TextStyle(
  76. fontSize: 15,
  77. color: Color(0xff333333),
  78. ),
  79. ),
  80. trailing: GestureDetector(
  81. child: Icon(
  82. Icons.refresh,
  83. size: 20,
  84. ),
  85. onTap: () {
  86. _webViewController.reload();
  87. },
  88. ),
  89. );
  90. }
  91. }