keyboard_detection 0.4.2+3 copy "keyboard_detection: ^0.4.2+3" to clipboard
keyboard_detection: ^0.4.2+3 copied to clipboard

outdated

This plugin gives you an easy way to detect if the keyboard is visible or not using bottom view insets.

example/lib/main.dart

// ignore_for_file: avoid_print

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

void main() {
  runApp(const MaterialApp(home: MyApp()));
}

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

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  bool isKeyboardVisible = false;

  late KeyboardDetectionController keyboardDetectionController;

  @override
  void initState() {
    keyboardDetectionController = KeyboardDetectionController(
      onChanged: (value) {
        print('Keyboard visibility onChanged: $value');
        setState(() {
          isKeyboardVisible = value;
        });
      },
      minDifferentSize: 100,
    );

    keyboardDetectionController.asStream.listen((isVisible) {
      print('Listen from stream: $isVisible');
    });

    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return KeyboardDetection(
      controller: keyboardDetectionController,
      child: Scaffold(
        appBar: AppBar(
          title: const Text('Keyboard Detection'),
        ),
        body: Center(
          child: Column(
            children: [
              Text('Is keyboard visible: $isKeyboardVisible'),
              Text(
                'Get current state: ${keyboardDetectionController.currentState}',
              ),
              Text(
                'Get current KeyboardState: ${keyboardDetectionController.keyboardState}',
              ),
              Text(
                'Is keyboard size loaded: ${keyboardDetectionController.isKeyboardSizeLoaded}',
              ),
              Text(
                'Get keyboard size: ${keyboardDetectionController.keyboardSize}',
              ),
              const TextField(),
              ElevatedButton(
                onPressed: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(
                      builder: (_) => const AnotherPage(),
                    ),
                  );
                },
                child: const Text('Navigate to another page'),
              ),
              ElevatedButton(
                onPressed: () {
                  Navigator.pushAndRemoveUntil(
                      context,
                      MaterialPageRoute(
                        builder: (_) => const AnotherPage(),
                      ),
                      (_) => false);
                },
                child: const Text('Move to another page'),
              )
            ],
          ),
        ),
      ),
    );
  }
}

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

  @override
  Widget build(BuildContext context) {
    return KeyboardDetection(
        controller: KeyboardDetectionController(
          onChanged: (value) {
            print('Keyboard visibility onChanged: $value');
          },
          minDifferentSize: 100,
        ),
        child: Scaffold(
          appBar: AppBar(),
          body: Center(
            child: Column(
              children: const [
                TextField(),
              ],
            ),
          ),
        ));
  }
}
22
likes
0
points
2.79k
downloads

Publisher

verified publisherlamnhan.dev

Weekly Downloads

This plugin gives you an easy way to detect if the keyboard is visible or not using bottom view insets.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

flutter

More

Packages that depend on keyboard_detection