setting.dart 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. import 'package:flutter/services.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:fluttertoast/fluttertoast.dart';
  4. import 'package:provider/provider.dart';
  5. import 'package:twongCustomer/store/models/serverModel.dart';
  6. import 'package:twongCustomer/utils/http.dart';
  7. import '../utils/cache.dart';
  8. import '../models/index.dart';
  9. import '../routes/utils.dart';
  10. import '../utils/socket.dart';
  11. import '../widgets/dialog.dart';
  12. class SettingPage extends StatefulWidget {
  13. @override
  14. State<StatefulWidget> createState() => SettingPageState();
  15. }
  16. class SettingPageState extends State<SettingPage> with WidgetsBindingObserver {
  17. UserInfo info;
  18. String serverType;
  19. bool _isExpanded = false;
  20. TextEditingController _inputController = TextEditingController();
  21. @override
  22. void initState() {
  23. super.initState();
  24. WidgetsBinding.instance.addObserver(this);
  25. _isExpanded = info == null;
  26. var url = Cache.get("server");
  27. info = AppData.get("info");
  28. var type = Cache.get("server_type");
  29. serverType = type == null ? "http://" : type;
  30. _inputController.text = url == null ? "" : url;
  31. }
  32. @override
  33. void didChangeMetrics() {
  34. super.didChangeMetrics();
  35. WidgetsBinding.instance.addPostFrameCallback((_) {
  36. setState(() {
  37. if(MediaQuery.of(context).viewInsets.bottom==0){
  38. //关闭键盘
  39. print("show");
  40. }else{
  41. //显示键盘
  42. print("hide");
  43. }
  44. });
  45. });
  46. }
  47. @override
  48. Widget build(BuildContext context) {
  49. return Scaffold(
  50. appBar: AppBar(
  51. title: Text('设置'),
  52. ),
  53. body: info == null ? _buildHomeSetting() : _buildMainSetting(),
  54. );
  55. }
  56. Widget _buildHomeSetting () {
  57. return ListView(
  58. physics: new NeverScrollableScrollPhysics(),
  59. children: [
  60. _buildServerEditor(),
  61. new Divider(),
  62. ListTile(
  63. title: Text('退出'),
  64. leading: Icon(Icons.exit_to_app, color: Colors.red),
  65. onTap: this._exit,
  66. ),
  67. new Divider(),
  68. ]
  69. );
  70. }
  71. Widget _buildMainSetting() {
  72. return ListView(
  73. physics: new NeverScrollableScrollPhysics(),
  74. children: <Widget>[
  75. new Divider(),
  76. ListTile(
  77. title: Text('当前用户:\t\t' + info.name),
  78. leading: Icon(Icons.account_circle, color: Colors.deepOrange),
  79. ),
  80. // 分割线
  81. new Divider(),
  82. ListTile(
  83. title: Text('退出登陆'),
  84. leading: Icon(Icons.logout, color: Colors.red),
  85. onTap: this._logout,
  86. ),
  87. new Divider(),
  88. ],
  89. );
  90. }
  91. Widget _buildServerEditor() {
  92. return SingleChildScrollView(
  93. child: ExpansionPanelList(
  94. children: <ExpansionPanel>[
  95. ExpansionPanel(
  96. headerBuilder: (context, isExpanded) {
  97. return ListTile(
  98. title: Text('服务器设置'),
  99. leading:
  100. Icon(Icons.language, color: Colors.lightBlue),
  101. );
  102. },
  103. body: Padding(
  104. padding: EdgeInsets.fromLTRB(15, 0, 15, 15),
  105. child: ListBody(
  106. children: <Widget>[
  107. Flex(
  108. direction: Axis.horizontal,
  109. children: [
  110. DropdownButton(
  111. items: [
  112. DropdownMenuItem(child: Text("HTTP"), value: "http://",),
  113. DropdownMenuItem(child: Text("HTTPS"), value: "https://",)
  114. ],
  115. hint: Text(serverType == "http://" ? "HTTP" : "HTTPS"),
  116. onChanged: (item) {
  117. setState(() {
  118. serverType = item;
  119. });
  120. },
  121. isExpanded: false,
  122. ),
  123. Expanded(child: Container(
  124. padding: EdgeInsets.only(left: 10,),
  125. margin: EdgeInsets.only(left: 10, bottom: 14),
  126. decoration: BoxDecoration(
  127. border: Border(bottom: BorderSide(
  128. width: 1.0,
  129. style: BorderStyle.solid,
  130. color: Color.fromRGBO(0, 0, 0, 0.1)))),
  131. child: TextField(
  132. controller: _inputController,
  133. decoration: InputDecoration(
  134. hintText: '请输入域名或IP地址',
  135. border: InputBorder.none,
  136. ),
  137. ),
  138. ))
  139. ]
  140. ),
  141. RaisedButton(
  142. color: Colors.blue,
  143. textColor: Colors.white,
  144. child: Text('保存设置'),
  145. onPressed: () => _saveServer(),
  146. ),
  147. ],
  148. ),
  149. ),
  150. isExpanded: _isExpanded,
  151. canTapOnHeader: true,
  152. ),
  153. ],
  154. expansionCallback: (panelIndex, isExpanded) {
  155. setState(() {
  156. _isExpanded = !isExpanded;
  157. });
  158. },
  159. animationDuration: kThemeAnimationDuration,
  160. ),
  161. );
  162. }
  163. void _saveServer () {
  164. var url = _inputController.text;
  165. print("set server: " + serverType + url);
  166. Cache.set("server", url);
  167. Cache.set("server_type", serverType);
  168. // Provider.of<ServerModel>(context,listen: false).update(serverType, url);
  169. // Http.updateConf(type: serverType, host: url);
  170. Fluttertoast.showToast(msg: "保存成功!");
  171. Navigator.pop(context);
  172. }
  173. void _exit() async {
  174. showDialog(
  175. context: context,
  176. builder: (context) {
  177. return CustomDialog(
  178. content: '确认要退出天旺客服么?',
  179. callback: (res) {
  180. SystemChannels.platform.invokeMethod('SystemNavigator.pop');
  181. }
  182. );
  183. }
  184. );
  185. }
  186. void _logout () {
  187. showDialog(
  188. context: context,
  189. builder: (context) {
  190. return CustomDialog(
  191. content: '确认要退出当前登陆的账户么?',
  192. callback: (res) {
  193. Cache.del("token");
  194. Cache.del("autoLogin");
  195. AppData.set("info", null);
  196. Socket.close();
  197. RouterUtils.toLogin(context);
  198. }
  199. );
  200. }
  201. );
  202. }
  203. }