flutter offset_Flutter 仿微信界面聊天室 | 基于 (Flutter+Dart) 聊天实例
1、項目介紹
Flutter是目前比較流行的跨平臺開發技術,憑借其出色的性能獲得很多前端技術愛好者的關注,比如阿里閑魚,美團,騰訊等大公司都在投入相關案例生產使用。flutter_chatroom項目是基于Flutter+Dart+chewie+photo_view+image_picker等技術開發的跨平臺仿微信app聊天界面應用,實現了消息/表情發送、圖片預覽、長按菜單、紅包/小視頻/朋友圈等功能。
2、技術框架
- 使用技術:Flutter 1.12.13/Dart 2.7.0
- 視頻組件:chewie: ^0.9.7
- 圖片/拍照:image_picker: ^0.6.6+1
- 圖片預覽組件:photo_view: ^0.9.2
- 彈窗組件:showModalBottomSheet/AlertDialog/SnackBar
- 本地存儲:shared_preferences: ^0.5.7+1
- 字體圖標:阿里iconfont字體圖標庫
鑒于flutter基于dart語言,需要安裝Dart Sdk / Flutter Sdk,至于如何搭建開發環境,可以去官網查閱文檔資料
https://flutter.cn/
https://flutterchina.club/
https://pub.flutter-io.cn/
https://www.dartcn.com/
使用vscode編輯器,可先安裝Dart 、Flutter 、Flutter widget snippets等擴展插件
3、flutter沉浸式狀態欄/底部tabbar
flutter中如何實現頂部全背景沉浸式透明狀態欄(去掉狀態欄黑色半透明背景),去掉右上角banner
4、flutter圖標組件/IconData自定義封裝組件
- 1、使用系統圖標組件: Icon(Icons.search)
- 2、使用IconData方式: Icon(IconData(0xe60e, fontFamily:'iconfont'), size:24.0)
使用第二種方式需要先下載阿里圖標庫字體文件,然后在pubspec.yaml中引入字體
class GStyle { // __ 自定義圖標 static iconfont(int codePoint, {double size = 16.0, Color color}) { return Icon( IconData(codePoint, fontFamily: 'iconfont', matchTextDirection: true), size: size, color: color, ); }}調用非常簡單,可自定義顏色、字體大小;GStyle.iconfont(0xe635, color: Colors.orange, size: 17.0)
5、flutter實現badge紅點/圓點提示
如上圖:在flutter中沒有圓點提示組件,需要自己封裝實現;
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(29, color: Colors.orange, height: 15.0, width: 15.0)
6、flutter長按自定義彈窗
- 在flutter中如何實現長按,并在長按位置彈出菜單,類似微信消息長按彈窗效果;
通過InkWell組件提供的onTapDown事件獲取坐標點實現
InkWell( splashColor: Colors.grey[200], child: Container(...), onTapDown: (TapDownDetails details) { _globalPositionX = details.globalPosition.dx; _globalPositionY = details.globalPosition.dy; }, onLongPress: () { _showPopupMenu(context); },),// 長按彈窗double _globalPositionX = 0.0; //長按位置的橫坐標double _globalPositionY = 0.0; //長按位置的縱坐標void _showPopupMenu(BuildContext context) { // 確定點擊位置在左側還是右側 bool isLeft = _globalPositionX > MediaQuery.of(context).size.width/2 ? false : true; // 確定點擊位置在上半屏幕還是下半屏幕 bool isTop = _globalPositionY > MediaQuery.of(context).size.height/2 ? false : true; showDialog( context: context, builder: (context) { return Stack( children: [ Positioned( top: isTop ? _globalPositionY : _globalPositionY - 200.0, left: isLeft ? _globalPositionX : _globalPositionX - 120.0, width: 120.0, child: Material( ... ), ) ], ); } );}- flutter如何實現去掉AlertDialog彈窗大小限制?
可通過SizedBox和無限制容器UnconstrainedBox組件實現
void _showCardPopup(BuildContext context) { showDialog( context: context, builder: (context) { return UnconstrainedBox( constrainedAxis: Axis.vertical, child: SizedBox( width: 260, child: AlertDialog( content: Container( ... ), elevation: 0, contentPadding: EdgeInsets.symmetric(horizontal: 10.0, vertical: 20.0), ), ), ); } );}7、flutter登錄/注冊表單|本地存儲
flutter提供了兩個文本框組件:TextField 和 TextFormField本文是使用TextField實現,并在文本框后添加清空文本框/密碼查看圖標
TextField( keyboardType: TextInputType.phone, controller: TextEditingController.fromValue(TextEditingValue( text: formObj['tel'], selection: new TextSelection.fromPosition(TextPosition(affinity: TextAffinity.downstream, offset: formObj['tel'].length)) )), decoration: InputDecoration( hintText: "請輸入手機號", isDense: true, hintStyle: TextStyle(fontSize: 14.0), suffixIcon: Visibility( visible: formObj['tel'].isNotEmpty, child: InkWell( child: GStyle.iconfont(0xe69f, color: Colors.grey, size: 14.0), onTap: () { setState(() { formObj['tel'] = ''; }); } ), ), border: OutlineInputBorder(borderSide: BorderSide.none) ), onChanged: (val) { setState(() { formObj['tel'] = val; }); },)TextField( decoration: InputDecoration( hintText: "請輸入密碼", isDense: true, hintStyle: TextStyle(fontSize: 14.0), suffixIcon: InkWell( child: Icon(formObj['isObscureText'] ? Icons.visibility_off : Icons.visibility, color: Colors.grey, size: 14.0), onTap: () { setState(() { formObj['isObscureText'] = !formObj['isObscureText']; }); }, ), border: OutlineInputBorder(borderSide: BorderSide.none) ), obscureText: formObj['isObscureText'], onChanged: (val) { setState(() { formObj['pwd'] = val; }); },)驗證消息提示則是使用flutter提供的SnackBar實現
// SnackBar提示final _scaffoldkey = new GlobalKey();void _snackbar(String title, {Color color}) { _scaffoldkey.currentState.showSnackBar(SnackBar( backgroundColor: color ?? Colors.redAccent, content: Text(title), duration: Duration(seconds: 1), ));}另外本地存儲使用的是shared_preferences,至于如何使用可參看https://pub.flutter-io.cn/packages/shared_preferences
void handleSubmit() async { if(formObj['tel'] == '') { _snackbar('手機號不能為空'); }else if(!Util.checkTel(formObj['tel'])) { _snackbar('手機號格式有誤'); }else if(formObj['pwd'] == '') { _snackbar('密碼不能為空'); }else { // ...接口數據 // 設置存儲信息 final prefs = await SharedPreferences.getInstance(); prefs.setBool('hasLogin', true); prefs.setInt('user', int.parse(formObj['tel'])); prefs.setString('token', Util.setToken()); _snackbar('恭喜你,登錄成功', color: Colors.greenAccent[400]); Timer(Duration(seconds: 2), (){ Navigator.pushNamedAndRemoveUntil(context, '/tabbarpage', (route) => route == null); }); }}8、flutter聊天頁面功能
- 在flutter中如何實現類似上圖編輯器功能?通過TextField提供的多行文本框屬性maxLines就可實現。
- flutter實現滾動聊天信息到最底部
通過ListView里controller屬性提供的jumpTo方法及_msgController.position.maxScrollExtent
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/dart開發聊天室實例就介紹到這里,希望能喜歡~~
最后
Flutter作為跨平臺開發技術、Flutter以其美觀、快速、高效、開放等優勢迅速俘獲人心,但很多Flutter興趣愛好者進階學習缺少資源,今天我把搜集和整理的這份學習資源分享給有需要的人,若是有所需要,麻煩各位轉發一下(可以幫助更多的人看到喲!),記得一定要關注+轉發,然后私信@蕪湖Android“Flutter”,即可回復免費下載的方式!!
以下是部分內容展示:
也希望 Flutter 生態越來越好 (flutter開發App效率真的很高,開發體驗也是很好的 )。
總結
以上是生活随笔為你收集整理的flutter offset_Flutter 仿微信界面聊天室 | 基于 (Flutter+Dart) 聊天实例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 获取自动增涨列的表中添加完成后的数据
- 下一篇: SmartTemplate学习入门一