使用Flutter开发的抖音国际版实例代码详解
程序员文章站
2022-11-23 10:30:00
简介最近花了两天时间研究使用flutter开发一个抖音国际版. 个人感觉使用flutter开发app快得不要不要的额. 两天就基本可以开发个大概出来. 最主要是热重载,太方便实时调整ui布局了. 相应...
简介
最近花了两天时间研究使用flutter开发一个抖音国际版. 个人感觉使用flutter开发app快得不要不要的额. 两天就基本可以开发个大概出来. 最主要是热重载,太方便实时调整ui布局了. 相应速度极快. 如下图:
主要项目架构
详细说明一下,开发主要在lib文件夹
- pubspec.yaml是配置插件的位置,如http: ^0.12.0+4,类似依赖组件.
- common文件夹存放的是重写的网络组件,以及图标组件icons.dart
- config文件夹存放的api.dart,wei调用的api配置文件
- models文件存放的实体层
- screen文件夹存放的页面view层
- tabs存放的底部切换文件夹层
- widgets存放的组件,包含视频播放组件player.dart以及左右等描述组件
功能介绍
主要的依赖组件,请使用国内镜像下载,切记切记!!!!
flutter: sdk: flutter flutter_svg: ^0.17.4 # the following adds the cupertino icons font to your application. # use with the cupertinoicons class for ios style icons. cupertino_icons: ^0.1.3 cached_network_image: ^2.2.0 json_annotation: ^3.0.1 font_awesome_flutter: ^8.8.1 http: ^0.12.0+4 provider: ^4.0.4 avatar_glow: any getflutter: ^1.0.11 flutter_money_formatter: ^0.8.3 video_player: ^0.10.8+1 dio: ^3.0.9 dio_cookie_manager: ^1.0.0
包含字体文件,主要为抖音自带的字体文件
import 'package:flutter/widgets.dart'; class douyinicons { douyinicons._(); static const _kfontfam = 'douyinicons'; static const icondata chat_bubble = const icondata(0xe808, fontfamily: _kfontfam); static const icondata create = const icondata(0xe809, fontfamily: _kfontfam); static const icondata heart = const icondata(0xe80a, fontfamily: _kfontfam); static const icondata home = const icondata(0xe80b, fontfamily: _kfontfam); static const icondata messages = const icondata(0xe80c, fontfamily: _kfontfam); static const icondata profile = const icondata(0xe80d, fontfamily: _kfontfam); static const icondata reply = const icondata(0xe80e, fontfamily: _kfontfam); static const icondata search = const icondata(0xe80f, fontfamily: _kfontfam); }
此次采用flutter开发安卓、ios等 app确实方便,主要为将tiktok的数据使用http下载下来.
import 'package:http/http.dart' as http; class requestcontroller { static string host = "https://www.tiktok.com/"; string url = host + "/share/item/list?secuid=&id=&type=5&count=30&mincursor=0&maxcursor=0&shareuid=&lang=en&_signature=pkb.ogageb9imosqahoqjksm.raapox"; future<string> getcookie() async { try { var response = await http.get(host + "/share/item/"); return response.headers["set-cookie"]; } catch (e) { return "error"; } }
model层
主要为实体层,解析json后绑定数据以及传递数据
class tiktok { int statuecode; body body; object errmsg; tiktok({this.statuecode, this.body, this.errmsg}); tiktok.fromjson(map<string, dynamic> json) { statuecode = json['statuscode']; body = json['body'] != null ? new body.fromjson(json['body']) : null; errmsg = json['errmsg']; } map<string, dynamic> tojson() { final map<string, dynamic> data = new map<string, dynamic>(); data['statuscode'] = this.statuecode; if (this.body != null) { data['body'] = this.body.tojson(); } data['errmsg'] = this.errmsg; return data; } }
视图层
另外屏幕层主要包含三个,homescreen,trendingscreen,以及显示videoscreen
import 'package:flutter/material.dart'; import 'package:flutter_app/screens/trendingscreen.dart'; import 'package:flutter_app/widgets/bottom_toolbar.dart'; class home extends statefulwidget { @override homestate createstate() => homestate(); } class homestate extends state<home> { int currentindex = 0; pagecontroller pagecontroller; @override widget build(buildcontext context) { return scaffold( body: pageview( controller: pagecontroller, children: <widget>[ trending(), ], onpagechanged: (int index) { setstate(() { currentindex = index; }); }, ), bottomnavigationbar: bottomitems(currentindex, pagecontroller), ); } }
tending层,主要包含读取抖音的api,将api转化成实体对象,绑定数据到videoscreen页面
import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:flutter/cupertino.dart'; import 'package:getflutter/getflutter.dart'; import 'package:flutter_app/config/api.dart'; import 'package:flutter_app/models/tiktok.dart'; import 'package:http/http.dart' as http; import 'package:flutter_app/screens/videoscreen.dart'; class trending extends statefulwidget { _trendingstate createstate() => _trendingstate(); } class _trendingstate extends state<trending> { pagecontroller pagecontroller; buildcontext context; requestcontroller api = requestcontroller(); list<widget> videos = []; gettrending() async { var cookies = await api.getcookie(); api.setcookie(cookies); try { var response = await http.get( api.url, headers: api.headers, ); tiktok tiktok = tiktok.fromjson(jsondecode(response.body)); tiktok.body.itemlistdata.foreach( (item) { setstate(() { videos.add(videoitem(data: item)); }); }, ); } catch (ex) { simpledialog( title: text('hot videos list is empty'), ); print(ex); } } @override void initstate() { super.initstate(); gettrending(); } @override widget build(buildcontext context) { context = context; return pageview( scrolldirection: axis.vertical, controller: pagecontroller, children: videos.length == 0 ? <widget>[ container( color: colors.black, child: center( child: gfloader( type: gfloadertype.circle, loadercolorone: colors.blueaccent, loadercolortwo: colors.white, loadercolorthree: colors.pink, ), ), ) ] : videos, ); } }
videoscreen主要为绑定数据. 展示抖音的视频
import 'package:flutter/material.dart'; import 'package:flutter_app/models/tiktok.dart'; import 'package:flutter_app/widgets/video_description.dart'; import 'package:flutter_app/widgets/actions_toolbar.dart'; import 'package:flutter_app/widgets/player.dart'; class videoitem extends statelesswidget { final itemlistdata data; const videoitem({@required this.data}); @override widget build(buildcontext context) { return scaffold( body: stack( children: <widget>[ douyinvideoplayer( url: data.iteminfos.video.urls[0], ), title(), videodescription( description: data.iteminfos.text, musicname: data.musicinfos.musicname, authorname: data.musicinfos.authorname, username: data.authorinfos.uniqueid, ), actionstoolbar( comments: data.iteminfos.commentcount.tostring(), userimg: data.authorinfos.covers[0], favorite: data.iteminfos.diggcount, coverimg: data.musicinfos.covers[0], ), ], ), ); } widget title() => align( alignment: alignment.topcenter, child: padding( padding: edgeinsets.symmetric(vertical: 28.0), child: text( "trending | for you", style: textstyle(color: colors.white, fontsize: 19.0), ), ), ); }
此次开发主要时间用在搭建flutter环境上,切记使用国内镜像,另外调式需要配合代理即可。
其他待完成的包含底部的导航页面,打算花两天时间把剩余的完成.
各位感兴趣的可以到我的github上点一下star. 留言可以教你们开发以及搭建dart环境. 地址:https://github.com/wangcharlie/douyin
总结
到此这篇关于使用flutter开发的抖音国际版的文章就介绍到这了,更多相关flutter抖音国际版内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!