查看: 1457|回复: 0
打印 上一主题 下一主题

flutter聊天App实例|flutter+dart仿微信界面

[复制链接]

该用户从未签到

28

主题

33

帖子

163

积分

注册会员

Rank: 2

积分
163
QQ
跳转到指定楼层
楼主
发表于 2020-5-15 00:14:06 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 xiaoyan2015 于 2020-5-15 00:20 编辑

FlutterChat聊天室是基于flutter+dart等技术开发的手机端App仿微信界面实例,实现编辑器光标处插入表情,图片预览,右键Popwin菜单,红包/朋友圈/视频等功能。

框架技术
编码/技术:Vscode + Flutter 1.12.13 + Dart 2.7.0
视频组件:chewie: ^0.9.7
图片/拍照:image_picker: ^0.6.6+1
图片预览组件:photo_view: ^0.9.2
弹窗组件:ShowDialog/SimpleDialog/AlertDialog/SnackBar(flutter封装自定义)
本地存储:shared_preferences: ^0.5.7+1
字体图标:阿里iconfont字体图标库

flutter目录结构/入口页面配置

  1. /**
  2. * @tpl Flutter入口页面 | Q:282310962
  3. */

  4. import 'package:flutter/material.dart';

  5. // 引入公共样式
  6. import 'styles/common.dart';

  7. // 引入底部Tabbar页面导航
  8. import 'components/tabbar.dart';

  9. // 引入地址路由
  10. import 'router/routes.dart';

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

  12. class MyApp extends StatelessWidget {
  13.   @override
  14.   Widget build(BuildContext context) {
  15.     return MaterialApp(
  16.       title: 'Flutter App',
  17.       debugShowCheckedModeBanner: false,
  18.       theme: ThemeData(
  19.         primaryColor: GStyle.appbarColor,
  20.       ),
  21.       home: TabBarPage(),
  22.       onGenerateRoute: onGenerateRoute,
  23.     );
  24.   }
  25. }
复制代码









flutter顶部状态栏全背景沉浸式+tabbar导航
flutter中如何实现顶部透明状态栏(去掉状态栏黑色半透明背景),去掉右上角banner,详细介绍可以去看这篇文章
Flutter沉浸式状态栏/AppBar导航栏/仿咸鱼底部凸起导航

flutter图标组件/使用阿里字体库

flutter中自带图标使用非常简单 Icon(Icons.search)
如果想要自定义图标,如使用阿里图标iconfont如何实现,这时就需要用到IconData来实现自定义图标了。Icon(IconData(0xe60e, fontFamily:'iconfont'), size:24.0)
使用IconData需要先下载阿里图标库字体文件,然后在pubspec.yaml中引入字体
  1. class GStyle {
  2.     // __ 自定义图标
  3.     static iconfont(int codePoint, {double size = 16.0, Color color}) {
  4.         return Icon(
  5.             IconData(codePoint, fontFamily: 'iconfont', matchTextDirection: true),
  6.             size: size,
  7.             color: color,
  8.         );
  9.     }
  10. }
复制代码
调用如下:支持自定义颜色、图标大小
GStyle.iconfont(0xe60e, color: Colors.orange, size: 17.0)

flutter实现未读消息圆形数字提醒
对于下面这种提示场景很常见,如微信消息未读提醒、朋友圈红点提示,在flutter中没有这种组件,需要自定义实现。

  1. class GStyle {
  2.     // 消息红点
  3.     static badge(int count, {Color color = Colors.red, bool isdot = false, double height = 18.0, double width = 18.0}) {
  4.         final _num = count > 99 ? '···' : count;
  5.         return Container(
  6.             alignment: Alignment.center, height: !isdot ? height : height/2, width: !isdot ? width : width/2,
  7.             decoration: BoxDecoration(color: color, borderRadius: BorderRadius.circular(100.0)),
  8.             child: !isdot ? Text('$_num', style: TextStyle(color: Colors.white, fontSize: 12.0)) : null
  9.         );
  10.     }
  11. }
复制代码
调用如下:支持自定义圆点颜色、大小及超过99会...提示
GStyle.badge(0, isdot:true)
GStyle.badge(13)
GStyle.badge(168, color: Colors.green, height: 17.0, width: 17.0)

flutter自定义长按弹窗菜单/去掉弹窗宽高限制
在flutter中如何实现类似微信聊天记录长按菜单,复制/发送给朋友/转发/收藏/删除

可通过flutter提供的InkWell组件onTapDown事件获取长按坐标点,然后onLongPress长按事件弹出菜单;
  1. InkWell(
  2.     splashColor: Colors.grey[200],
  3.     child: Container(...),
  4.     onTapDown: (TapDownDetails details) {
  5.         _globalPositionX = details.globalPosition.dx;
  6.         _globalPositionY = details.globalPosition.dy;
  7.     },
  8.     onLongPress: () {
  9.         _showPopupMenu(context);
  10.     },
  11. ),
复制代码
  1. double _globalPositionX = 0.0; //长按位置的横坐标
  2. double _globalPositionY = 0.0; //长按位置的纵坐标
  3. void _showPopupMenu(BuildContext context) {
  4.     // 确定点击位置在左侧还是右侧
  5.     bool isLeft = _globalPositionX > MediaQuery.of(context).size.width/2 ? false : true;
  6.     // 确定点击位置在上半屏幕还是下半屏幕
  7.     bool isTop = _globalPositionY > MediaQuery.of(context).size.height/2 ? false : true;

  8.     showDialog(
  9.       context: context,
  10.       builder: (context) {
  11.         return Stack(
  12.           children: <Widget>[
  13.             Positioned(
  14.               top: isTop ? _globalPositionY : _globalPositionY - 200.0,
  15.               left: isLeft ? _globalPositionX : _globalPositionX - 120.0,
  16.               width: 120.0,
  17.               child: Material(
  18.                 ...
  19.               ),
  20.             )
  21.           ],
  22.         );
  23.       }
  24.     );
  25. }
复制代码
flutter中弹窗大小貌似是固定的,如何去掉弹窗宽高大小限制?可通过SizedBox和无限制容器UnconstrainedBox组件配合实现
  1. void _showCardPopup(BuildContext context) {
  2.     showDialog(
  3.       context: context,
  4.       builder: (context) {
  5.         return UnconstrainedBox(
  6.           constrainedAxis: Axis.vertical,
  7.           child: SizedBox(
  8.             width: 260,
  9.             child: AlertDialog(
  10.               content: Container(
  11.                 ...
  12.               ),
  13.               elevation: 0,
  14.               contentPadding: EdgeInsets.symmetric(horizontal: 10.0, vertical: 20.0),
  15.             ),
  16.           ),
  17.         );
  18.       }
  19.     );
  20. }
复制代码
okey,关于flutter/dart开发聊天IM仿微信界面实例就介绍到这里,希望能喜欢~~
electron聊天室|vue+electron-vue仿微信客户端|electron桌面聊天
基于vue+uniapp仿抖音短视频/陌陌直播聊天室




分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 分享淘帖
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|Swiper中文网 ( 粤ICP备15001020号

GMT+8, 2024-4-27 06:19 , Processed in 0.096165 second(s), 31 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表