flutter textfield unfocus code example

Example 1: flutter unfocus textfield

FocusScopeNode currentFocus = FocusScope.of(context);

if (!currentFocus.hasPrimaryFocus) {
currentFocus.unfocus();
}

Example 2: flutter unfocus textfield

FocusScope.of(context).unfocus();

Example 3: remove focus textfield flutter

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

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

class MyApp extends StatelessWidget {
  @override
  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 4: unfocus textfield flutter

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

Tags:

Java Example