颤振-模式集团

时间:2019-03-12 20:55:22

标签: dart flutter bloc

如果我在单个文本字段中设置keyboardType:TextInputType.number,如果我将其设置为TextInputType.text,我会在提交之前找到所有其他空值,是否在错误的地方找到正确的值?我希望有一个人可以帮助我。 这是我的代码: bloc.dart

    import 'dart:async';
import 'package:gdgbloc/bloc_provider.dart';

import 'validator.dart';
import 'package:rxdart/rxdart.dart';

class Bloc extends Object with Validators implements BlocBase  {
  final _emailController = BehaviorSubject<String>();
  final _passwordController = BehaviorSubject<String>();
  final  _aliquotaIvaController = BehaviorSubject<String>();

  Function(String) get emailChanged => _emailController.sink.add;
  Function(String) get passwordChanged => _passwordController.sink.add;
  Function(String) get changeAliquotaIva => _aliquotaIvaController.sink.add;



 Stream<String> get email => _emailController.stream.transform(emailValidator);
  Stream<String> get password =>
      _passwordController.stream.transform(passwordValidator);
Stream<double> get aliquotaIva =>
      _aliquotaIvaController.stream.transform(validateCampoVuotoDouble);
  Stream<bool> get submitCheck =>
      Observable.combineLatest2(email, password, (e, p) => true);

  submit() {
    print("xyx");
    final validCodice = _emailController.value;
    final validDescrizione = _passwordController.value;
    final validAlquota= _aliquotaIvaController.value;

    print('Codice is $validCodice');
    print('Descrizione is $validDescrizione');
    print('aliquota iva is $validAlquota');
  }

  @override
  void dispose() {
    _emailController?.close();
    _passwordController?.close();
    _aliquotaIvaController?.close();
  }
}

blocprovider.dart

import 'package:flutter/material.dart';

Type _typeOf<T>() => T;

abstract class BlocBase {
  void dispose();
}

class BlocProvider<T extends BlocBase> extends StatefulWidget {
  BlocProvider({
    Key key,
    @required this.child,
    @required this.bloc,
  }): super(key: key);

  final Widget child;
  final T bloc;

  @override
  _BlocProviderState<T> createState() => _BlocProviderState<T>();

  static T of<T extends BlocBase>(BuildContext context){
    final type = _typeOf<_BlocProviderInherited<T>>();
    _BlocProviderInherited<T> provider = 
            context.ancestorInheritedElementForWidgetOfExactType(type)?.widget;
    return provider?.bloc;
  }
}

class _BlocProviderState<T extends BlocBase> extends State<BlocProvider<T>>{
  @override
  void dispose(){
    widget.bloc?.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context){
    return new _BlocProviderInherited<T>(
      bloc: widget.bloc,
      child: widget.child,
    );
  }
}

class _BlocProviderInherited<T> extends InheritedWidget {
  _BlocProviderInherited({
    Key key,
    @required Widget child,
    @required this.bloc,
  }) : super(key: key, child: child);

  final T bloc;

  @override
  bool updateShouldNotify(_BlocProviderInherited oldWidget) => false;
}

main.dart

import 'package:flutter/material.dart';
import 'package:gdgbloc/bloc.dart';
import 'package:gdgbloc/bloc_provider.dart';
import 'package:gdgbloc/pagetwo.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home:HomePage(),
      theme: ThemeData(
        primarySwatch: Colors.teal,
      ),
      debugShowCheckedModeBanner: false,
    );
  }
}

class HomePage extends StatefulWidget {
  @override
  HomePageState createState() => new HomePageState();
}

class HomePageState extends State<HomePage> {


  @override
  Widget build(BuildContext context) {

    return Scaffold(
      appBar: AppBar(
        title: Text("Bloc Pattern"),
      ),
      body: SingleChildScrollView(
        child: Container(
          height: MediaQuery.of(context).size.height,
          padding: EdgeInsets.all(16)
      ),
    floatingActionButton: new FloatingActionButton(
        onPressed: () => {
              Navigator.push(context, MaterialPageRoute(builder: (context) {
                return BlocProvider<Bloc>(
                  bloc: Bloc(),
                  child: PageTwo(),
                );
              }))
            },
        mini: false,
        child: new Icon(Icons.add),
      ),
    );
  }
}

这是我代表三个texfields的页面。 pagetwo.dart

import 'package:flutter/material.dart';
import 'package:gdgbloc/bloc.dart';
import 'package:gdgbloc/bloc_provider.dart';

class PageTwo extends StatefulWidget {
  @override
  PageTwoState createState() => new PageTwoState();
}

class PageTwoState extends State<PageTwo> {
    final GlobalKey<ScaffoldState> _scaffoldKey =
        new GlobalKey<ScaffoldState>();

  @override
  Widget build(BuildContext context) {
    final bloc = BlocProvider.of<Bloc>(context);


    return Scaffold(
      key: _scaffoldKey,
      appBar: AppBar(
        title: Text("Bloc Pattern"),
      ),
      body: SingleChildScrollView(
        child: Container(
          height: MediaQuery.of(context).size.height,
          padding: EdgeInsets.all(16),
          child: Column(
            mainAxisSize: MainAxisSize.max,
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              StreamBuilder<String>(
                stream: bloc.email,
                builder: (context, snapshot) => TextField(
                      onChanged: bloc.emailChanged,
                      keyboardType: TextInputType.emailAddress,
                      decoration: InputDecoration(
                          border: OutlineInputBorder(),
                          hintText: "Enter email",
                          labelText: "Email",
                          errorText: snapshot.error),
                    ),
              ),
              SizedBox(
                height: 20.0,
              ),
              StreamBuilder<String>(
                stream: bloc.password,
                builder: (context, snapshot) => TextField(
                      onChanged: bloc.passwordChanged,
                      keyboardType: TextInputType.text,
                      decoration: InputDecoration(
                          border: OutlineInputBorder(),
                          hintText: "Enter password",
                          labelText: "Password",
                          errorText: snapshot.error),
                    ),
              ),
              SizedBox(
                height: 20.0,
              ),
              StreamBuilder<double>(
                stream: bloc.aliquotaIva,
                builder: (context, snapshot) => TextField(
                      onChanged: bloc.changeAliquotaIva,
                      keyboardType: TextInputType.text,
                      decoration: InputDecoration(
                          border: OutlineInputBorder(),
                          hintText: "Ealiquoita",
                          labelText: "Password",
                          errorText: snapshot.error),
                    ),
              ),
              SizedBox(
                height: 20.0,
              ),
              StreamBuilder<bool>(
                //stream: bloc.submitCheck,
                builder: (context, snapshot) => RaisedButton(
                      color: Colors.tealAccent,
                      onPressed: () => bloc.submit() ,
                      child: Text("Submit"),
                    ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

我仍处于起步阶段和集团模式的开始。除了无法获取文本字段值的问题之外,我还想知道是否正确使用了模式或要进行哪些更改。希望有所帮助

我还将链接附加到完整示例example complete

编辑: 我注意到,如果注释keyboardType有效,而我设置了注释,它将不再起作用=(

0 个答案:

没有答案