【问题标题】:Flutter: How to make telegram type bubble in chat messageFlutter:如何在聊天消息中制作电报类型气泡
【发布时间】:2019-07-07 13:21:10
【问题描述】:

电流输出

预期输出


代码

Align(
  alignment: Alignment.topRight,
  child: Container(
    padding: const EdgeInsets.symmetric(vertical: 8, horizontal: 20),
    margin: EdgeInsets.only(right: 12, top: 8),
    decoration: BoxDecoration(
      color: Color(0xFF486993),
      borderRadius: BorderRadius.all(Radius.circular(20)),
    ),
    child: Text("This is my message"),
  ),
)

【问题讨论】:

标签: flutter dart flutter-layout


【解决方案1】:

这正是在 Flutter 中创建整洁的语音气泡所需要的。

首先创建一个扩展 Painter 类的自定义 Painter 类

class CustomChatBubble extends CustomPainter {
  CustomChatBubble({this.color, @required this.isOwn});

  final Color color;
  final bool isOwn;

  @override
  void paint(Canvas canvas, Size size) {
    final paint = Paint()..color = color ?? Colors.blue;

    Path paintBubbleTail() {
      Path path;
      if (!isOwn) {
        path = Path()
          ..moveTo(5, size.height - 5)
          ..quadraticBezierTo(-5, size.height, -16, size.height - 4)
          ..quadraticBezierTo(-5, size.height - 5, 0, size.height - 17);
      }
      if (isOwn) {
        path = Path()
          ..moveTo(size.width - 6, size.height - 4)
          ..quadraticBezierTo(
              size.width + 5, size.height, size.width + 16, size.height - 4)
          ..quadraticBezierTo(
              size.width + 5, size.height - 5, size.width, size.height - 17);
      }
      return path;
    }

    final RRect bubbleBody = RRect.fromRectAndRadius(
        Rect.fromLTWH(0, 0, size.width, size.height), Radius.circular(16));
    final Path bubbleTail = paintBubbleTail();

    canvas.drawRRect(bubbleBody, paint);
    canvas.drawPath(bubbleTail, paint);
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) {
    // TODO: implement shouldRepaint
    return true;
  }
}

然后在您的聊天屏幕中使用它。请注意,此屏幕仅用于演示目的,您可以根据需要进行修改。

class ChatSampleWidget extends StatefulWidget {
  @override
  _ChatSampleWidgetState createState() => _ChatSampleWidgetState();
}

class _ChatSampleWidgetState extends State<ChatSampleWidget> {
  TextEditingController _editingController = TextEditingController();
  FocusNode _focusNode = FocusNode();
  final TextStyle textStyle = TextStyle(color: Colors.white);
  @override
  void initState() {
    super.initState();
  }

  @override
  dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Chatter'),
      ),
      body: SafeArea(
        child: Stack(
          children: <Widget>[
            Container(
              padding: EdgeInsets.all(20),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.start,
               
                children: <Widget>[
                  Row(
                    mainAxisAlignment: MainAxisAlignment.start,
                    children: <Widget>[
                      CustomPaint(
                          painter: CustomChatBubble(isOwn: false),
                          child: Container(
                              padding: EdgeInsets.all(8),
                              child: Text(
                                'Message from someone else \n Says sometihngs',
                                style: textStyle,
                              ))),
                    ],
                  ),
                  SizedBox(
                    height: 5,
                  ),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.start,
                    children: <Widget>[
                      CustomPaint(
                          painter:
                              CustomChatBubble(color: Colors.grey, isOwn: false),
                          child: Container(
                              padding: EdgeInsets.all(10),
                              child: FlutterLogo())),
                    ],
                  ),
                  SizedBox(
                    height: 5,
                  ),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.end,
                    children: <Widget>[
                      CustomPaint(
                          painter:
                              CustomChatBubble(color: Colors.green, isOwn: true),
                          child: Container(
                              padding: EdgeInsets.all(8),
                              child: Text(
                                'Message from me',
                                style: textStyle,
                              ))),
                    ],
                  )
                ],
              ),
            ),
            Positioned(
                bottom: 0,
                child: Container(
                  padding: EdgeInsets.all(8),
                  width: MediaQuery.of(context).size.width,
                  color: Colors.grey.withOpacity(0.1),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceAround,
                    children: <Widget>[
                      Flexible(
                        child: TextField(
                          controller: _editingController,
                          focusNode: _focusNode,
                          decoration:
                              InputDecoration(hintText: 'Say something...'),
                        ),
                      ),
                      IconButton(
                          icon: Icon(
                            Icons.send,
                            size: 30,
                          ),
                          onPressed: () {
                            print(_editingController.text);
                          })
                    ],
                  ),
                ))
          ],
        ),
      ),
    );
  }
}

这就是它的样子;

【讨论】:

    【解决方案2】:

    你在这里:

    import 'package:flutter/material.dart';
    
    void main() => runApp(
        MyApp()
    );
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          home: Padding(
            padding: EdgeInsets.all(7),
            child: Align(
              alignment: Alignment.centerRight,
              child: Stack(
                children: [
                  Container(
                    padding: const EdgeInsets.symmetric(vertical: 8, horizontal: 20),
                    decoration: BoxDecoration(
                      color: Color(0xFF486993),
                      borderRadius: BorderRadius.all(Radius.circular(20)),
                    ),
                    child: Row(
                      mainAxisSize: MainAxisSize.min,
                      children: <Widget>[
                        RichText(
                          text: TextSpan(
                            children: <TextSpan>[
                              TextSpan(
                                text: 'This is telegram message   ',
                                style: TextStyle(
                                    color: Colors.white,
                                    fontSize: 14.0
                                ),
                              ),
                              TextSpan(
                                text: '3:16 PM',
                                style: TextStyle(
                                    color: Colors.grey,
                                    fontSize: 12.0,
                                    fontStyle: FontStyle.italic
                                ),
                              ),
                            ],
                          ),
                        ),
                        Icon(Icons.check, color: Color(0xFF7ABAF4), size: 16,)
                      ]
                    ),
                  ),
                  Positioned(
                    bottom: 0,
                    right: 0,
                    child: CustomPaint(
                      painter: ChatBubbleTriangle(),
                    )
                  )
                ]
              )
            ),
          ),
        );
      }
    }
    
    class ChatBubbleTriangle extends CustomPainter {
      @override
      void paint(Canvas canvas, Size size) {
        var paint = Paint()..color = Color(0xFF486993);
    
        var path = Path();
        path.lineTo(-15, 0);
        path.lineTo(0, -15);
        path.lineTo(0, 0);
        canvas.drawPath(path, paint);
      }
    
      @override
      bool shouldRepaint(CustomPainter oldDelegate) {
        return true;
      }
    }
    
    

    您将不得不更改paint 方法,因为现在它只是一个三角形。

    否则,它看起来像您要求的那样。

    【讨论】:

    • 也看看这个question
    • 非常感谢,我知道这需要使用CustomPainter 来完成,但这就是我遇到困难的地方。您在右下角创建了一个三角形,这只是一种解决方法而不是正确的解决方案。我真的不能接受这一点,但我绝对必须为你的努力投票。
    【解决方案3】:

    尝试自己重新创建 Telegram 布局,我想出了这个来制作形状;

    enum _TailDirection { right, left }
    
    class ChatBubblePainter extends CustomPainter {
      ChatBubblePainter({this.color, this.isSentByMe});
    
      final Color color;
      final bool isSentByMe;
    
      @override
      void paint(Canvas canvas, Size size) {
        final paint = Paint()..color = color;
    
        Path paintBubbleTail(_TailDirection direction) {
          double startingPoint;
          double point;
          double endPoint;
          double curvePoint;
          if (direction == _TailDirection.right) {
            startingPoint = size.width - 5;
            point = size.width + 10;
            endPoint = size.width + 3;
            curvePoint = size.width;
          }
          if (direction == _TailDirection.left) {
            startingPoint = 5;
            point = -10;
            endPoint = -3;
            curvePoint = 0;
          }
          return Path()
            ..moveTo(startingPoint, size.height)
            ..lineTo(point, size.height)
            ..quadraticBezierTo(endPoint, size.height, curvePoint, size.height - 10);
        }
    
        final RRect bubbleBody = RRect.fromRectAndRadius(
            Rect.fromLTWH(0, 0, size.width, size.height), Radius.circular(5.0));
        final Path bubbleTail = isSentByMe
            ? paintBubbleTail(_TailDirection.right)
            : paintBubbleTail(_TailDirection.left);
    
        canvas.drawRRect(bubbleBody, paint);
        canvas.drawPath(bubbleTail, paint);
      }
    
      @override
      bool shouldRepaint(CustomPainter oldDelegate) {
        // TODO: implement shouldRepaint
        return true;
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2022-11-17
      • 1970-01-01
      • 2022-11-18
      • 2020-02-29
      • 2021-12-03
      • 1970-01-01
      • 2020-01-29
      • 2021-12-16
      • 1970-01-01
      相关资源
      最近更新 更多