DenisovAV / flutter_tv

219 stars 36 forks source link

Keyboard For TextFields #34

Open jtkeyva opened 5 days ago

jtkeyva commented 5 days 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 4 days ago

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

jtkeyva commented 4 days ago

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