有没有办法将自定义工具栏放在键盘上?

Is there any way to put custom toolbar on the keypad?

我想在键盘上放置一个自定义工具栏,如上图所示。颤振有可能吗?或者我应该在 iOS 或 Android 端编写代码?

是的,在 flutter 中有一种方法可以实现这一点。

  1. 创建要添加的工具栏的小部件。
  2. 将其设置为在输入焦点上可见。

为了参考,我正在分享我是如何实现的代码。

class InputDoneView extends StatelessWidget {


@override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      color: Style.lightGrey,
      child: Align(
        alignment: Alignment.topRight,
        child: Padding(
          padding: const EdgeInsets.only(top: 1.0, bottom: 1.0),
          child: CupertinoButton(
            padding: EdgeInsets.only(right: 24.0, top: 2.0, bottom: 2.0),
            onPressed: () {
              FocusScope.of(context).requestFocus(new FocusNode());
            },
            child: Text(
              "Done",
              style: TextStyle(color: Style.primaryColor,fontWeight: FontWeight.normal)
            ),
          ),
        ),
      ),
    );
  }
}

当输入字段聚焦和聚焦时在主视图中调用它。

showOverlay(BuildContext context) {
    if (overlayEntry != null) return;
    OverlayState overlayState = Overlay.of(context);
    overlayEntry = OverlayEntry(builder: (context) {
      return Positioned(
          bottom: MediaQuery.of(context).viewInsets.bottom, right: 0.0, left: 0.0, child: InputDoneView());
    });

    overlayState.insert(overlayEntry);
  }

  removeOverlay() {
    if (overlayEntry != null) {
      overlayEntry.remove();
      overlayEntry = null;
    }
  }

您可以复制粘贴 运行 下面的完整代码
请参阅下面的工作演示
您可以使用包 https://pub.dev/packages/keyboard_overlay
第 1 步:使用 with HandleFocusNodesOverlayMixin
第 2 步:将 FocusNodeOverlay 用于 focusNode
第 3 步:使用 GetFocusNodeOverlay 并设置 _focusNodeOverlay = GetFocusNodeOverlay(
第 4 步:TextField 使用 TextField(focusNode: _focusNodeOverlay,
代码片段

class _MyHomePageState extends State<MyHomePage>
    with HandleFocusNodesOverlayMixin {
  FocusNodeOverlay _focusNodeOverlay;

  @override
  void initState() {
    _focusNodeOverlay = GetFocusNodeOverlay(
      child: TopKeyboardUtil(
        Container(
          color: Colors.white,
          height: 45,
          width: MediaQueryData.fromWindow(ui.window).size.width,
          child: Row(
            children: [
              GestureDetector(
                child: Icon(Icons.save),
                onTap: () => print("click"),
              ),
             ...
              Spacer(),
              Container(
                width: 60,
                child: Center(
                  child: DoneButtonIos(
                    backgroundColor: Colors.white,
                    textColor: Colors.green,
                    label: 'Post',
                    onSubmitted: () {
                      print("submit");
                    },
                    platforms: ['android', 'ios'],
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );

工作演示

完整代码

import 'package:flutter/material.dart';
import 'package:keyboard_overlay/keyboard_overlay.dart';
import 'dart:ui' as ui;

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage>
    with HandleFocusNodesOverlayMixin {
  FocusNodeOverlay _focusNodeOverlay;

  @override
  void initState() {
    _focusNodeOverlay = GetFocusNodeOverlay(
      child: TopKeyboardUtil(
        Container(
          color: Colors.white,
          height: 45,
          width: MediaQueryData.fromWindow(ui.window).size.width,
          child: Row(
            children: [
              GestureDetector(
                child: Icon(Icons.save),
                onTap: () => print("click"),
              ),
              GestureDetector(
                child: Icon(Icons.computer),
                onTap: () => print("click"),
              ),
              GestureDetector(
                child: Icon(Icons.home),
                onTap: () => print("click"),
              ),
              Spacer(),
              Container(
                width: 60,
                child: Center(
                  child: DoneButtonIos(
                    backgroundColor: Colors.white,
                    textColor: Colors.green,
                    label: 'Post',
                    onSubmitted: () {
                      print("submit");
                    },
                    platforms: ['android', 'ios'],
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );

    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            TextField(
              focusNode: _focusNodeOverlay,
              style: TextStyle(color: Colors.grey),
              decoration: InputDecoration(
                labelText: 'Type Something',
                labelStyle: TextStyle(color: Colors.black),
                fillColor: Colors.orange,
                hintStyle: TextStyle(
                  color: Colors.grey,
                ),
                enabledBorder: UnderlineInputBorder(
                  borderSide: BorderSide(color: Colors.black, width: 1.0),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}