GIfatahTH / states_rebuilder

a simple yet powerful state management technique for Flutter
494 stars 56 forks source link

buggy isEnabledRM and isReadonlyRM in OnFormBuilder #265

Closed benny856694 closed 2 years ago

benny856694 commented 2 years ago

See the attached code.

steps:

  1. check ReadOnly, fields are not read only, still can be edited
  2. uncheck Enabled, fields are disabled
  3. check Enabled again, fields remain disabled
import 'package:flutter/material.dart';
import 'package:states_rebuilder/states_rebuilder.dart';

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

class Validators {
  static String? emailValidator(String? email) {
    if (email != null && !email.contains('@')) {
      return 'A valid email must contains @';
    }
    return null;
  }

  static String? passwordValidator(String? password) {
    if (password != null && password.length < 6) {
      return 'A valid password must contains as least 6 characters';
    }
    return null;
  }
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

final emailRM = RM.injectTextEditing(
  validators: [
    //Validators.emailValidator,
  ],
);

final passwordRM = RM.injectTextEditing(
  validators: [
    //Validators.passwordValidator,
  ],
);

final enabledRM = true.inj();
final readonlyRM = false.inj();

final form = RM.injectForm(
  // By default form validation is done after field losing focus
  // autovalidateMode: AutovalidateMode.onUserInteraction,
  submit: () async {
    await Future.delayed(const Duration(seconds: 10));
  },
  // submissionSideEffects: SideEffects.onOrElse(
  //   onWaiting: () => enabledRM.state = false,
  //   orElse: (_) => enabledRM.state = true,
  // ),
);

class MyHomePage extends StatefulWidget {
  MyHomePage({Key? key}) : super(key: key);

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    //emailRM.controller.text = 'cool';
    return Scaffold(
      appBar: AppBar(
        title: const Text('Login form'),
      ),
      body: Padding(
        padding: const EdgeInsets.all(8.0),
        child: OnFormBuilder(
            isEnabledRM: enabledRM,
            isReadOnlyRM: readonlyRM,
            listenTo: form,
            builder: () {
              return ListView(
                children: <Widget>[
                  TextField(
                    enabled: emailRM.isEnabled, // .state,
                    readOnly: emailRM.isReadOnly, // readonlyRM.state,
                    controller: emailRM.controller,
                    focusNode: emailRM.focusNode,
                    keyboardType: TextInputType.emailAddress,
                    decoration: InputDecoration(
                      hintText: "your@email.com",
                      labelText: "Email Address",
                      errorText: emailRM.error,
                    ),
                  ),
                  TextField(
                    enabled: passwordRM.isEnabled, // .state,
                    readOnly: passwordRM.isReadOnly, // readonlyRM.state,
                    controller: passwordRM.controller,
                    focusNode: passwordRM.focusNode,
                    decoration: InputDecoration(
                      hintText: "Password",
                      labelText: 'Password',
                      errorText: passwordRM.error,
                    ),
                  ),
                  const SizedBox(height: 8),
                  Row(
                    children: [
                      OnFormSubmissionBuilder(
                        listenTo: form,
                        onSubmitting: () => const CircularProgressIndicator(),
                        child: ElevatedButton(
                          child: const Text('Submit'),
                          onPressed: form.submit,
                        ),
                      ),
                      const SizedBox(width: 8),
                      ElevatedButton(
                        child: const Text('Reset'),
                        onPressed: () {
                          form.reset();
                        },
                      ),
                      const SizedBox(width: 8),
                      ElevatedButton(
                        child: const Text('New Form'),
                        onPressed: () {
                          Navigator.of(context).push(
                            MaterialPageRoute(
                              builder: (context) => MyHomePage(),
                            ),
                          );
                        },
                      ),
                      Expanded(
                        child: CheckboxListTile(
                          title: const Text('Readonly'),
                          value: readonlyRM.state,
                          onChanged: (_) {
                            //enabledRM.state = false;
                            readonlyRM.toggle();
                            print(readonlyRM.state);
                          },
                        ),
                      ),
                      Expanded(
                        child: CheckboxListTile(
                          title: const Text('Enabled'),
                          value: enabledRM.state,
                          onChanged: (_) {
                            //enabledRM.state = false;
                            enabledRM.toggle();
                            print(enabledRM.state);
                          },
                        ),
                      ),
                    ],
                  ),
                  if (form.isDirty)
                    const Text('The form is changed but not submitted yet!')
                ],
              );
            }),
      ),
    );
  }
}
GIfatahTH commented 2 years ago

Check version 6.1.0 the bug is fixed

benny856694 commented 2 years ago

thanks. it works.