基础库
 
 
 
 
 

343 lines
11 KiB

  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_bloc/flutter_bloc.dart';
  3. import 'package:provider/provider.dart';
  4. import 'package:zhiying_comm/pages/login_page/model/login_model.dart';
  5. import 'package:zhiying_comm/zhiying_comm.dart';
  6. import 'package:cached_network_image/cached_network_image.dart';
  7. import 'package:zhiying_comm/util/empty_util.dart';
  8. import 'bloc/bloc.dart';
  9. import 'bloc/login_invite_repository.dart';
  10. import 'model/login_invite_user.dart';
  11. ///
  12. /// 邀请页面
  13. ///
  14. class LoginInvitePage extends StatelessWidget {
  15. @override
  16. Widget build(BuildContext context) {
  17. return Scaffold(
  18. resizeToAvoidBottomInset: false,
  19. backgroundColor: HexColor.fromHex('#FFFFFF'),
  20. body: BlocProvider(
  21. create: (_) => LoginInviteBloc(repostitory: LoginInviteRepository())..add(LoginInviteInitEvent()),
  22. child: LoginInvitePageContainer(),
  23. ),
  24. );
  25. }
  26. }
  27. ///
  28. /// 邀请
  29. class LoginInvitePageContainer extends StatefulWidget {
  30. @override
  31. _LoginInvitePageContainerState createState() => _LoginInvitePageContainerState();
  32. }
  33. class _LoginInvitePageContainerState extends State<LoginInvitePageContainer> {
  34. TextEditingController _editingController;
  35. FocusNode _focusNode;
  36. bool _showInviteInfo = false;
  37. /// 返回上一页
  38. void _openPop() {
  39. if (Navigator.canPop(context)) {
  40. Navigator.pop(context);
  41. }
  42. }
  43. /// 注册成功跳转
  44. void _successJump(){
  45. RouterUtil.hideKeyboard(context);
  46. Navigator.pushAndRemoveUntil(
  47. context,
  48. MaterialPageRoute(builder: (BuildContext context) => PageFactory.create('homePage', null)),
  49. (Route<dynamic> route) => false,
  50. );
  51. }
  52. /// 输入框输入变化
  53. void _onChange(String string) {
  54. setState(() {
  55. _showInviteInfo = false;
  56. });
  57. print('$string');
  58. if (!EmptyUtil.isEmpty(string) && string.length >= 3) {
  59. _queryInviteInfo(string);
  60. }
  61. }
  62. /// 查询邀请人
  63. void _queryInviteInfo(String inviteNum) {
  64. if (!EmptyUtil.isEmpty(inviteNum) && inviteNum.length < 3) {
  65. return;
  66. }
  67. BlocProvider.of<LoginInviteBloc>(context).add(LoginInviteQueryEvent(num: inviteNum));
  68. }
  69. /// 填写邀请啊吗
  70. void _submitOnClick(LoginInviteUser inviteUser) async {
  71. _focusNode.unfocus();
  72. /// 邀请码
  73. String inviteNum = inviteUser?.userId ?? '';
  74. /// 手机号
  75. UserInfoModel model = await Provider.of<UserInfoNotifier>(context, listen: false)?.getUserInfoModel();
  76. String mobile = model?.mobile ?? '';
  77. if (!EmptyUtil.isEmpty(inviteNum) && !EmptyUtil.isEmpty(mobile)) {
  78. BlocProvider.of<LoginInviteBloc>(context).add(LoginInviteSubmitEvent(mobile: mobile, num: inviteNum));
  79. }
  80. }
  81. @override
  82. void initState() {
  83. _editingController = TextEditingController();
  84. _focusNode = FocusNode();
  85. super.initState();
  86. }
  87. @override
  88. void didChangeDependencies() {
  89. super.didChangeDependencies();
  90. }
  91. @override
  92. void dispose() {
  93. _editingController?.dispose();
  94. _focusNode?.unfocus();
  95. _focusNode?.dispose();
  96. super.dispose();
  97. }
  98. @override
  99. Widget build(BuildContext context) {
  100. return BlocConsumer<LoginInviteBloc, LoginInviteState>(
  101. listener: (context, state) {},
  102. buildWhen: (previous, current) {
  103. /// 提交失败
  104. if (current is LoginInviteSubmitErrorState) {
  105. return false;
  106. }
  107. /// 数据加载出错
  108. if (current is LoginInviteErrorState) {
  109. return false;
  110. }
  111. /// 查询邀请人失败
  112. if(current is LoginInviteQueryErrorState){
  113. return false;
  114. }
  115. /// 邀请人查询成功
  116. if(current is LoginInviteQuerySuccessState){
  117. _showInviteInfo = true;
  118. return true;
  119. }
  120. /// 邀请码成功 跳转
  121. if (current is LoginInviteSubmitSuccess) {
  122. // 缓存数据
  123. Provider.of<UserInfoNotifier>(context, listen: false).setUserInfo(current.model);
  124. _successJump();
  125. return false;
  126. }
  127. return true;
  128. },
  129. builder: (context, state) {
  130. print(state);
  131. if (state is LoginInviteLoadedState) {
  132. return _getMainWidget(state.model, null);
  133. }
  134. if (state is LoginInviteQuerySuccessState) {
  135. return _getMainWidget(state.pageMdel, state.model);
  136. }
  137. /// 骨架屏
  138. return Container();
  139. },
  140. );
  141. }
  142. /// 主视图
  143. Widget _getMainWidget(LoginModel model, LoginInviteUser inviteUser) {
  144. return Column(
  145. children: <Widget>[
  146. /// appbar
  147. _getAppBar(model),
  148. /// 标题
  149. Padding(padding: const EdgeInsets.only(left: 27.5, right: 27.5, top: 40), child: _getTitleWidget(model)),
  150. /// 输入框
  151. Padding(padding: const EdgeInsets.only(left: 27.5, right: 27.5, top: 30), child: _getInviteInputWidget(model)),
  152. /// 邀请人信息
  153. Visibility(
  154. visible: inviteUser != null && _showInviteInfo,
  155. child: Padding(padding: const EdgeInsets.only(left: 27.5, right: 27.5, top: 8), child: _getInviteInfoWidget(inviteUser))),
  156. /// 按钮
  157. Padding(padding: const EdgeInsets.only(left: 27.5, right: 27.5, top: 30), child: _getSubmiBtnWidget(model, inviteUser)),
  158. ],
  159. );
  160. }
  161. /// appBar
  162. Widget _getAppBar(LoginModel model) {
  163. return AppBar(
  164. backgroundColor: HexColor.fromHex('#FFFFFF'),
  165. elevation: 0,
  166. title: Text(
  167. model?.invite?.appBarTitle ?? '登录',
  168. style: TextStyle(color: HexColor.fromHex(model?.invite?.appBarTitleColor ?? '#333333')),
  169. ),
  170. centerTitle: true,
  171. leading: IconButton(
  172. icon: Icon(
  173. Icons.arrow_back_ios,
  174. size: 22,
  175. color: HexColor.fromHex('#333333'),
  176. ),
  177. onPressed: () => _openPop(),
  178. ),
  179. );
  180. }
  181. /// title
  182. Widget _getTitleWidget(LoginModel model) {
  183. return Align(
  184. alignment: Alignment.centerLeft,
  185. child: Text(
  186. model?.invite?.title ?? '输入邀请码',
  187. style: TextStyle(color: HexColor.fromHex(model?.invite?.titleColor ?? '#333333'), fontSize: 25),
  188. ));
  189. }
  190. /// 邀请码输入框
  191. Widget _getInviteInputWidget(LoginModel model) {
  192. return _getCustomInputWidget(
  193. hint: model?.invite?.inputInviteText ?? '请输入邀请码',
  194. controller: _editingController,
  195. focusNode: _focusNode,
  196. onChanged: _onChange,
  197. hintColor: model?.invite?.inputHintColor ?? '#999999',
  198. bgColor: model?.invite?.inputBgColor ?? '#F7F7F7',
  199. textColor: model?.invite?.inputInviteTextColor ?? '#333333',
  200. iconUrl: model?.invite?.inputInviteIcon ?? '');
  201. }
  202. /// 邀请人信息
  203. Widget _getInviteInfoWidget(LoginInviteUser model) {
  204. return Container(
  205. // height: 77.5,
  206. decoration: BoxDecoration(borderRadius: BorderRadius.circular(8), border: Border.all(color: HexColor.fromHex('#E8E8E8'), width: 0.5)),
  207. padding: const EdgeInsets.all(15),
  208. child: Row(
  209. children: <Widget>[
  210. /// 头像
  211. CircleAvatar(
  212. radius: 23.5,
  213. backgroundImage: CachedNetworkImageProvider(model?.avatar ?? ''),
  214. ),
  215. const SizedBox(width: 13),
  216. Column(
  217. mainAxisAlignment: MainAxisAlignment.spaceAround,
  218. crossAxisAlignment: CrossAxisAlignment.start,
  219. children: <Widget>[
  220. /// 名字
  221. Text(
  222. '${model?.nickname}',
  223. style: TextStyle(color: HexColor.fromHex(model?.nickNameColor), fontSize: 13),
  224. ),
  225. /// 邀请
  226. RichText(
  227. text: TextSpan(text: '邀请您进入', style: TextStyle(fontSize: 11, color: HexColor.fromHex(model?.nickNameColor)), children: [
  228. TextSpan(
  229. text: '${model?.appName}',
  230. style: TextStyle(fontSize: 11, color: HexColor.fromHex(model?.appNameColor)),
  231. ),
  232. ]),
  233. )
  234. ],
  235. )
  236. ],
  237. ),
  238. );
  239. }
  240. /// 按钮
  241. Widget _getSubmiBtnWidget(LoginModel model, LoginInviteUser inviteUser) {
  242. return Material(
  243. child: Container(
  244. height: 52,
  245. width: double.infinity,
  246. color: Colors.white,
  247. child: RaisedButton(
  248. child: Text(
  249. model?.invite?.btnSubmitText ?? '进入智莺生活',
  250. style: TextStyle(fontSize: 15),
  251. ),
  252. textColor: HexColor.fromHex(model?.invite?.btnSubmitTextColor ?? '#FFFFFF'),
  253. color: HexColor.fromHex(model?.invite?.btnSubmitBgColor ?? '#FF3939'),
  254. disabledColor: HexColor.fromHex(model?.invite?.btnBanBgColor ?? '#F5F5F5'),
  255. disabledTextColor: HexColor.fromHex(model?.invite?.btnBanTextColor ?? '#999999'),
  256. elevation: 5,
  257. shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(52 / 2)),
  258. onPressed: _showInviteInfo && inviteUser != null ? ()=> _submitOnClick(inviteUser) : null,
  259. ),
  260. ),
  261. );
  262. }
  263. /// 自定义输入框
  264. Widget _getCustomInputWidget(
  265. {String hint, String hintColor, String bgColor, String textColor, String iconUrl, TextEditingController controller, ValueChanged<String> onChanged, FocusNode focusNode}) {
  266. var border = OutlineInputBorder(borderRadius: BorderRadius.circular(8), borderSide: BorderSide(color: HexColor.fromHex(bgColor), width: 0));
  267. return Container(
  268. height: 42,
  269. padding: const EdgeInsets.symmetric(horizontal: 15),
  270. decoration: BoxDecoration(
  271. color: HexColor.fromHex(bgColor),
  272. borderRadius: BorderRadius.circular(8),
  273. ),
  274. child: Row(
  275. mainAxisAlignment: MainAxisAlignment.start,
  276. crossAxisAlignment: CrossAxisAlignment.center,
  277. children: <Widget>[
  278. CachedNetworkImage(
  279. imageUrl: iconUrl ?? '',
  280. width: 10,
  281. ),
  282. Expanded(
  283. child: TextField(
  284. controller: controller,
  285. focusNode: focusNode,
  286. onChanged: onChanged,
  287. expands: false,
  288. style: TextStyle(color: HexColor.fromHex(textColor)),
  289. maxLines: 1,
  290. keyboardType: TextInputType.number,
  291. decoration: InputDecoration(
  292. contentPadding: EdgeInsets.only(top: 30, left: 7.5),
  293. hintText: hint,
  294. hintStyle: TextStyle(fontSize: 13, color: HexColor.fromHex(hintColor)),
  295. hintMaxLines: 1,
  296. filled: true,
  297. fillColor: Colors.transparent,
  298. border: border,
  299. focusedBorder: border,
  300. enabledBorder: border,
  301. disabledBorder: border,
  302. errorBorder: border,
  303. focusedErrorBorder: border,
  304. ),
  305. ),
  306. ),
  307. ],
  308. ),
  309. );
  310. }
  311. }