home_page.dart 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. import 'package:flutter/cupertino.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:mvvm_flutter/di/dartin.dart';
  4. import 'package:mvvm_flutter/helper/dialog.dart';
  5. import 'package:mvvm_flutter/helper/toast.dart';
  6. import 'package:mvvm_flutter/helper/widgetutils.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. class HomePage extends PageProvideNode {
  11. final String title;
  12. HomePage(this.title) {
  13. mProviders.provideValue(inject<HomeProvide>(params: [title]));
  14. }
  15. @override
  16. Widget buildContent(BuildContext context) {
  17. return _HomeContentPage();
  18. }
  19. }
  20. class _HomeContentPage extends StatefulWidget {
  21. @override
  22. State<StatefulWidget> createState() {
  23. return _HomeContentState();
  24. }
  25. }
  26. /**
  27. * View
  28. */
  29. class _HomeContentState extends State<_HomeContentPage> with SingleTickerProviderStateMixin<_HomeContentPage> implements Presenter {
  30. HomeProvide _viewModel;
  31. AnimationController _controller;
  32. Animation<double> _animation;
  33. final _ACTION_LOGIN = "login";
  34. final LoadingDialog loadingDialog = LoadingDialog();
  35. @override
  36. void initState() {
  37. super.initState();
  38. _controller = AnimationController(vsync: this, duration: const Duration(milliseconds: 300));
  39. _animation = Tween(begin: 295.0, end: 48.0).animate(_controller)
  40. ..addListener(() {
  41. _viewModel.btnWidth = _animation.value;
  42. });
  43. }
  44. @override
  45. void dispose() {
  46. print('-------dispose-------');
  47. _controller.dispose();
  48. _viewModel.disposeBag();
  49. super.dispose();
  50. }
  51. @override
  52. void onClick(String action) {
  53. if (action == _ACTION_LOGIN) {
  54. _login();
  55. }
  56. }
  57. _login() {
  58. final s = _viewModel.login().doOnListen(() {
  59. _controller.forward();
  60. }).doOnDone(() {
  61. _controller.reverse();
  62. }).doOnCancel(() {
  63. print("======cancel======");
  64. }).listen((_) {
  65. //success
  66. Toast.show("login success", context, type: Toast.SUCCESS);
  67. }, onError: (e) {
  68. //error
  69. dispatchFailure(context, e);
  70. });
  71. _viewModel.plus(s);
  72. }
  73. @override
  74. Widget build(BuildContext context) {
  75. _viewModel = Provide.value<HomeProvide>(context);
  76. print("--------build--------");
  77. return Scaffold(
  78. appBar: AppBar(
  79. title: Text(_viewModel.title),
  80. ),
  81. body: DefaultTextStyle(
  82. style: TextStyle(),
  83. child: Material(
  84. child: Column(
  85. children: <Widget>[
  86. TextField(
  87. keyboardType: TextInputType.text,
  88. decoration: InputDecoration(
  89. contentPadding: EdgeInsets.all(10.0),
  90. icon: Icon(Icons.person),
  91. labelText: '账号',
  92. ),
  93. autofocus: false,
  94. onChanged: (str) => _viewModel.username = str,
  95. ),
  96. TextField(
  97. obscureText: true,
  98. keyboardType: TextInputType.number,
  99. decoration: InputDecoration(
  100. contentPadding: EdgeInsets.all(10.0),
  101. icon: Icon(Icons.lock),
  102. labelText: '密码',
  103. ),
  104. autofocus: false,
  105. onChanged: (str) => _viewModel.password = str,
  106. ),
  107. const Padding(
  108. padding: EdgeInsets.only(top: 30.0),
  109. ),
  110. Provide<HomeProvide>(
  111. builder: (BuildContext context, Widget child, HomeProvide value) => CupertinoButton(
  112. onPressed: value.loading ? null : () => onClick(_ACTION_LOGIN),
  113. pressedOpacity: 0.8,
  114. child: Container(
  115. alignment: Alignment.center,
  116. width: value.btnWidth,
  117. height: 48,
  118. decoration: BoxDecoration(
  119. borderRadius: BorderRadius.all(Radius.circular(30.0)),
  120. gradient: LinearGradient(colors: [
  121. Color(0xFF686CF2),
  122. Color(0xFF0E5CFF),
  123. ]),
  124. boxShadow: [BoxShadow(color: Color(0x4D5E56FF), offset: Offset(0.0, 4.0), blurRadius: 13.0)]),
  125. child: _buildChild(value),
  126. ),
  127. ),
  128. ),
  129. const Text(
  130. "Response:",
  131. style: TextStyle(fontSize: 18),
  132. textAlign: TextAlign.start,
  133. ),
  134. Expanded(
  135. child: Container(
  136. constraints: BoxConstraints(minWidth: double.infinity),
  137. margin: EdgeInsets.fromLTRB(12, 12, 12, 0),
  138. padding: EdgeInsets.all(5.0),
  139. decoration: BoxDecoration(border: Border.all(color: Colors.blue)),
  140. child: Provide<HomeProvide>(
  141. builder: (BuildContext context, Widget child, HomeProvide value) => Text(value.response),
  142. ),
  143. ),
  144. )
  145. ],
  146. ),
  147. ),
  148. ),
  149. );
  150. }
  151. Widget _buildChild(HomeProvide value) {
  152. if (value.loading) {
  153. return const CircularProgressIndicator();
  154. } else {
  155. return const FittedBox(
  156. fit: BoxFit.scaleDown,
  157. child: const Text(
  158. '使用GitHub账号登录',
  159. maxLines: 1,
  160. textAlign: TextAlign.center,
  161. overflow: TextOverflow.fade,
  162. style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 16.0, color: Colors.white),
  163. ),
  164. );
  165. }
  166. }
  167. }