基于flutter仿微信聊天室|dart+flutter聊天实例

  1. 云栖社区>
  2. 博客>
  3. 正文

基于flutter仿微信聊天室|dart+flutter聊天实例

xiaoyan2015 2020-05-13 17:30:21 浏览450
展开阅读全文

最近flutter跨平台应用技术比较火,作为前端开发有必要去学习了解下。这次给大家分享的是基于flutter+dart+chewie+photo_view+image_picker等技术开发仿微信app聊天项目。
022360_20200512003659242

技术点

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

flutter入口页面main.dart

/**
 * @tpl Flutter入口页面 | Q:282310962
 */

import 'package:flutter/material.dart';

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

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

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter App',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primaryColor: GStyle.appbarColor,
      ),
      home: TabBarPage(),
      onGenerateRoute: onGenerateRoute,
    );
  }
}

001360_20200512002407906

003360_20200512002631530

004360_20200512002755155

005360_20200512002840849

007360_20200512002934978

008360_20200512003004490

012360_20200512003148120

016360_20200512003322336

018360_20200512003422368

023360_20200512003901929

024360_20200512003937594

025360_20200512004305675

026360_20200512004446202

027360_20200512004604921

029360_20200512004708377

031360_20200512005508992

flutter图标Icon及自定义IconData组件

flutter中自带图标使用非常简单 Icon(Icons.search)
可是如果想要自定义图标,如使用阿里图标iconfont如何实现,这时就需要用到IconData来实现自定义图标了。Icon(IconData(0xe60e, fontFamily:'iconfont'), size:24.0)
使用IconData需要先下载阿里图标库字体文件,然后在pubspec.yaml中引入字体

360_20200513090806912

class GStyle {
    // __ 自定义图标
    static iconfont(int codePoint, {double size = 16.0, Color color}) {
        return Icon(
            IconData(codePoint, fontFamily: 'iconfont', matchTextDirection: true),
            size: size,
            color: color,
        );
    }
}

flutter实现badge红点提示

在app中,类似如下红点提醒很常见,平时微信中就有见到,可以flutter没有提供这种组件,只能自定义实现了。

360_20200513091117720

class GStyle {
    // 消息红点
    static badge(int count, {Color color = Colors.red, bool isdot = false, double height = 18.0, double width = 18.0}) {
        final _num = count > 99 ? '···' : count;
        return Container(
            alignment: Alignment.center, height: !isdot ? height : height/2, width: !isdot ? width : width/2,
            decoration: BoxDecoration(color: color, borderRadius: BorderRadius.circular(100.0)),
            child: !isdot ? Text('$_num', style: TextStyle(color: Colors.white, fontSize: 12.0)) : null
        );
    }
}

调用非常简单:支持自定义红点大小、颜色,默认数字超过99就...显示;
GStyle.badge(0, isdot:true)
GStyle.badge(13)
GStyle.badge(168, color: Colors.green, height: 17.0, width: 17.0)

flutter聊天页面|编辑框表情处理

360_20200513093616798

flutter中TextField文本框提供的maxLines属性可实现多行/换行文本,不过默认会有个高度,可在外层加个容器限制最小高度,然后设置 maxLines: null  keyboardType: TextInputType.multiline

Container(
    margin: GStyle.margin(10.0),
    decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(3.0)),
    constraints: BoxConstraints(minHeight: 30.0, maxHeight: 150.0),
    child: TextField(
        maxLines: null,
        keyboardType: TextInputType.multiline,
        decoration: InputDecoration(
          hintStyle: TextStyle(fontSize: 14.0),
          isDense: true,
          contentPadding: EdgeInsets.all(5.0),
          border: OutlineInputBorder(borderSide: BorderSide.none)
        ),
        controller: _textEditingController,
        focusNode: _focusNode,
        onChanged: (val) {
          setState(() {
            editorLastCursor = _textEditingController.selection.baseOffset;
          });
        },
        onTap: () {handleEditorTaped();},
    ),
),

flutter滚动聊天消息到最底部,通过ListView组件controller控制器实现

ScrollController _msgController = new ScrollController();
...
ListView(
    controller: _msgController,
    padding: EdgeInsets.all(10.0),
    children: renderMsgTpl(),
)

// 滚动消息至聊天底部
void scrollMsgBottom() {
    timer = Timer(Duration(milliseconds: 100), () => _msgController.jumpTo(_msgController.position.maxScrollExtent));
}

好了,基于flutter聊天室项目今天就介绍到这里,希望大家能喜欢~~

网友评论

登录后评论
0/500
评论
xiaoyan2015
+ 关注