home_page.dart 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. import 'package:flutter/cupertino.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:mvvm_flutter/helper/dialog.dart';
  4. import 'package:mvvm_flutter/helper/toast.dart';
  5. import 'package:mvvm_flutter/helper/widget_utils.dart';
  6. import 'package:mvvm_flutter/view/base.dart';
  7. import 'package:mvvm_flutter/viewmodel/home_provide.dart';
  8. import 'package:provider/provider.dart';
  9. /// Page :HomePage
  10. ///
  11. /// 获取其它页面传递来的参数
  12. /// 构造出各个 Provide 对象,放入到 [mProviders]里
  13. class HomePage extends PageProvideNode<HomeProvide> {
  14. /// 提供
  15. ///
  16. /// 获取参数 [title] 并生成一个[HomeProvide]对象
  17. /// 然后放入 [mProviders]中
  18. HomePage(String title) : super([title]);
  19. @override
  20. Widget buildContent(BuildContext context) {
  21. return _HomeContentPage(mProvider);
  22. }
  23. }
  24. /// View : 登录页面
  25. ///
  26. /// 展示UI (ps:如果有UI地址,最好附上相应的链接)
  27. /// 与用户进行交互
  28. class _HomeContentPage extends StatefulWidget {
  29. final HomeProvide provide;
  30. _HomeContentPage(this.provide);
  31. @override
  32. State<StatefulWidget> createState() {
  33. return _HomeContentState();
  34. }
  35. }
  36. class _HomeContentState extends State<_HomeContentPage>
  37. with SingleTickerProviderStateMixin<_HomeContentPage>
  38. implements Presenter {
  39. HomeProvide mProvide;
  40. /// 处理动画
  41. AnimationController _controller;
  42. Animation<double> _animation;
  43. static const ACTION_LOGIN = "login";
  44. final LoadingDialog loadingDialog = LoadingDialog();
  45. @override
  46. void initState() {
  47. super.initState();
  48. mProvide = widget.provide;
  49. _controller = AnimationController(vsync: this, duration: const Duration(milliseconds: 300));
  50. _animation = Tween(begin: 295.0, end: 48.0).animate(_controller)
  51. ..addListener(() {
  52. mProvide.btnWidth = _animation.value;
  53. });
  54. }
  55. @override
  56. void dispose() {
  57. print('-------dispose-------');
  58. _controller.dispose();
  59. super.dispose();
  60. }
  61. @override
  62. void onClick(String action) {
  63. if (action == ACTION_LOGIN) {
  64. login();
  65. }
  66. }
  67. /// 登录
  68. ///
  69. /// 调用 [mProvide] 的 login 方法并进行订阅
  70. /// 请求开始时:启动动画 [AnimationStatus.forward]
  71. /// 请求结束时:反转动画 [AnimationStatus.reverse]
  72. /// 成功 :弹出 'login success'
  73. /// 失败 :[dispatchFailure] 显示错误原因
  74. void login() {
  75. final s = mProvide.login().doOnListen(() {
  76. _controller.forward();
  77. }).doOnDone(() {
  78. _controller.reverse();
  79. }).doOnCancel(() {
  80. print("======cancel======");
  81. }).listen((_) {
  82. //success
  83. Toast.show("login success", context, type: Toast.SUCCESS);
  84. }, onError: (e) {
  85. //error
  86. dispatchFailure(context, e);
  87. });
  88. mProvide.addSubscription(s);
  89. }
  90. @override
  91. Widget build(BuildContext context) {
  92. print("--------build--------");
  93. return Material(
  94. child: Scaffold(
  95. appBar: AppBar(
  96. title: Text(mProvide.title),
  97. ),
  98. body: DefaultTextStyle(
  99. style: TextStyle(color: Colors.black),
  100. child: Column(
  101. children: <Widget>[
  102. TextField(
  103. keyboardType: TextInputType.text,
  104. decoration: InputDecoration(
  105. contentPadding: EdgeInsets.all(10.0),
  106. icon: Icon(Icons.person),
  107. labelText: 'Account',
  108. ),
  109. autofocus: false,
  110. onChanged: (str) => mProvide.username = str,
  111. ),
  112. TextField(
  113. obscureText: true,
  114. keyboardType: TextInputType.text,
  115. decoration: InputDecoration(
  116. contentPadding: EdgeInsets.all(10.0),
  117. icon: Icon(Icons.lock),
  118. labelText: 'Password',
  119. ),
  120. autofocus: false,
  121. onChanged: (str) => mProvide.password = str,
  122. ),
  123. const Padding(
  124. padding: EdgeInsets.only(top: 30.0),
  125. ),
  126. buildLoginBtnProvide(),
  127. const Text(
  128. "Response:",
  129. style: TextStyle(fontSize: 18),
  130. textAlign: TextAlign.start,
  131. ),
  132. Expanded(
  133. child: Container(
  134. constraints: BoxConstraints(minWidth: double.infinity),
  135. margin: EdgeInsets.fromLTRB(12, 12, 12, 0),
  136. padding: EdgeInsets.all(5.0),
  137. decoration: BoxDecoration(border: Border.all(color: Colors.blue)),
  138. child: Consumer<HomeProvide>(
  139. builder: (BuildContext context, HomeProvide value, Widget child) => Text(value.response),
  140. ),
  141. ),
  142. )
  143. ],
  144. ),
  145. ),
  146. ),
  147. );
  148. }
  149. /// 登录按钮
  150. ///
  151. /// 按钮宽度根据是否进行请求由[_controller]控制
  152. /// 当 [mProvide.loading] 为true 时 ,点击事件不生效
  153. Consumer<HomeProvide> buildLoginBtnProvide() {
  154. return Consumer<HomeProvide>(
  155. builder: (BuildContext context, HomeProvide value, Widget child) {
  156. return CupertinoButton(
  157. onPressed: value.loading ? null : () => onClick(ACTION_LOGIN),
  158. pressedOpacity: 0.8,
  159. child: Container(
  160. alignment: Alignment.center,
  161. width: value.btnWidth,
  162. height: 48,
  163. decoration: BoxDecoration(
  164. borderRadius: BorderRadius.all(Radius.circular(30.0)),
  165. gradient: LinearGradient(colors: [
  166. Color(0xFF686CF2),
  167. Color(0xFF0E5CFF),
  168. ]),
  169. boxShadow: [BoxShadow(color: Color(0x4D5E56FF), offset: Offset(0.0, 4.0), blurRadius: 13.0)]),
  170. child: buildLoginChild(value),
  171. ),
  172. );
  173. },
  174. );
  175. }
  176. /// 登录按钮内部的 child
  177. ///
  178. /// 当请求进行时 [value.loading] 为 true 时,显示 [CircularProgressIndicator]
  179. /// 否则显示普通文本
  180. Widget buildLoginChild(HomeProvide value) {
  181. if (value.loading) {
  182. return const CircularProgressIndicator();
  183. } else {
  184. return FittedBox(
  185. fit: BoxFit.scaleDown,
  186. child: Consumer<HomeProvide>(
  187. builder: (BuildContext context, HomeProvide value, Widget child) => Text(
  188. 'Login With Github Account',
  189. maxLines: 1,
  190. textAlign: TextAlign.center,
  191. overflow: TextOverflow.fade,
  192. style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 16.0, color: Colors.white),
  193. ),
  194. ),
  195. );
  196. }
  197. }
  198. }