基础库
 
 
 
 
 

352 lines
11 KiB

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