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,至於如何搭建開發環境,可以去官網查閱文檔資料
使用vscode編輯器,可先安裝Dart 、Flutter 、Flutter widget snippets等擴展插件
3、flutter沉浸式狀態欄/底部tabbar
flutter中如何實現頂部全背景沉浸式透明狀態欄(去掉狀態欄黑色半透明背景),去掉右上角banner,可以去看這篇文章 segmentfault.com/a/11...
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: <Widget>[
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,至於如何使用可參看 pub.flutter-io.cn/packages/sh…
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就可實現。 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屬性提供的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開發聊天室實例就介紹到這里,希望能喜歡~~💪💪
2020年iOS高級面試題
有興趣的小伙伴進群交流:651612063 進群密碼111,不管你是小白還是大牛歡迎入駐 ,分享BAT,阿里面試題、面試經驗,討論技術, 大家一起交流學習成長!
