home_page.dart 6.7 KB

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