home_page.dart 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. import 'package:flutter/cupertino.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:mvvm_flutter/helper/toast.dart';
  4. import 'package:mvvm_flutter/helper/widget_utils.dart';
  5. import 'package:mvvm_flutter/view/base.dart';
  6. import 'package:mvvm_flutter/viewmodel/home_provide.dart';
  7. import 'package:provider/provider.dart';
  8. /// Page :HomePage
  9. ///
  10. /// 获取其它页面传递来的参数
  11. class HomePage extends PageProvideNode<HomeProvide> {
  12. /// 提供
  13. ///
  14. /// 获取参数 [title] 并生成一个[HomeProvide]对象
  15. HomePage(String title) : super(params: [title]);
  16. @override
  17. Widget buildContent(BuildContext context) {
  18. return _HomeContentPage(mProvider);
  19. }
  20. }
  21. /// View : 登录页面
  22. ///
  23. /// 展示UI (ps:如果有UI地址,最好附上相应的链接)
  24. /// 与用户进行交互
  25. class _HomeContentPage extends StatefulWidget {
  26. final HomeProvide provide;
  27. _HomeContentPage(this.provide);
  28. @override
  29. State<StatefulWidget> createState() {
  30. return _HomeContentState();
  31. }
  32. }
  33. class _HomeContentState extends State<_HomeContentPage>
  34. with TickerProviderStateMixin<_HomeContentPage>
  35. implements Presenter {
  36. HomeProvide mProvide;
  37. /// 处理动画
  38. AnimationController _controller;
  39. Animation<double> _animation;
  40. static const ACTION_LOGIN = "login";
  41. @override
  42. void initState() {
  43. super.initState();
  44. mProvide = widget.provide;
  45. _controller = AnimationController(vsync: this, duration: const Duration(milliseconds: 300));
  46. _animation = Tween(begin: 295.0, end: 48.0).animate(_controller)
  47. ..addListener(() {
  48. mProvide.btnWidth = _animation.value;
  49. });
  50. }
  51. @override
  52. void dispose() {
  53. print('-------dispose-------');
  54. _controller.dispose();
  55. super.dispose();
  56. }
  57. @override
  58. void onClick(String action) {
  59. print("onClick:" + action);
  60. if (ACTION_LOGIN == action) {
  61. login();
  62. }
  63. }
  64. /// 登录
  65. ///
  66. /// 调用 [mProvide] 的 login 方法并进行订阅
  67. /// 请求开始时:启动动画 [AnimationStatus.forward]
  68. /// 请求结束时:反转动画 [AnimationStatus.reverse]
  69. /// 成功 :弹出 'login success'
  70. /// 失败 :[dispatchFailure] 显示错误原因
  71. void login() {
  72. final s = mProvide.login().doOnListen(() {
  73. _controller.forward();
  74. }).doOnDone(() {
  75. _controller.reverse();
  76. }).listen((_) {
  77. //success
  78. Toast.show("login success", context, type: Toast.SUCCESS);
  79. }, onError: (e) {
  80. //error
  81. dispatchFailure(context, e);
  82. });
  83. mProvide.addSubscription(s);
  84. }
  85. @override
  86. Widget build(BuildContext context) {
  87. print("--------build--------");
  88. return Material(
  89. child: Scaffold(
  90. appBar: AppBar(
  91. title: Text(mProvide.title),
  92. ),
  93. body: DefaultTextStyle(
  94. style: TextStyle(color: Colors.black),
  95. child: Column(
  96. children: <Widget>[
  97. TextField(
  98. keyboardType: TextInputType.text,
  99. decoration: InputDecoration(
  100. contentPadding: EdgeInsets.all(10.0),
  101. icon: Icon(Icons.person),
  102. labelText: 'Account',
  103. ),
  104. autofocus: false,
  105. onChanged: (str) => mProvide.username = str,
  106. ),
  107. TextField(
  108. obscureText: true,
  109. keyboardType: TextInputType.text,
  110. decoration: InputDecoration(
  111. contentPadding: EdgeInsets.all(10.0),
  112. icon: Icon(Icons.lock),
  113. labelText: 'Password',
  114. ),
  115. autofocus: false,
  116. onChanged: (str) => mProvide.password = str,
  117. ),
  118. const Padding(
  119. padding: EdgeInsets.only(top: 30.0),
  120. ),
  121. buildLoginBtnProvide(),
  122. const Text(
  123. "Response:",
  124. style: TextStyle(fontSize: 18),
  125. textAlign: TextAlign.start,
  126. ),
  127. Expanded(
  128. child: Container(
  129. constraints: BoxConstraints(minWidth: double.infinity),
  130. margin: EdgeInsets.fromLTRB(12, 12, 12, 0),
  131. padding: EdgeInsets.all(5.0),
  132. decoration: BoxDecoration(border: Border.all(color: Colors.blue)),
  133. child: Selector<HomeProvide, String>(
  134. selector: (_, data) => data.response,
  135. builder: (context, value, child) {
  136. // 使用Selector,当provide.notifyListeners()时,只有data.response改变的时候才会build
  137. return Text(value);
  138. },
  139. ),
  140. ),
  141. )
  142. ],
  143. ),
  144. ),
  145. ),
  146. );
  147. }
  148. /// 登录按钮
  149. ///
  150. /// 按钮宽度根据是否进行请求由[_controller]控制
  151. /// 当 [mProvide.loading] 为true 时 ,点击事件不生效
  152. Consumer<HomeProvide> buildLoginBtnProvide() {
  153. return Consumer<HomeProvide>(
  154. builder: (context, value, child) {
  155. // 使用 Consumer ,当provide.notifyListeners()时都会rebuild
  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: Text(
  187. 'Login With Github Account',
  188. maxLines: 1,
  189. textAlign: TextAlign.center,
  190. overflow: TextOverflow.fade,
  191. style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 16.0, color: Colors.white),
  192. ),
  193. );
  194. }
  195. }
  196. }