简体   繁体   English

如何使小部件粘在键盘顶部

[英]How to make widget stick to top of keyboard

I'd like to make a widget that sticks to the bottom of the page, and then is pinned to the top of the keyboard (when it appears).我想制作一个贴在页面底部的小部件,然后固定在键盘顶部(当它出现时)。

Note how the input textfield is pinned to the keyboard in the image below:请注意下图中的输入文本字段是如何固定到键盘的:

TextField 贴在键盘上的聊天应用程序

How would I do this?我该怎么做? I tried putting it in the bottomNavigationBar , but this (obviously) didn't work.我尝试将它放在bottomNavigationBar ,但这(显然)不起作用。 Is there a builtin way to do this?有没有内置的方法来做到这一点?

This is a working example of the thing you want.这是你想要的东西的一个工作示例。 I think!我想! Just copy/paste/run只需复制/粘贴/运行

What's important in this example is the Expanded.在这个例子中重要的是 Expanded。 A really nice widget that expands to as much space as it can get.一个非常好的小部件,可以扩展到尽可能多的空间。 And in result pushing the chat box down as much as possible结果是尽可能地将聊天框向下推
(Bottom of the screen or bottom of the keyboard) (屏幕底部或键盘底部)

import 'package:flutter/material.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
    // This widget is the root of your application.
    @override
    Widget build(BuildContext context) {
        return new MaterialApp(
            title: 'Flutter Demo',
            theme: new ThemeData(
                primarySwatch: Colors.blue,
            ),
            home: new MyHomePage(title: 'Flutter Demo Home Page'),
        );
    }
}

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

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

class _MyHomePageState extends State<MyHomePage> {

    @override
    Widget build(BuildContext context) {
        return new Scaffold(
            appBar: new AppBar(
                title: new Text('49715760 Stackoverflow'),
            ),
            body: new Column(
                crossAxisAlignment: CrossAxisAlignment.stretch,
                children: <Widget>[
                    new Expanded(
                        child: new Material(
                            color: Colors.red,
                            child: new Text("Filled"),
                        ),
                    ),
                    new Container(
                        color: Colors.white,
                        padding: new EdgeInsets.all(10.0),
                        child: new TextField(
                            decoration: new InputDecoration(
                                hintText: 'Chat message',
                            ),
                        ),
                    ),
                ],
            ),
        );
    }
}

The best way to resolve this is to use a dedicated widget.解决此问题的最佳方法是使用专用小部件。

MediaQuery.of(context).viewInsets.bottom will give you the value of the height covered by the system UI(in this case the keyboard). MediaQuery.of(context).viewInsets.bottom 将为您提供系统 UI(在本例中为键盘)覆盖的高度值。

import 'dart:async';

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

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

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

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

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      resizeToAvoidBottomInset: false,
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: _getBody(),
      floatingActionButton: FloatingActionButton(
        onPressed: () {},
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }

  Widget _getBody() {
    return Stack(children: <Widget>[
      Container(
        decoration: BoxDecoration(
            image: DecorationImage(
                image: AssetImage("assets/sample.jpg"), fit: BoxFit.fitWidth)),
        // color: Color.fromARGB(50, 200, 50, 20),
        child: Column(
          children: <Widget>[TextField()],
        ),
      ),
      Positioned(
        bottom: MediaQuery.of(context).viewInsets.bottom,
        left: 0,
        right: 0,
        child: Container(
          height: 50,
          child: Text("Hiiiii"),
          decoration: BoxDecoration(color: Colors.pink),
        ),
      ),
    ]);
  }
}

there is a lib for that:有一个库:

https://pub.dev/packages/keyboard_attachable https://pub.dev/packages/keyboard_attachable

Widget build(BuildContext context) => FooterLayout(
    footer: MyFooterWidget(),
    child: PageMainContent(),
);

在此处输入图片说明

This worked for me,这对我有用,

showBottomSheet(
  context: context,
  builder: (context) => Container(
  height: // YOUR WIDGET HEIGHT
  child: // YOUR CHILD
)

showBottomSheet is a flutter inbuilt function. showBottomSheet 是一个颤振内置函数。

mohammad byervand's solution is the best indeed. mohammad byervand 的解决方案确实是最好的。 but keep in mind it would not work if you set android:windowSoftInputMode="adjustPan".但请记住,如果您设置 android:windowSoftInputMode="adjustPan",它将无法工作。 as same as MediaQuery.of(context).viewInsets.bottom and resizeToAvoidBottomInset: false与 MediaQuery.of(context).viewInsets.bottom 和 resizeToAvoidBottomInset: false 相同

use of bottomSheet option from Scaffold.使用 Scaffold 中的 bottomSheet 选项。

Scaffold(
    bottomSheet: chatBar(),
    body: Column(
           children: [
              Expanded(
               child: ListView()
              )
           ]
         )
    )

the chatBar is top of keyboard, when keyboard is open.当键盘打开时,聊天栏位于键盘顶部。

for transparent chatBar: can wrap Scaffold by对于透明聊天栏:可以通过以下方式包裹 Scaffold

Theme(
          data: ThemeData.light().copyWith(
              bottomSheetTheme: BottomSheetThemeData(backgroundColor: Colors.transparent),
              
          ),

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM