Flutter
是目前比較流行的跨平臺開發技術,憑藉其出色的性能得到不少前端技術愛好者的關注,好比阿里閒魚
,美團
,騰訊
等大公司都有投入相關案例生產使用。
flutter_chatroom項目是基於Flutter+Dart+chewie+photo_view+image_picker
等技術開發的跨平臺仿微信app聊天界面應用,實現了消息/表情發送、圖片預覽、長按菜單、紅包/小視頻/朋友圈等功能。html
鑑於flutter基於dart語言,須要安裝Dart Sdk / Flutter Sdk
,至於如何搭建開發環境,能夠去官網查閱文檔資料前端
https://flutterchina.club/segmentfault
使用vscode編輯器,可先安裝Dart
、Flutter
、Flutter widget snippets
等擴展插件框架
flutter中如何實現頂部全背景沉浸式透明狀態欄(去掉狀態欄黑色半透明背景),去掉右上角banner,能夠去看這篇文章
https://segmentfault.com/a/11...electron
Icon(Icons.search)
Icon(IconData(0xe60e, fontFamily:'iconfont'), size:24.0)
使用第二種方式須要先下載阿里圖標庫字體文件,而後在pubspec.yaml中引入字體async
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)
編輯器
如上圖:在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)
經過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: <Widget>[ Positioned( top: isTop ? _globalPositionY : _globalPositionY - 200.0, left: isLeft ? _globalPositionX : _globalPositionX - 120.0, width: 120.0, child: Material( ... ), ) ], ); } ); }
可經過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), ), ), ); } ); }
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<ScaffoldState>(); 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); }); } }
maxLines
就可實現。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();}, ), ),
經過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開發聊天室實例就介紹到這裏,但願能喜歡~~💪💪
最後附上electron桌面端應用實例
electron聊天室|vue+electron-vue仿微信客戶端|electron桌面聊天