Skip to content

[General]: AutovalidateMode.always shouldn't focus to first field #1457

Closed
@DarenF-20C

Description

@DarenF-20C

Is there an existing issue for this?

  • I have searched the existing issues

Package/Plugin version

9.7.0

Platforms

  • Android
  • iOS
  • Linux
  • MacOS
  • Web
  • Windows

Flutter doctor

Flutter doctor
[✓] Flutter (Channel stable, 3.27.1, on macOS 14.6.1 23G93 darwin-arm64, locale
    en-MY)
[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
[✓] Xcode - develop for iOS and macOS (Xcode 16.2)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2024.1)
[✓] VS Code (version 1.96.2)
[✓] Connected device (4 available)
[✓] Network resources

• No issues found!

Minimal code example

Code sample
import 'package:flutter/material.dart';
import 'package:flutter_form_builder/flutter_form_builder.dart';
import 'package:form_builder_validators/form_builder_validators.dart';

void main() {
  runApp(const FlutterExample());
}

class FlutterExample extends StatelessWidget {
  const FlutterExample({super.key});

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

class HomePage extends StatelessWidget {
  const HomePage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Home Page'),
      ),
      body: Center(
        child: ElevatedButton(
          onPressed: () {
            Navigator.push(
              context,
              MaterialPageRoute(
                builder: (context) => const TheForm(),
              ),
            );
          },
          child: const Text('Go to Next Page'),
        ),
      ),
    );
  }
}

class TheForm extends StatefulWidget {
  const TheForm({super.key});

  @override
  State<TheForm> createState() => _TheFormState();
}

class _TheFormState extends State<TheForm> {
  final _formKey = GlobalKey<FormBuilderState>();
  bool _isValid = false;

  InputDecoration _defaultInputDecoration(String placeholder) =>
      InputDecoration(labelText: placeholder);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Form Page'),
      ),
      body: SafeArea(
        child: FormBuilder(
          key: _formKey,
          onChanged: () {
            setState(() {
              _isValid = _formKey.currentState!.isValid;
            });
          },
          child: Column(
            children: [
              FormBuilderTextField(
                name: 'name',
                decoration: _defaultInputDecoration('Name'),
                validator: FormBuilderValidators.required(),
                autovalidateMode: AutovalidateMode.always,
              ),
              FormBuilderTextField(
                name: 'email',
                decoration: _defaultInputDecoration('E-Mail'),
                validator: FormBuilderValidators.compose([
                  FormBuilderValidators.required(),
                  FormBuilderValidators.email(),
                ]),
                autovalidateMode: AutovalidateMode.always,
              ),
              Padding(
                padding: const EdgeInsets.all(8.0),
                child: Text(_isValid ? 'Form is valid' : 'Form is not valid'),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

Current Behavior

When set autovalidateMode: AutovalidateMode.always, the last field will be focus when enter the page which unexpected and logically weird.

Expected Behavior

When set autovalidateMode: AutovalidateMode.always, the first required field being focus when enter the page.
But the best, is allowing user to pass in whether to focus the field when it sets to required.

As the validate provides the control of focusOnInvalid which we can back to previous behaviour.

Steps To Reproduce

  1. run the code and able to see a home page with button
  2. press the button to next page.
  3. last field (email) being focus, but not the first.
  • Based on previous behaviour (upgraded from 7.8.0), it won't focus the field when entering page.

Aditional information

issue_formBuilder.mov

From the video, shows what is not expecting.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions