Advertisement
wemersonrv

SettingsBloc

Jul 8th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 4.21 KB | None | 0 0
  1. import 'package:bloc_pattern/bloc_pattern.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:mobile/blocs/settings-bloc.dart';
  4. import 'package:mobile/widgets/custom-drawer.dart';
  5.  
  6. import '../config.dart';
  7.  
  8. class HomeScreen extends StatelessWidget {
  9.   @override
  10.   Widget build(BuildContext context) {
  11.     return Scaffold(
  12.       appBar: AppBar(
  13.         title: Text("MaisSync"),
  14.         centerTitle: true,
  15.       ),
  16.       drawer: CustomDrawer(),
  17.       body: DetailsWidget(),
  18.     );
  19.   }
  20. }
  21.  
  22. class DetailsWidget extends StatelessWidget {
  23.   @override
  24.   Widget build(BuildContext context) {
  25.     final bloc = BlocProvider.of<SettingsBloc>(context);
  26.     return SingleChildScrollView(
  27.       child: Container(
  28.         margin: EdgeInsets.all(32),
  29.         child: Column(
  30.           children: <Widget>[
  31.             Container(
  32.               width: 160,
  33.               child: Image.asset("images/logo.png"),
  34.             ), // Logo
  35.             SizedBox(height: 48),
  36.             Card(
  37.               child: Container(
  38.                 margin: EdgeInsets.all(8),
  39.                 child: Column(
  40.                   children: <Widget>[
  41.                     Text(
  42.                       "Dados de conexão",
  43.                       style: TextStyle(
  44.                         fontSize: 18,
  45.                         fontWeight: FontWeight.bold,
  46.                       ),
  47.                     ),
  48.                     SizedBox(height: 32),
  49.                     StreamBuilder<String>(
  50.                       stream: bloc.outIP,
  51.                       builder: (context, snapshot) {
  52.                         return ("IP: ${snapshot.data}");
  53.  
  54.                         return Row(
  55.                           mainAxisAlignment: MainAxisAlignment.spaceBetween,
  56.                           children: <Widget>[
  57.                             Text(
  58.                               "IP do Concentrador",
  59.                               style: TextStyle(fontSize: 16),
  60.                             ),
  61.                             Text(
  62.                               snapshot.data,
  63.                               style: TextStyle(fontSize: 16),
  64.                             ),
  65.                           ],
  66.                         );
  67.                       },
  68.                     ),
  69.                     SizedBox(height: 16),
  70.                     StreamBuilder<int>(
  71.                       stream: bloc.outPort,
  72.                       builder: (context, snapshot) {
  73.                         return ("Porta: ${snapshot.data}");
  74.  
  75.                         return Row(
  76.                           mainAxisAlignment: MainAxisAlignment.spaceBetween,
  77.                           children: <Widget>[
  78.                             Text(
  79.                               "Porta de conexão",
  80.                               style: TextStyle(fontSize: 16),
  81.                             ),
  82.                             Text(
  83.                               "${snapshot.data}",
  84.                               style: TextStyle(fontSize: 16),
  85.                             ),
  86.                           ],
  87.                         );
  88.                       },
  89.                     ),
  90.                   ],
  91.                 ),
  92.               ),
  93.             ),
  94.           ],
  95.         ),
  96.       ),
  97.     );
  98.   }
  99. }
  100.  
  101.  
  102. import 'dart:async';
  103.  
  104. import 'package:bloc_pattern/bloc_pattern.dart';
  105. import 'package:rxdart/rxdart.dart';
  106. import 'package:shared_preferences/shared_preferences.dart';
  107.  
  108. class SettingsBloc extends BlocBase {
  109.   // Fluxo do IP
  110.   var _ipController = BehaviorSubject<String>(seedValue: "---");
  111.   Stream<String> get outIP => _ipController.stream;
  112.   Sink<String> get inIP => _ipController.sink;
  113.  
  114.   // Fluxo da Porta
  115.   var _portController = BehaviorSubject<int>(seedValue: 0);
  116.   Stream<int> get outPort => _portController.stream;
  117.   Sink<int> get inPort => _portController.sink;
  118.  
  119.   SettingsBloc() {
  120.     SharedPreferences.getInstance().then((prefs) {
  121.       String ip =
  122.           prefs.getKeys().contains("ip") ? prefs.getString("ip") : "---";
  123.       int port = prefs.getKeys().contains("port") ? prefs.getInt("port") : 0;
  124.  
  125.       _ipController.add(ip);
  126.       _portController.add(port);
  127.     });
  128.   }
  129.  
  130.   @override
  131.   void dispose() {
  132.     _ipController.close();
  133.     _portController.close();
  134.   }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement