skip to Main Content

I am making a calculator app, and when I click on an input field, I want to show a custom keyboard (basically a custom made widget that is the whole keyboard), but have no clue how to make it look like it is a real keyboard – opens from the bottom like a real one, looks like the real one, similarly functions like a normal one.

How do I make a widget appear and act as a keyboard in flutter?

2

Answers


  1. In Flutter, you can create a widget that mimics the behavior of a keyboard using a combination of Flutter’s built-in widgets and user input handling. To create a custom widget that behaves like a keyboard, you’ll typically use GestureDetector to capture user input and modify the state of your widget accordingly.

    The steps to create a simple custom keyboard widget in Flutter

    • Create a Custom Keyboard Widget Class: Define a custom widget class
      that represents your keyboard. This class will extend StatefulWidget.
      The state of the widget will hold the current keyboard input.

      Design Your Keyboard UI: Within the build method of your widget,
      design the UI of your keyboard using Flutter widgets like Container,
      Row, Column, and FlatButton. Each button on the keyboard can be a
      FlatButton or a GestureDetector that responds to user input.

      Handle User Input: Wrap each button or key with a GestureDetector to
      capture taps or gestures. In the gesture handler, update the keyboard
      input state (e.g., append characters to the input string).

      Display the Keyboard: Add your custom keyboard widget to the Flutter
      UI where you want the keyboard to appear. Typically, this is within a
      Stack or a Positioned widget to overlay it on top of your input
      fields.

      Interact with Input Fields: For input fields that should use your
      custom keyboard, you can set up controllers or handlers to insert
      text from the custom keyboard into those fields when keys are
      pressed.

    examples

    import 'package:flutter/material.dart';
    
    class CustomKeyboard extends StatefulWidget {
      @override
      _CustomKeyboardState createState() => _CustomKeyboardState();
    }
    
    class _CustomKeyboardState extends State<CustomKeyboard> {
      String inputText = '';
    
      void _handleKeyTap(String key) {
        setState(() {
          inputText += key;
        });
      }
    
      @override
      Widget build(BuildContext context) {
        return Column(
          children: [
            // Input field where you want to display the text
            TextField(
              controller: TextEditingController(text: inputText),
            ),
            // Custom keyboard UI
            Row(
              children: [
                GestureDetector(
                  onTap: () => _handleKeyTap('1'),
                  child: Container(
                    // Customize your key appearance here
                    child: Text('1'),
                  ),
                ),
                GestureDetector(
                  onTap: () => _handleKeyTap('2'),
                  child: Container(
                    // Customize your key appearance here
                    child: Text('2'),
                  ),
                ),
                // Add more keys as needed
              ],
            ),
          ],
        );
      }
    }
    
    void main() => runApp(MaterialApp(
          home: Scaffold(
            appBar: AppBar(
              title: Text('Custom Keyboard Example'),
            ),
            body: CustomKeyboard(),
          ),
        ));
    
    Login or Signup to reply.
  2. "GestureDetector(
      behavior: HitTestBehavior.translucent,
      onTap: () {
        // Your tap handling code here
      },
      child: YourChildWidget(),
    )"
    
    AbsorbPointer(
      absorbing: true, // Set to true to block gestures
      child: GestureDetector(
        onTap: () {
          // Your tap handling code here (will not trigger if absorbing is true)
        },
        child: YourChildWidget(),
      ),
    )
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search