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

haz*_*haz 3 keyboard flutter

我想制作一个粘贴在页面底部的小部件,然后将其固定在键盘的顶部(当它出现时).

请注意输入文本字段如何固定到下图中的键盘:

TextField粘贴到键盘的聊天应用程序

我该怎么做?我试过把它放进去bottomNavigationBar,但这(显然)不起作用.有没有内置的方法来做到这一点?

Ali*_*eri 13

使用Scaffold 中的bottomSheet 选项。

Scaffold(
    bottomSheet: chatBar(),
    body: Column(
           children: [
              Expanded(
               child: ListView()
              )
           ]
         )
    )
Run Code Online (Sandbox Code Playgroud)

当键盘打开时,聊天栏位于键盘顶部。

对于透明的chatBar:可以通过以下方式包装Scaffold

Theme(
  data: ThemeData.light().copyWith(
    bottomSheetTheme: BottomSheetThemeData(backgroundColor: Colors.transparent),  
  ),
Run Code Online (Sandbox Code Playgroud)


Kev*_*ter 11

这是你想要的事情的一个实例.我认为!只需复制/粘贴/运行

在这个例子中重要的是Expanded.一个非常好的小部件,可以扩展到尽可能多的空间.并在结果中尽可能地向下推动聊天框
(屏幕底部或键盘底部)

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',
                            ),
                        ),
                    ),
                ],
            ),
        );
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 遗憾的是,该解决方案与另一个解决方案类似,因为它并不理想,因为 TextField 以与键盘不同的速度移动到新位置,从而导致动画效果不太理想。 (4认同)

小智 10

解决此问题的最佳方法是使用专用小部件。

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),
        ),
      ),
    ]);
  }
}
Run Code Online (Sandbox Code Playgroud)

  • 这是一个不错的干净解决方案,但并不理想,因为 TextField 以与键盘不同的速度移动到新位置,从而导致动画效果不太理想。 (3认同)

Leo*_*nte 5

有一个库:

https://pub.dev/packages/keyboard_attachable

Widget build(BuildContext context) => FooterLayout(
    footer: MyFooterWidget(),
    child: PageMainContent(),
);
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明