Flutter實現微信朋友圈功能
本文實例為大傢分享瞭Flutter實現微信朋友圈功能的具體代碼,供大傢參考,具體內容如下
今天給大傢實現一下微信朋友圈的效果,下面是效果圖
下面還是老樣子,還是以代碼的方式進行講解
import 'package:dio/dio.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:nursery_school_gardener/view/main/dynamic/FriendView/FriendCell.dart'; class Dynamic extends StatefulWidget { @override _DynamicState createState() => _DynamicState(); } class _DynamicState extends State<Dynamic> { // 朋友圈信息數據 List<Result> cachesData; @override void initState() { super.initState(); } @override Widget build(BuildContext context) { return CustomScaffold( contentWidget: Expanded( flex: 1, child: ListView.builder(// 朋友圈列表 itemBuilder: (BuildContext context, int index) { // 每一條的朋友圈 return FriendCell( result: cachesData[index],//將數據傳入每一條列表中 ); }, itemCount: cachesData.length(), ), ), ); } }
上面就是列表,下面是列表中的每一個樣式
import 'dart:math'; import 'package:flutter/material.dart'; import 'package:nursery_school_gardener/util/ColorUtils.dart'; class FriendCell extends StatefulWidget { // 上一頁傳過來的數據 Result result; FriendCell({this.result, Key key}) : super(key: key); @override _FriendCellState createState() => _FriendCellState(); } class _FriendCellState extends State<FriendCell> { TextEditingController editingController = new TextEditingController(); // 照片展示樣式,1張、2|4張、或者其他 Widget makePictureCount(List<KgPhotosList> pics) { if (pics.length == 1) { return GestureDetector( onTap: () { //點擊圖片 }, child: Container( margin: EdgeInsets.fromLTRB(0, 10, 50, 10), width: MediaQuery.of(context).size.width - 164, height: (MediaQuery.of(context).size.width - 164) / 2, decoration: BoxDecoration( image: DecorationImage( image: AssetImage("images/hsf2.jpg"),//展示的圖片根據需求展示不同類型 fit: BoxFit.cover, ), borderRadius: BorderRadius.circular(8), ), ), ); } else if (pics.length == 4 || pics.length == 2) { return Container( margin: EdgeInsets.fromLTRB(0, 10, 0, 10), child: Wrap( spacing: 5, runSpacing: 5, alignment: WrapAlignment.start, children: pics .map( (p) => GestureDetector( onTap: () { //點擊圖片 }, child: Container( width: (MediaQuery.of(context).size.width - 164) / 2.2, height: (MediaQuery.of(context).size.width - 164) / 2.2, decoration: BoxDecoration( image: DecorationImage( image: AssetImage("images/hsf2.jpg"),//展示的圖片根據需求展示不同類型 fit: BoxFit.cover, ), borderRadius: BorderRadius.circular(8), ), ), ), ) .toList(), ), ); } else if (pics.length == 3 || pics.length > 4) { return Container( margin: EdgeInsets.fromLTRB(0, 10, 0, 10), child: Wrap( spacing: 5, runSpacing: 5, alignment: WrapAlignment.start, children: pics .map( (p) => GestureDetector( onTap: () { //點擊圖片 }, child: Container( width: (MediaQuery.of(context).size.width - 164) / 3, height: (MediaQuery.of(context).size.width - 164) / 3, decoration: BoxDecoration( image: DecorationImage( image:AssetImage("images/hsf2.jpg"),//展示的圖片根據需求展示 fit: BoxFit.cover, ), borderRadius: BorderRadius.circular(8), ), ), ), ) .toList(), ), ); } else { return Container(); } } bool _isShow = true; @override Widget build(BuildContext context) { bool deleteStatus = widget.result.addTeacher != Variable.share().loginData.result.userInfo.id; return Container( margin: new EdgeInsets.only(left: 12, right: 12, bottom: 12), decoration: BoxDecoration( borderRadius: BorderRadius.circular(10), color: ColorUtils.WHITE, boxShadow: [ BoxShadow( color: ColorUtils.MAIN_BG, blurRadius: 10.0, spreadRadius: 1.0), ], ), child: Stack( children: [ Container( child: Column( children: <Widget>[ Flex( direction: Axis.horizontal, mainAxisAlignment: MainAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start, children: <Widget>[ //頭像 Container( width: 40, height: 40, margin: EdgeInsets.fromLTRB(15, 20, 15, 0), decoration: BoxDecoration( image: DecorationImage( image: AssetImage("images/hsf2.jpg"),//用戶頭像 fit: BoxFit.cover, ), borderRadius: BorderRadius.circular(8), ), ), Expanded( child: Container( margin: EdgeInsets.fromLTRB(0, 20, 60, 0), child: Column( mainAxisAlignment: MainAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start, children: <Widget>[ //姓名 Text( "姓名", style: TextStyle( fontSize: 17, color: Color(0XFF4D6CAB), fontWeight: FontWeight.w500), ), SizedBox( height: 5, ), //動態內容 Text( "內容", style: TextStyle(fontSize: 15), ), SizedBox( height: 5, ), //發表的圖片,上一頁面傳遞來的屬性 makePictureCount(widget.result.kgPhotosList), ], ), ), ), ], ), Stack( children: [ Flex( direction: Axis.horizontal, mainAxisAlignment: MainAxisAlignment.start, children: <Widget>[ //發佈的時間 Container( margin: EdgeInsets.only(left: 70, bottom: 5), child: Text( "時間", style: TextStyle( fontSize: 12, color: Color(0XFFB2B2B2)), ), ), //刪除朋友圈按鈕 deleteStatus代表是否是自己的朋友圈,是可以刪除, Offstage( offstage: deleteStatus, child: GestureDetector( onTap: () { CustomDialog.show(context, title: "動態刪除", message: "你確定要刪除當前動態嗎?", callBack: (flag) { if (flag) { delteDynamic(); } }); }, child: Container( margin: EdgeInsets.only(left: 5, bottom: 5), child: Text( "刪除", style: TextStyle( fontSize: 12, color: ColorUtils.BLUE_NORMAL), ), ), ), ), //管理員駁回按鈕,看是否是管理員,可以通過和駁回朋友圈 Offstage( offstage: ToolUtils.isContainsElement( Variable.share().USER_DYNAMIC_TYPE), child: GestureDetector( onTap: () { CustomDialog.show( context, title: "動態通過", message: "你確定要通過當前動態嗎?", callBack: (flag) { if (flag) {} }, ); }, child: Container( margin: EdgeInsets.only(left: 5, bottom: 5), child: Text( "通過", style: TextStyle( fontSize: 12, color: ColorUtils.BLUE_NORMAL), ), ), ), ), //管理員駁回按鈕,看是否是管理員,可以通過和駁回朋友圈 Offstage( offstage: ToolUtils.isContainsElement( Variable.share().USER_DYNAMIC_TYPE), child: GestureDetector( onTap: () { CustomInputDialog.show(context, title: "動態駁回", message: "你確定要駁回當前動態嗎?", callBack: (flag) { if (flag) {} }); }, child: Container( margin: EdgeInsets.only(left: 5, bottom: 5), child: Text( "駁回", style: TextStyle( fontSize: 12, color: ColorUtils.BLUE_NORMAL), ), ), ), ), ], ), Container( margin: EdgeInsets.only(right: 20), child: Row( mainAxisAlignment: MainAxisAlignment.end, children: <Widget>[ Offstage( offstage: _isShow, child: AnimatedContainer( decoration: BoxDecoration( borderRadius: BorderRadius.circular(5), color: Color(0XFF4C5154)), duration: Duration(milliseconds: 100), width: 130, height: 30, child: Flex( direction: Axis.horizontal, children: <Widget>[ // 點贊模塊 Expanded( flex: 1, child: Row( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ Icon( Icons.favorite_border, color: Colors.white, size: 15, ), SizedBox( width: 5, ), InkWell( onTap: () { // 點贊功能 setState( () { isShow(); addPraise(); }, ); }, child: Text( "贊", style: TextStyle( color: Colors.white, fontSize: 12), ), ), ], ), ), // 評論模塊 Expanded( flex: 1, child: Row( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ Icon( Icons.sms, color: Colors.white, size: 15, ), SizedBox( width: 5, ), InkWell( onTap: () { setState( () { isShow(); addDiscuss("我是評論內容"); }, ); }, child: Text( "評論", style: TextStyle( color: Colors.white, fontSize: 12), ), ), ], ), ), ], ), ), ), SizedBox( width: 10, ), InkWell( onTap: () { isShow(); }, child: Image.asset( "images/button.png", width: 22, height: 18, ), ), ], ), ), ], ), //評論模塊 Offstage( offstage: widget.result.kgPraiseList.length == 0 ? true : false, child: Container( constraints: BoxConstraints(minWidth: double.infinity), margin: EdgeInsets.fromLTRB(70, 10, 15, 0), padding: EdgeInsets.only(top: 5, bottom: 5, left: 10, right: 10), //因為點贊和評論是兩個控件,所以當他倆存在的時候需要設置對應的圓角,保證UI decoration: BoxDecoration( borderRadius: BorderRadius.only( topLeft: Radius.circular(8), topRight: Radius.circular(8), bottomLeft: Radius.circular( widget.result.kgDiscussList.length == 0 ? 8 : 0), bottomRight: Radius.circular( widget.result.kgDiscussList.length == 0 ? 8 : 0)), color: Color(0XFFF3F3F5), ), child: Wrap( alignment: WrapAlignment.start, runSpacing: 5, spacing: 5, children: likeView(widget.result.kgPraiseList.length)), ), ), //點贊模塊 Offstage( offstage: widget.result.kgDiscussList.length == 0 ? true : false, child: Container( constraints: BoxConstraints(minWidth: double.infinity), margin: EdgeInsets.fromLTRB(70, 0, 15, 0), padding: EdgeInsets.only(top: 5, bottom: 5, left: 10, right: 10), //因為點贊和評論是兩個控件,所以當他倆存在的時候需要設置對應的圓角,保證UI decoration: BoxDecoration( borderRadius: BorderRadius.only( topLeft: Radius.circular( widget.result.kgPraiseList.length == 0 ? 8 : 0), topRight: Radius.circular( widget.result.kgPraiseList.length == 0 ? 8 : 0), bottomLeft: Radius.circular(8), bottomRight: Radius.circular(8)), color: Color(0XFFF3F3F5), ), child: Wrap( alignment: WrapAlignment.start, runSpacing: 5, spacing: 5, children: talkView(widget.result.kgDiscussList.length)), ), ), SizedBox( height: 10, ), ], ), ), Offstage( offstage: true, child: Container( margin: new EdgeInsets.only( left: MediaQuery.of(context).size.width - 50, top: 20), child: Image.asset( "images/ic_no_network.png", width: 18, height: 18, ), ), ), ], ), ); } // 點贊和評論模塊是否顯示 void isShow() { setState(() { _isShow = !_isShow; }); } /* * 刪除朋友圈 * */ void delteDynamic() { // 刪除朋友圈 } /* * 發佈評論 * */ void addDiscuss(String discuss) { // 發佈評論 } /* * 點贊 * */ void addPraise() { // 點贊 } //點贊 List<Widget> likeView(int count) { List<Widget> result = []; // TODO: 這裡不要刪除,後期肯定會改回改版本,這個是展示所有點贊人的信息,因為項目需要這裡註釋瞭,隻用下面的方式 /* for (int i = 0; i < count; i++) { var praise = widget.result.kgPraiseList[i]; result.add( Container( child: Row( mainAxisSize: MainAxisSize.min, children: <Widget>[ Icon( Icons.favorite_border, size: 13, color: Color(0XFF566B94), ), SizedBox(width: 5), Container( child: Text( ToolUtils.isEmptyOrNull(praise.praisePerson), style: TextStyle( color: Color(0XFF566B94), fontSize: 15, fontWeight: FontWeight.w500), ), ) ], ), ), ); } */ // 點贊數量 if (widget.result.kgPraiseList.length > 0) { result.add( Container( child: Row( mainAxisSize: MainAxisSize.min, children: <Widget>[ Icon( Icons.favorite, size: 16, color: Color(0XFF4D6CAB), ), SizedBox(width: 5), Container( child: Text( "${widget.result.kgPraiseList.length}人贊過", style: TextStyle( color: Color(0XFF4D6CAB), fontSize: 14, fontWeight: FontWeight.w500), ), ) ], ), ), ); } return result; } //評論 List<Widget> talkView(int count) { List<Widget> result = []; for (int i = 0; i < count; i++) { var discuss = widget.result.kgDiscussList[i]; var rng = new Random(); result.add( Container( child: Flex( direction: Axis.vertical, children: [ Container( child: Row( children: <Widget>[ Expanded( child: Text.rich( TextSpan( style: TextStyle( fontSize: 15, color: Color(0xFF333333), ), children: [ TextSpan( text: ToolUtils.isEmptyOrNull( discuss.discussPerson) + ':', style: TextStyle( fontWeight: FontWeight.w500, color: Color(0XFF4D6CAB), ), ), TextSpan( text: ToolUtils.isEmptyOrNull( discuss.discussMessage)), ]), textAlign: TextAlign.start, ), ), ], ), ), Container(), ], ), ), ); } return result; } }
到此朋友圈效果的實現就完成瞭。
以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。
推薦閱讀:
- Flutter實現自定義篩選框的示例代碼
- Flutter 開發一個登錄頁面
- Flutter加載圖片的多樣玩法匯總
- flutter實現帶刪除動畫的listview功能
- 利用Flutter制作一個摸魚桌面版App