| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393 |
- import 'package:flutter/material.dart';
- import 'package:file_picker/file_picker.dart';
- import 'package:flutter/cupertino.dart';
- import 'package:flutter_screenutil/flutter_screenutil.dart';
- import 'package:dio/dio.dart';
- import 'package:intl/intl.dart';
- import 'package:package_info/package_info.dart';
- import '../styles/colors.dart';
- import 'dart:io';
- import 'dart:async';
- import 'dart:convert';
- import 'dart:ui';
- import '../styles/totast.dart';
- import 'ChangeUserInfo.dart'; //修改信息页面
- import 'loginFirst.dart'; //登录
- import 'package:redux/redux.dart';
- import 'package:flutter_redux/flutter_redux.dart';
- import '../redux/AppState.dart';
- import '../model/UserInfo.dart';
- import '../net/HttpManager.dart';
- import '../net/Result.dart';
- import '../redux/UserRedux.dart';
- class Setting extends StatefulWidget {
- @override
- SettingState createState() => SettingState();
- }
- class SettingState extends State<Setting> {
- String version = "";
- bool receiveMsg = false;
- void getFilePath() async {
- String filePath = await FilePicker.getFilePath(type: FileType.IMAGE);
- if (filePath == null) {
- return;
- }
- File _file = File(filePath);
- Toast.show(context, '加载中', -1, 'loading');
- Result res = await HttpManager.post("assets/uploadFile", data: {
- "file": UploadFileInfo(_file, filePath),
- });
- Toast.hide();
- if (res.success) {
- updateUserInfo(StoreProvider.of<AppState>(context).state.userInfo, res.data[0], 'icon');
- } else {}
- }
- @override
- void initState() {
- super.initState();
- PackageInfo.fromPlatform().then((packageInfo) {
- setState(() {
- version = packageInfo.version;
- });
- });
- }
- @override
- Widget build(BuildContext context) {
- ScreenUtil.instance = ScreenUtil(width: 375, height: 667)..init(context);
- return StoreConnector<AppState, UserInfo>(
- converter: (Store store) => store.state.userInfo,
- builder: (context, userInfo) {
- return new WillPopScope(
- key: Key("SettingsPage"),
- child: Scaffold(
- backgroundColor: PAGE_BACKGROUND_COLOR,
- appBar: AppBar(
- backgroundColor: PRIMARY_COLOR,
- title: Text('系统设置'),
- centerTitle: true,
- elevation: 0,
- ),
- body: Stack(
- children: <Widget>[
- RefreshIndicator(
- color: PRIMARY_COLOR,
- backgroundColor: BG_SUB_COLOR,
- displacement: 10,
- onRefresh: () async {
- await new Future.delayed(const Duration(seconds: 1));
- getUserInfo();
- },
- child: SizedBox.expand(
- child: SingleChildScrollView(
- physics: AlwaysScrollableScrollPhysics(),
- child: Column(
- children: <Widget>[
- _sectionDivier(),
- _section([
- _cell(
- '头像',
- ClipOval(
- child: Image.network(userInfo.icon, width: 36, height: 36),
- ), onTap: () {
- getFilePath();
- }),
- _cell('昵称', userInfo.nickname, placeholder: '请填写昵称', onTap: () {
- Navigator.push(
- context, new CupertinoPageRoute(builder: (context) => new ChangeUserInfo(title: '昵称', val: userInfo.nickname)));
- }),
- _cell('性别', userInfo.sex, placeholder: '请添加性别信息', onTap: () {
- _chooseSex(context);
- }),
- _cell(
- '生日',
- DateFormat('yyyy-MM-dd').format(
- DateTime.fromMillisecondsSinceEpoch(userInfo.birthday),
- ),
- placeholder: '选择生日',
- onTap: () {
- _chooseBirthday(context);
- },
- ),
- _cell('手机', userInfo.phone, showBorder: false, onTap: () {
- Navigator.push(context, new CupertinoPageRoute(builder: (context) => new ChangeUserInfo(title: '手机号', val: userInfo.phone)));
- })
- ]),
- _sectionDivier(),
- _section([
- _cell(
- '是否接收消息',
- Switch(
- value: userInfo.noticeFlag,
- onChanged: (val) {
- updateUserInfo(userInfo, val ? 'Y' : 'N', 'noticeFlag');
- }),
- showBorder: false)
- ]),
- _sectionDivier(),
- _section([
- _cell('检查更新', ' '),
- _cell('版本号', version, showBorder: false),
- ]),
- ],
- ),
- ),
- ),
- ),
- Positioned(
- bottom: 10,
- right: 15,
- left: 15,
- height: 48,
- child: FlatButton(
- textColor: Colors.white,
- color: PRIMARY_COLOR,
- highlightColor: Color(0xFF763434),
- child: Text(
- "退出登录",
- style: TextStyle(fontSize: 16, fontWeight: FontWeight.w700),
- ),
- onPressed: () {
- Future.delayed(const Duration(seconds: 1), () {
- StoreProvider.of<AppState>(context).dispatch(UpdateUserAction(null));
- });
- Toast.show(context, '退出成功', 1500, 'success');
- Navigator.push(context, new MaterialPageRoute(builder: (context) => new LoginFirst()));
- }),
- )
- ],
- ),
- ),
- onWillPop: () {
- Toast.hide();
- Navigator.pop(context);
- return Future.value(false);
- });
- });
- }
- void updateUserInfo(userInfo, value, key) async {
- print(userInfo);
- print(value);
- print(key);
- if (Toast.preToast == null) {
- Toast.show(context, '加载中', -1, 'loading');
- }
- final Result res = await HttpManager.post('userInfo/update', data: {"id": userInfo.id, key: value});
- Toast.hide();
- if (res.success) {
- Toast.show(context, '修改成功', 1500, 'success');
- getUserInfo();
- } else {
- Toast.show(context, res.error, 1500, 'info');
- }
- }
- void getUserInfo() async {
- Result res = await HttpManager.get("userInfo/getUserInfo");
- if (res.success) {
- StoreProvider.of<AppState>(context).dispatch(UpdateUserAction(UserInfo.fromJson(res.data)));
- } else {}
- }
- _chooseSex(BuildContext context) async {
- String sex = await showCupertinoModalPopup(
- context: context,
- builder: (BuildContext context) {
- return CupertinoActionSheet(
- title: Text('选择性别'),
- actions: <Widget>[
- CupertinoActionSheetAction(
- child: Text('男'),
- onPressed: () {
- Navigator.pop(context, '男');
- },
- ),
- CupertinoActionSheetAction(
- child: Text('女'),
- onPressed: () {
- Navigator.pop(context, '女');
- },
- ),
- CupertinoDialogAction(
- child: const Text('取消'),
- isDestructiveAction: true,
- onPressed: () {
- Navigator.pop(context);
- },
- )
- ],
- );
- });
- if (sex != null) {
- updateUserInfo(StoreProvider.of<AppState>(context).state.userInfo, sex, 'sex');
- }
- }
- _chooseBirthday(BuildContext context) async {
- UserInfo userInfo = StoreProvider.of<AppState>(context).state.userInfo;
- DateTime date = userInfo.birthday > 0 ? DateTime.fromMillisecondsSinceEpoch(userInfo.birthday) : DateTime.now();
- DateTime res = await showCupertinoModalPopup<DateTime>(
- context: context,
- builder: (BuildContext context) {
- return Container(
- height: 216,
- color: CupertinoColors.white,
- child: Column(
- children: <Widget>[
- SizedBox(
- height: 44,
- child: Stack(
- children: <Widget>[
- Positioned(
- left: 0,
- top: 0,
- bottom: 0,
- child: CupertinoButton(
- child: Text(
- '取消',
- style: TextStyle(color: PRIMARY_COLOR),
- ),
- onPressed: () {
- Navigator.of(context).pop();
- },
- ),
- ),
- Positioned(
- right: 0,
- top: 0,
- bottom: 0,
- child: CupertinoButton(
- child: Text(
- '确定',
- style: TextStyle(color: PRIMARY_COLOR),
- ),
- onPressed: () {
- Navigator.of(context).pop(date);
- },
- ),
- )
- ],
- ),
- ),
- Expanded(
- flex: 1,
- child: DefaultTextStyle(
- style: const TextStyle(
- color: CupertinoColors.black,
- fontSize: 22.0,
- ),
- child: GestureDetector(
- // Blocks taps from propagating to the modal sheet and popping.
- onTap: () {},
- child: SafeArea(
- top: false,
- child: CupertinoDatePicker(
- mode: CupertinoDatePickerMode.date,
- initialDateTime: date,
- onDateTimeChanged: (DateTime newDateTime) {
- date = newDateTime;
- },
- ),
- ),
- ),
- ),
- )
- ],
- ),
- );
- },
- );
- if (res != null) {
- print(res);
- updateUserInfo(userInfo, res.millisecondsSinceEpoch, 'birthday');
- }
- }
- _chooseImage(BuildContext context) async {
- UserInfo userInfo = StoreProvider.of<AppState>(context).state.userInfo;
- var image = await FilePicker.getFilePath(type: FileType.IMAGE);
- Toast.show(context, '加载中', -1, 'loading');
- File _image = File(image);
- _image.readAsBytes().then((bytes) async {
- String encoded1 = 'data:image/jpeg;base64,' + base64Encode(bytes);
- FormData formData = new FormData.from({"base64": encoded1});
- Result res = await HttpManager.post('assets/uploadImg', data: formData);
- if (res.success) {
- updateUserInfo(userInfo, res.data, 'icon');
- }
- });
- }
- Widget _sectionDivier() {
- return SizedBox(height: 10);
- }
- Widget _section(List<Widget> children) {
- return Container(
- color: CELL_COLOR,
- child: Column(
- children: children,
- ),
- );
- }
- Widget _cell(String title, dynamic child, {String placeholder, void Function() onTap, bool showBorder = true}) {
- Widget secondChild;
- if (child == null || (child is String && child.length == 0)) {
- secondChild = Text(
- placeholder ?? '',
- style: TextStyle(color: PLACEHOLDER_COLOR, fontSize: 13),
- );
- } else if (child is String) {
- secondChild = Text(
- child ?? '',
- style: TextStyle(color: Colors.white, fontSize: 15),
- );
- } else {
- secondChild = child;
- }
- return Container(
- height: 60,
- padding: EdgeInsets.fromLTRB(15, 0, 15, 0),
- child: GestureDetector(
- onTap: onTap,
- child: Container(
- decoration: BoxDecoration(
- border: Border(
- bottom: BorderSide(
- color: Color(0x2E000000),
- width: showBorder ? 1 : 0,
- ),
- ),
- ),
- child: Row(
- mainAxisAlignment: MainAxisAlignment.center,
- children: <Widget>[
- Expanded(
- child: Text(
- title ?? '',
- style: TextStyle(color: Colors.white, fontSize: 14),
- ),
- ),
- secondChild,
- Padding(
- padding: EdgeInsets.only(left: 2),
- child: Image.asset(
- "images/icon_inter.png",
- width: 24,
- height: 24,
- ),
- )
- ],
- ),
- ),
- ),
- );
- }
- }
|