更改StreamController后按钮保持禁用状态 .broadcast()到BehaviorSubject ()

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了更改StreamController后按钮保持禁用状态 .broadcast()到BehaviorSubject ()相关的知识,希望对你有一定的参考价值。

我使用Streams和rxdart进行表单验证。当我使用StreamController<String>.broadcast()时,按钮启用 - 禁用逻辑工作正常。

即使逻辑正确,将其更改为BehaviorSubject<String>()按钮也会保持禁用状态。

bloc.dart

import 'dart:async';

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

class Bloc with Validator 
  final _emailController = StreamController<String>.broadcast();
  final _passwordController = StreamController<String>.broadcast();

  Stream<String> get email => _emailController.stream.transform(validateEmail);

  Stream<String> get password =>
      _passwordController.stream.transform(validatePassword);

  Stream<bool> get validForm =>
      Observable.combineLatest2(email, password, (e, p) => true);

  Function(String) get changeEmail => _emailController.sink.add;

  Function(String) get changePassword => _passwordController.sink.add;

  dispose() 
    _emailController.close();
    _passwordController.close();
  


final bloc = Bloc();

home.dart, Two TextFields and Submit button

Widget emailTextField(context, Bloc bloc) => StreamBuilder(
        stream: bloc.email,
        builder: (context, snapshot) => TextField(
              decoration: InputDecoration(
                  labelText: "Email",
                  hintText: "Enter your email address",
                  errorText: snapshot.error,
                  border: UnderlineInputBorder()),
              keyboardType: TextInputType.emailAddress,
              onChanged: bloc.changeEmail,
            ),
      );

  Widget passwordTextField(context, Bloc bloc) => StreamBuilder(
        stream: bloc.password,
        builder: (context, snapshot) => TextField(
              decoration: InputDecoration(
                  labelText: "Password",
                  hintText: "Enter your password",
                  errorText: snapshot.error,
                  border: UnderlineInputBorder()),
              keyboardType: TextInputType.text,
              obscureText: true,
              onChanged: bloc.changePassword,
            ),
      );

  Widget submitButton(context, Bloc bloc) => StreamBuilder(
        stream: bloc.validForm,
        builder: (context, snapshot) 
          return Container(
            child: RaisedButton(
              color: Colors.blue,
              disabledColor: Colors.grey,
              textColor: Colors.white,
              disabledTextColor: Colors.white70,
              onPressed: snapshot.hasData && !snapshot.hasError
                  ? () 
                      print("Submit button pressed");
                    
                  : null,
              elevation: 5,
              disabledElevation: 0,
              child: Text("Submit"),
            ),
          );
        ,
      );

Changes

在bloc.dart中

final _emailController = BehaviorSubject<String>();
final _passwordController = BehaviorSubject<String>();

submit() 
    final validEmail = _emailController.value;
    final validPassword = _passwordController.value;

    print("\n\tEmail: $validEmail\n\tPassword: $validPassword");

在home.dart

snapshot.hasData && !snapshot.hasError ? bloc.submit() : null,

我希望按钮在逻辑正确时保持启用状态,并且除非按下按钮,否则不应调用该方法

答案

在home.dart:

改变这个:

snapshot.hasData && !snapshot.hasError ? bloc.submit() : null,

对此

snapshot.hasData && !snapshot.hasError ? bloc.submit : null,

说明:使用bloc.submit,您可以引用方法,使用bloc.submit(),您可以引用该方法的返回值。

你所做的是你指的是该方法的返回值,因为你没有返回任何方法只是执行但是当返回的值为null时,按钮仍然被禁用。

以上是关于更改StreamController后按钮保持禁用状态 .broadcast()到BehaviorSubject ()的主要内容,如果未能解决你的问题,请参考以下文章

Knockout + MVC 禁用 html 按钮并在 ajax 调用后更改颜色

使按钮的背景颜色在单击后更改并保持不变

在 JQuery 中单击后禁用按钮

在Swift中更改ViewController后禁用任务栏?

如何通过单击按钮禁用 JQuery 功能,但默认保持打开状态?

需要 Javascript 代码帮助 - 单击四个按钮中的一个后如何禁用按钮(5 个按钮)