SHARE
TWEET

Untitled

a guest Apr 18th, 2019 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import 'dart:io';
  2. import 'dart:async';
  3.  
  4. import 'package:flutter/material.dart';
  5. import 'package:flutter_user_agent/flutter_user_agent.dart';
  6. import 'package:webview_flutter/webview_flutter.dart';
  7. import 'package:http/http.dart' as http;
  8. //import 'package:share/share.dart';
  9.  
  10. void main() => runApp(MaterialApp(home: ForumsScreen()));
  11.  
  12. class ForumsScreen extends StatefulWidget {
  13.   @override
  14.   State<StatefulWidget> createState() => ForumsScreenState();
  15. }
  16.  
  17. class ForumsScreenState extends State<ForumsScreen> {
  18.   final _bloc = ForumsBloc();
  19.   Completer<WebViewController> _controller = Completer<WebViewController>();
  20.  
  21.   // todo: try passing data using an inherited widget-
  22.   @override
  23.   Widget build(BuildContext context) {
  24.     return StreamBuilder<bool>(
  25.         stream: _bloc.loadingStream,
  26.         initialData: true,
  27.         builder: (context, snapshot) {
  28.           return Scaffold(
  29.             appBar: AppBar(
  30.               title: Text("Forums"),
  31.               actions: <Widget>[
  32.                 RefreshButton(snapshot.data),
  33.                 SharePostButton(_controller, snapshot.data),
  34.               ],
  35.             ),
  36.             body: ForumsWebView(_bloc),
  37.           );
  38.         });
  39.   }
  40. }
  41.  
  42. class ForumsWebView extends StatefulWidget {
  43.   final ForumsBloc _bloc;
  44.   ForumsWebView(this._bloc);
  45.  
  46.   @override
  47.   _ForumsWebViewState createState() => _ForumsWebViewState();
  48. }
  49.  
  50. class _ForumsWebViewState extends State<ForumsWebView> {
  51.   Completer<WebViewController> _controller = Completer<WebViewController>();
  52.   String _webViewUserAgent;
  53.  
  54.   // todo: potentially remove
  55.   @override
  56.   void initState() {
  57.     super.initState();
  58.     getDefaultUserAgent();
  59.   }
  60.  
  61.   @override
  62.   Widget build(BuildContext context) {
  63.     return WebView(
  64.       initialUrl: 'http://forums.redflagdeals.com',
  65.       onWebViewCreated: (WebViewController webViewController) {
  66.         _controller.complete(webViewController);
  67.       },
  68.       onPageFinished: (String string) {
  69.         widget._bloc.forumsEventSink.add(LoadingEvent());
  70.         final snackBar = SnackBar(
  71.           content: Text(_getNewUserAgent(_webViewUserAgent)),
  72.           duration: Duration(seconds: 2),
  73.         );
  74.         Scaffold.of(context).showSnackBar(snackBar);
  75.       },
  76.       navigationDelegate: (NavigationRequest request) {
  77.         return NavigationDecision.navigate;
  78.       },
  79.     );
  80.   }
  81.  
  82.   Future<void> getDefaultUserAgent() async {
  83.     String webViewUserAgent;
  84.     try {
  85.       await FlutterUserAgent.init();
  86.       webViewUserAgent = FlutterUserAgent.webViewUserAgent;
  87.     } catch (Exception) {
  88.       webViewUserAgent = '<error>';
  89.     }
  90.  
  91.     if (!mounted) return;
  92.  
  93.     setState(() {
  94.       _webViewUserAgent = webViewUserAgent;
  95.     });
  96.   }
  97.  
  98.   // todo user agent into variables
  99.   // todo format by locale
  100.   String _getNewUserAgent(String oldUserAgent) {
  101.     return oldUserAgent.replaceFirst(
  102.         RegExp(r'^[^(]*'), "RedFlagDealsApp/3.13.9 ");
  103.   }
  104. }
  105.  
  106. class RefreshButton extends StatefulWidget {
  107.   final bool _loading;
  108.   RefreshButton(this._loading);
  109.  
  110.   @override
  111.   State<StatefulWidget> createState() => RefreshButtonState();
  112. }
  113.  
  114. class RefreshButtonState extends State<RefreshButton> {
  115.   @override
  116.   Widget build(BuildContext context) {
  117.     return IconButton(
  118.       icon: Icon(
  119.         Icons.refresh,
  120.         color: widget._loading ? Theme.of(context).disabledColor : Colors.white,
  121.       ),
  122.       onPressed: () {
  123.         _refreshPost();
  124.       },
  125.     );
  126.   }
  127.  
  128.   _refreshPost() {
  129.     // TODO
  130.   }
  131. }
  132.  
  133. class SharePostButton extends StatefulWidget {
  134.   final Completer<WebViewController> controller;
  135.   final bool _loading;
  136.   SharePostButton(this.controller, this._loading);
  137.  
  138.   @override
  139.   State<StatefulWidget> createState() => SharePostButtonState();
  140. }
  141.  
  142. class SharePostButtonState extends State<SharePostButton> {
  143.   @override
  144.   Widget build(BuildContext context) {
  145.     return IconButton(
  146.         icon: Icon(Icons.share,
  147.             color: widget._loading
  148.                 ? Theme
  149.                 .of(context)
  150.                 .disabledColor
  151.                 : Colors.white),
  152.         onPressed: () {
  153.           _sharePost();
  154.         });
  155.   }
  156.  
  157.   void _sharePost() {}
  158.  
  159. }
  160.  
  161.  
  162. //BLOC Pattern components, move out eventually
  163. abstract class ForumsEvent {}
  164.  
  165. class LoadingEvent extends ForumsEvent {}
  166.  
  167. class ForumsBloc {
  168.   // too many loading variables??
  169.   bool _loading = true;
  170.  
  171.   final _loadingStateController = StreamController<bool>();
  172.   StreamSink<bool> get _loadingSink => _loadingStateController.sink;
  173.   Stream<bool> get loadingStream => _loadingStateController.stream;
  174.  
  175.   // input events
  176.   final _forumsEventController = StreamController<ForumsEvent>();
  177.   Sink<ForumsEvent> get forumsEventSink => _forumsEventController.sink;
  178.  
  179.   ForumsBloc() {
  180.     _forumsEventController.stream.listen(_mapEventToState);
  181.   }
  182.  
  183.   void _mapEventToState(ForumsEvent event) {
  184.     if (event is LoadingEvent) {
  185.       _mapLoadEventToState(event);
  186.     }
  187.   }
  188.  
  189.   void _mapLoadEventToState(LoadingEvent event) {
  190.     _loading = !_loading;
  191.     _loadingSink.add(_loading);
  192.   }
  193.  
  194.   void dispose() {
  195.     _loadingStateController.close();
  196.     _forumsEventController.close();
  197.   }
  198. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top