简体   繁体   English

如何在 Flutter 中创建底部评论小部件?

[英]How to create a bottom comment widget in Flutter?

I would like to build a comment widget floating at the bottom of screen.我想构建一个浮动在屏幕底部的评论小部件。 when user tap the input box, a key board pop-up.当用户点击输入框时,会弹出一个键盘。

Here is the problem.这是问题所在。 I tried to add a Container inside of a BottomNavigationBar.我试图在底部导航栏内添加一个容器。 But when I tap the input box, key board pop-up and covered the entire BottomNavigationBar.但是当我点击输入框时,键盘弹出并覆盖整个BottomNavigationBar。 SO I have no way to see what I just tapped in the input box.所以我无法看到我刚刚在输入框中点击了什么。

Here are 2 images to show you the comment widget box I would like to build.这里有 2 张图像向您展示我想要构建的评论小部件框。 And the key point is I don't want keyboard cover the comment widget.关键是我不希望键盘覆盖评论小部件。

Please help me.请帮我。

close key board关闭键盘

open key board打开键盘

MediaQuery.of(context).viewInsets returns ofsets which caused by keyboards. MediaQuery.of(context).viewInsets 返回由键盘引起的偏移量。 So, you can wrap your BottomNavigationBar into Padding like this:因此,您可以像这样将 BottomNavigationBar 包装到 Padding 中:

Scaffold(
  bottomNavigationBar: Padding(padding: MediaQuery.of(context).viewInsets,
  child: BottomNavigationBar(
    ...
  )
...

This will work best Flutter Comment Box这将最有效Flutter Comment Box

import 'package:comment_box/comment/comment.dart';
import 'package:flutter/material.dart';

class TestMe extends StatefulWidget {
  @override
  _TestMeState createState() => _TestMeState();
}

class _TestMeState extends State<TestMe> {
  final formKey = GlobalKey<FormState>();
  final TextEditingController commentController = TextEditingController();
  List filedata = [
    {
      'name': 'Adeleye Ayodeji',
      'pic': 'https://picsum.photos/300/30',
      'message': 'I love to code'
    },
    {
      'name': 'Biggi Man',
      'pic': 'https://picsum.photos/300/30',
      'message': 'Very cool'
    },
    {
      'name': 'Biggi Man',
      'pic': 'https://picsum.photos/300/30',
      'message': 'Very cool'
    },
    {
      'name': 'Biggi Man',
      'pic': 'https://picsum.photos/300/30',
      'message': 'Very cool'
    },
  ];

  Widget commentChild(data) {
    return ListView(
      children: [
        for (var i = 0; i < data.length; i++)
          Padding(
            padding: const EdgeInsets.fromLTRB(2.0, 8.0, 2.0, 0.0),
            child: ListTile(
              leading: GestureDetector(
                onTap: () async {
                  // Display the image in large form.
                  print("Comment Clicked");
                },
                child: Container(
                  height: 50.0,
                  width: 50.0,
                  decoration: new BoxDecoration(
                      color: Colors.blue,
                      borderRadius: new BorderRadius.all(Radius.circular(50))),
                  child: CircleAvatar(
                      radius: 50,
                      backgroundImage: NetworkImage(data[i]['pic'] + "$i")),
                ),
              ),
              title: Text(
                data[i]['name'],
                style: TextStyle(fontWeight: FontWeight.bold),
              ),
              subtitle: Text(data[i]['message']),
            ),
          )
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Comment Page"),
        backgroundColor: Colors.pink,
      ),
      body: Container(
        child: CommentBox(
          userImage:
              "https://lh3.googleusercontent.com/a-/AOh14GjRHcaendrf6gU5fPIVd8GIl1OgblrMMvGUoCBj4g=s400",
          child: commentChild(filedata),
          labelText: 'Write a comment...',
          errorText: 'Comment cannot be blank',
          sendButtonMethod: () {
            if (formKey.currentState.validate()) {
              print(commentController.text);
              setState(() {
                var value = {
                  'name': 'New User',
                  'pic':
                      'https://lh3.googleusercontent.com/a-/AOh14GjRHcaendrf6gU5fPIVd8GIl1OgblrMMvGUoCBj4g=s400',
                  'message': commentController.text
                };
                filedata.insert(0, value);
              });
              commentController.clear();
              FocusScope.of(context).unfocus();
            } else {
              print("Not validated");
            }
          },
          formKey: formKey,
          commentController: commentController,
          backgroundColor: Colors.black,
          textColor: Colors.white,
          sendWidget: Icon(Icons.send_sharp, size: 30, color: Colors.white),
        ),
      ),
    );
  }
}

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

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