DenisovAV / flutter_tv

232 stars 40 forks source link

Keyboard For TextFields #34

Open jtkeyva opened 1 month ago

jtkeyva commented 1 month ago

Hello, this is raelly great what you have done. I cannot figure out how to invoke the native keyboard (the strip of letters). I tried some swift stuff but no luck. Any help greatly appreciated.

import Flutter

@main
@objc class AppDelegate: FlutterAppDelegate {
  var textField: UITextField?

  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
    GeneratedPluginRegistrant.register(with: self)

    // Setup the method channel to communicate with Flutter
    let controller: FlutterViewController = window?.rootViewController as! FlutterViewController
    let channel = FlutterMethodChannel(name: "flutter_tv/input", binaryMessenger: controller.binaryMessenger)

    channel.setMethodCallHandler { [weak self] (call: FlutterMethodCall, result: @escaping FlutterResult) -> Void in
      if call.method == "showNativeKeyboard" {
        self?.showNativeTextField()
        result(nil)
      } else {
        result(FlutterMethodNotImplemented)
      }
    }

    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }

  func showNativeTextField() {
    // Create and configure UITextField
    let uiTextField = UITextField(frame: CGRect(x: 50, y: 100, width: 300, height: 50))
    uiTextField.backgroundColor = .white
    uiTextField.placeholder = "Enter your review"
    uiTextField.borderStyle = .roundedRect
    uiTextField.becomeFirstResponder() // Show the keyboard

    // Add it to the root view
    if let rootView = window?.rootViewController?.view {
      rootView.addSubview(uiTextField)
    }

    // Store a reference to the text field so it can be dismissed later if needed
    textField = uiTextField
  }
}

class TvKeyboardHelper {
  static const platform = MethodChannel('flutter_tv/input');

  static Future<void> showNativeKeyboard() async {
    try {
      await platform.invokeMethod('showNativeKeyboard');
    } on PlatformException catch (e) {
      print("Failed to show native keyboard: ${e.message}");
    }
  }
}

  child: TextField(
    onTap: () {
      TvKeyboardHelper.showNativeKeyboard();  // Trigger the native tvOS keyboard
    },
    decoration: InputDecoration(
      labelText: 'Enter your review',
    ),
  ),```
DenisovAV commented 1 month ago

The good point, I need to add keyboard case to the example, will do it soon

jtkeyva commented 1 month ago

awesome i look forward to it! this really is great stuff you are doing, thanks

kzietek commented 1 day ago

I also look forward to it meanwhile I'll invent some kind of workaround. For the completness of this thread I want to report what I'm exactly doing on the Flutter side.

The widget looks like this:

TextField(focusNode: widget.textFieldFocus, ...)

I'm pressing the select button on AppleTV. It's correctly recognized and pushed as an event. The event gets picked by FocusableActionDetector. Inside the detector, i'm calling:

widget.textFieldFocus.requestFocus();

The keyboard does not show and XCode prints the error:

UITextInputSessionActionAnalytics is nil