Skip to content

TextFormField reset works only for the first time #104975

@ghost

Description

Steps to Reproduce

  1. Execute flutter run on the code sample or on dart pad
  2. Enter some text inside textInput (like 123)
  3. Press submit
  4. Text is well reset
  5. Enter again some text inside textInput (like 456)
  6. Press submit again
  7. Text is not well reset
FocusScope.of(context).requestFocus(FocusNode());  // TextFormField initialValue seems register controller.value to default and broke the next reset.
_formKey.currentState?.reset();

Expected results:
TextInput should reset
Actual results:
TextInput is not reset

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

const Color darkBlue = Color.fromARGB(255, 18, 32, 47);

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData.dark().copyWith(
        scaffoldBackgroundColor: darkBlue,
      ),
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: Center(
          child: MyWidget(),
        ),
      ),
    );
  }
}

class MyWidget extends StatefulWidget {
  @override
  State<MyWidget> createState() => _MyWidgetState();
}

class _MyWidgetState extends State<MyWidget> {
  final GlobalKey<FormState> _formKey = GlobalKey<FormState>();

  final TextEditingController controller = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Form(
    
    key: _formKey,
        autovalidateMode: AutovalidateMode.onUserInteraction,
      child: Column(
      children: [
        TextFormField(
          controller: controller,
        ),
        ElevatedButton(
          onPressed: () {
            
            FocusScope.of(context).requestFocus(FocusNode()); 
            _formKey.currentState?.reset();
          },
        child: const Text("submit"),
        ), 
      ],)
    );
  }
}
Logs

Issue reproductible on dart pad.
Capture d’écran 2022-05-30 à 17 22 10

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Important issues not at the top of the work lista: text inputEntering text in a text field or keyboard related problemsf: material designflutter/packages/flutter/material repository.found in release: 3.0Found to occur in 3.0found in release: 3.1Found to occur in 3.1frameworkflutter/packages/flutter repository. See also f: labels.has reproducible stepsThe issue has been confirmed reproducible and is ready to work onteam-designOwned by Design Languages teamtriaged-designTriaged by Design Languages team

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions