wemersonrv

showSnackBar ModalScope locked error

Jul 31st, 2019
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 4.88 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. import 'package:mobile/widgets/input_field.dart';
  6.  
  7. import '../config.dart';
  8. import 'home_screen.dart';
  9.  
  10. class SetupScreen extends StatelessWidget {
  11.   @override
  12.   Widget build(BuildContext context) {
  13.     return Scaffold(
  14.       appBar: AppBar(
  15.         title: Text("$APP_TITLE - Ajustes"),
  16.         centerTitle: true,
  17.       ),
  18.       drawer: CustomDrawer(),
  19.       body: SetupForm(),
  20.     );
  21.   }
  22. }
  23.  
  24. class SetupForm extends StatefulWidget {
  25.   @override
  26.   _SetupFormState createState() => _SetupFormState();
  27. }
  28.  
  29. class _SetupFormState extends State<SetupForm> {
  30.   final _formKey = GlobalKey<FormState>();
  31.  
  32.   @override
  33.   Widget build(BuildContext context) {
  34.     final bloc = BlocProvider.of<SettingsBloc>(context);
  35.  
  36.     return SingleChildScrollView(
  37.       child: Container(
  38.         margin: EdgeInsets.all(32),
  39.         child: Form(
  40.           key: _formKey,
  41.           child: Column(
  42.             mainAxisAlignment: MainAxisAlignment.center,
  43.             children: <Widget>[
  44.               Container(
  45.                 width: 160,
  46.                 child: Image.asset("images/logo.png"),
  47.               ), // Logo
  48.               SizedBox(height: 32),
  49.               StreamBuilder<SettingsState>(
  50.                 stream: bloc.outState,
  51.                 builder: (context, snapshot) {
  52.                   if (snapshot.data == SettingsState.SAVED) {
  53.                     bloc.inState.add(SettingsState.DONE);
  54.                     WidgetsBinding.instance.addPostFrameCallback(
  55.                         (_) => _showDialog(context, bloc));
  56.                   }
  57.  
  58.                   if (snapshot.data != SettingsState.DONE) {
  59.                     return Container(
  60.                       alignment: Alignment.center,
  61.                       margin: EdgeInsets.only(top: 32),
  62.                       child: CircularProgressIndicator(),
  63.                     );
  64.                   }
  65.  
  66.                   return Column(
  67.                     children: <Widget>[
  68.                       InputField(
  69.                         icon: Icons.local_gas_station,
  70.                         hint: "IP do Concentrador",
  71.                         obscure: false,
  72.                         keyboardType: TextInputType.number,
  73.                         inputFormat: CustomFormatters.FORMAT_IP_ADDRESS,
  74.                         stream: bloc.outEditIP,
  75.                         onChanged: bloc.changeIP,
  76.                       ),
  77.                       InputField(
  78.                         icon: Icons.exit_to_app,
  79.                         hint: "Porta de conexão(771, 1771, 857)",
  80.                         obscure: false,
  81.                         keyboardType: TextInputType.number,
  82.                         inputFormat: CustomFormatters.FORMAT_IP_PORT,
  83.                         stream: bloc.outEditPort,
  84.                         onChanged: bloc.changePort,
  85.                       ),
  86.                       SizedBox(height: 48),
  87.                       StreamBuilder<bool>(
  88.                         stream: bloc.outValidated,
  89.                         builder: (context, snapshot) {
  90.                           return SizedBox(
  91.                             height: 44,
  92.                             child: RaisedButton.icon(
  93.                               disabledColor: Colors.green.withAlpha(140),
  94.                               onPressed: snapshot.hasData
  95.                                   ? () {
  96.                                       bloc.store();
  97.                                     }
  98.                                   : null,
  99.                               icon: Icon(Icons.save, color: Colors.white),
  100.                               label: Text(
  101.                                 "Gravar",
  102.                                 style: TextStyle(
  103.                                   fontSize: 18.0,
  104.                                   color: Colors.white,
  105.                                 ),
  106.                               ),
  107.                               color: Colors.green,
  108.                             ),
  109.                           );
  110.                         },
  111.                       ),
  112.                     ],
  113.                   );
  114.                 },
  115.               ),
  116.             ],
  117.           ),
  118.         ),
  119.       ),
  120.     );
  121.   }
  122.  
  123.   void _showDialog(BuildContext context, SettingsBloc bloc) {
  124.     Scaffold.of(context).showSnackBar(SnackBar(
  125.       content: Text('Dados de conexão gravados com sucesso!'),
  126.       duration: Duration(days: 1),
  127.       backgroundColor: Colors.black87,
  128.       action: SnackBarAction(
  129.         textColor: Colors.yellow,
  130.         label: 'Ok',
  131.         onPressed: () async {
  132.           bloc.inPage.add(0);
  133.           await Navigator.of(context).push(
  134.             MaterialPageRoute(builder: (context) => HomeScreen()),
  135.           );
  136.         },
  137.       ),
  138.     ));
  139.   }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment