flutter remove focus from textfield code example

Example 1: flutter unfocus textfield

FocusScope.of(context).unfocus();

Example 2: remove focus textfield flutter

// main.dart
import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        FocusScopeNode currentFocus = FocusScope.of(context);

        if (!currentFocus.hasPrimaryFocus) {
          currentFocus.unfocus();
        }
      },
      child: MaterialApp(
        title: 'Flutter Demo',
        theme: ThemeData(
          primarySwatch: Colors.blue,
        ),
        home: MyHomePage(),
      ),
    );
  }
}

Example 3: unfocus textfield flutter

FocusScope.of(context).unfocus();
_textEditingController.clear();

Tags:

Dart Example