基础组件库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

login_page.dart 8.5 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. import 'package:flutter/cupertino.dart';
  2. import 'package:flutter/gestures.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:flutter_bloc/flutter_bloc.dart';
  5. import 'package:zhiying_base_widget/pages/login_page/bloc/bloc.dart';
  6. import 'package:zhiying_base_widget/pages/login_page/bloc/login_bloc.dart';
  7. import 'package:zhiying_base_widget/pages/login_page/bloc/login_repository.dart';
  8. import 'package:zhiying_base_widget/pages/login_page/model/login_model.dart';
  9. import 'package:zhiying_comm/util/empty_util.dart';
  10. import 'package:zhiying_comm/zhiying_comm.dart';
  11. import 'package:cached_network_image/cached_network_image.dart';
  12. ///
  13. /// 登陆页面
  14. ///
  15. class LoginPage extends StatelessWidget {
  16. final Map<String, dynamic> data;
  17. const LoginPage(this.data, {Key key}) : super(key: key);
  18. @override
  19. Widget build(BuildContext context) {
  20. return Scaffold(
  21. backgroundColor: HexColor.fromHex('#FFFFFF'),
  22. body: BlocProvider<LoginBloc>(
  23. create: (_) => LoginBloc(repository: LoginRepository())..add(LoginInitEvent()),
  24. child: LoginPageContainer(),
  25. ),
  26. );
  27. }
  28. }
  29. class LoginPageContainer extends StatefulWidget {
  30. @override
  31. _LoginPageContainerState createState() => _LoginPageContainerState();
  32. }
  33. class _LoginPageContainerState extends State<LoginPageContainer> {
  34. /// 微信登陆
  35. void _loginClick(String type) {
  36. print('登陆$type');
  37. if(type == 'mobile'){
  38. Navigator.push(context, MaterialPageRoute(
  39. builder: (_) => PageFactory.create('login_account', null)
  40. ));
  41. }
  42. }
  43. /// 第三方登陆
  44. void _otherLoginClick(BottomIcons model) {
  45. print('第三方登陆${model.type}');
  46. }
  47. /// 跳到用户协议
  48. void _jumpUserAgreement(String url) {
  49. if(!EmptyUtil.isEmpty(url)) {
  50. print('协议');
  51. }
  52. }
  53. /// 展开关闭其它登陆
  54. void _showOrColoseOtherLogin() {
  55. setState(() {
  56. _showOther = !_showOther;
  57. });
  58. }
  59. final _sizedHeight50 = const SizedBox(height: 50);
  60. final _sizedHeight9 = const SizedBox(height: 9);
  61. final _sizedHeight18 = const SizedBox(height: 18);
  62. final _sizedHeight21 = const SizedBox(height: 21);
  63. bool _showOther = true;
  64. @override
  65. Widget build(BuildContext context) {
  66. return BlocConsumer<LoginBloc, LoginState>(
  67. listener: (context, state) {
  68. // Fluttertoast.showToast(msg: '网络异常');
  69. },
  70. buildWhen: (prev, current) {
  71. if (current is LoginErrorState) {
  72. return false;
  73. }
  74. return true;
  75. },
  76. builder: (context, state) {
  77. if (state is LoginCacheState) {
  78. return _getMainWidget(state.model);
  79. }
  80. if (state is LoginLoadedState) {
  81. return _getMainWidget(state.model);
  82. }
  83. return Container();
  84. },
  85. );
  86. }
  87. /// 主视图
  88. Widget _getMainWidget(LoginModel model) {
  89. return Column(
  90. children: <Widget>[
  91. /// 头部
  92. _headWidget(model),
  93. _sizedHeight50,
  94. /// 按钮
  95. _buttonsWidget(model),
  96. _sizedHeight9,
  97. /// 协议
  98. _protocolWidget(model),
  99. /// 其它登陆方式
  100. _otherLoginWidget(model),
  101. ],
  102. );
  103. }
  104. /// 头部Widget
  105. Widget _headWidget(LoginModel model) {
  106. return Container(
  107. height: 228 + MediaQuery.of(context).padding.top,
  108. width: double.infinity,
  109. decoration: BoxDecoration(
  110. image: DecorationImage(
  111. image: CachedNetworkImageProvider(model?.main?.backgroundImg ?? ''),
  112. fit: BoxFit.fill,
  113. ),
  114. ),
  115. child: Stack(
  116. alignment: Alignment.center,
  117. children: <Widget>[
  118. AppBar(
  119. backgroundColor: Colors.transparent,
  120. elevation: 0,
  121. leading: Icon(
  122. Icons.arrow_back_ios,
  123. size: 22,
  124. color: HexColor.fromHex('#333333'),
  125. ),
  126. ),
  127. Column(
  128. crossAxisAlignment: CrossAxisAlignment.center,
  129. mainAxisAlignment: MainAxisAlignment.center,
  130. children: <Widget>[
  131. /// logo
  132. Container(
  133. margin: EdgeInsets.only(bottom: 12, top: MediaQuery.of(context).padding.top),
  134. decoration: BoxDecoration(
  135. borderRadius: BorderRadius.circular(14),
  136. boxShadow: [
  137. BoxShadow(color: Colors.grey[300], offset: Offset(0.0, 0.0), blurRadius: 10.0, spreadRadius: 1.0),
  138. BoxShadow(color: Colors.grey[300], offset: Offset(0.0, 0.0)),
  139. ],
  140. ),
  141. height: 80,
  142. width: 80,
  143. child: CachedNetworkImage(imageUrl: model?.logoImg ?? ''),
  144. ),
  145. /// logo 名字
  146. CachedNetworkImage( imageUrl: model?.main?.appNameImg ?? '', width: 90,),
  147. ],
  148. ),
  149. ],
  150. ),
  151. );
  152. }
  153. /// 按钮
  154. Widget _buttonsWidget(LoginModel model) {
  155. return Container(
  156. padding: const EdgeInsets.symmetric(horizontal: 27.5),
  157. child: Column(
  158. children: model.main.importanceLogin.map((item) {
  159. return Padding(
  160. padding: const EdgeInsets.only(bottom: 8),
  161. child: _customButton(
  162. height: 40,
  163. text: item?.btnText,
  164. iconUrl: item?.btnMobileIcon ?? '',
  165. textColor: item?.btnTextColor,
  166. bgColor: item?.btnBgColor,
  167. borderColor: item?.btnBorderColor,
  168. onTap: () => _loginClick(item?.type)),
  169. );
  170. }).toList(),
  171. ),
  172. );
  173. }
  174. /// 协议
  175. Widget _protocolWidget(LoginModel model) {
  176. return RichText(
  177. text: TextSpan(text: '', children: model.main.agreements.map((item){
  178. return TextSpan(text: item?.text, style: TextStyle(color: HexColor.fromHex(item?.textColor), fontSize: 10),recognizer: TapGestureRecognizer()..onTap = (){
  179. _jumpUserAgreement(item?.url);
  180. });
  181. }).toList()),
  182. );
  183. }
  184. /// 其它登陆方式
  185. Widget _otherLoginWidget(LoginModel model) {
  186. return Expanded(
  187. child: Column(
  188. mainAxisAlignment: MainAxisAlignment.end,
  189. children: <Widget>[
  190. _getOtherLoginTitle(model),
  191. _sizedHeight18,
  192. Visibility(visible: _showOther, child: _getOtherLoginIcons(model)),
  193. Visibility(visible: _showOther, child: _sizedHeight21),
  194. ],
  195. ),
  196. );
  197. }
  198. /// 其它登陆方式的title
  199. Widget _getOtherLoginTitle(LoginModel model) {
  200. return GestureDetector(
  201. behavior: HitTestBehavior.opaque,
  202. onTap: () => _showOrColoseOtherLogin(),
  203. child: Row(
  204. mainAxisAlignment: MainAxisAlignment.center,
  205. crossAxisAlignment: CrossAxisAlignment.center,
  206. children: <Widget>[
  207. Text('${model?.main?.otherIconsTitle}', style: TextStyle(fontSize: 13, color: HexColor.fromHex(model?.main?.otherIconsTitleColor))),
  208. SizedBox(width: 5.5),
  209. RotatedBox(
  210. quarterTurns: _showOther ? 0 : 2,
  211. child: CachedNetworkImage(imageUrl: model?.main?.otherExpansionIcon ?? '', width: 12),
  212. ),
  213. ],
  214. ),
  215. );
  216. }
  217. /// 其它登陆方式的按钮
  218. Widget _getOtherLoginIcons(LoginModel model) {
  219. return Row(
  220. mainAxisAlignment: MainAxisAlignment.center,
  221. children: model.main.bottomIcons.map((item) {
  222. return GestureDetector(
  223. behavior: HitTestBehavior.opaque,
  224. onTap: () => _otherLoginClick(item),
  225. child: Container(
  226. width: 30,
  227. margin: const EdgeInsets.symmetric(horizontal: 20),
  228. child: CachedNetworkImage(imageUrl: item?.img ?? ''),
  229. ),
  230. );
  231. }).toList(),
  232. );
  233. }
  234. /// 自定义按钮
  235. Widget _customButton({double height, String text, String iconUrl, String textColor, String bgColor, String borderColor, GestureTapCallback onTap}) {
  236. return GestureDetector(
  237. onTap: onTap,
  238. child: Container(
  239. width: double.infinity,
  240. height: height ?? 0,
  241. decoration: BoxDecoration(
  242. border: Border.all(color: HexColor.fromHex(borderColor), width: 0.5),
  243. borderRadius: BorderRadius.circular(height ?? 0 / 2),
  244. color: HexColor.fromHex(bgColor),
  245. ),
  246. child: Row(
  247. mainAxisAlignment: MainAxisAlignment.center,
  248. crossAxisAlignment: CrossAxisAlignment.center,
  249. children: <Widget>[
  250. // icon
  251. CachedNetworkImage(imageUrl: iconUrl, width: 12),
  252. SizedBox(width: 8),
  253. // text
  254. Text(text, style: TextStyle(color: HexColor.fromHex(textColor), fontSize: 15))
  255. ],
  256. ),
  257. ),
  258. );
  259. }
  260. }