| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- import 'package:flutter/material.dart';
- import "package:image_picker/image_picker.dart";
- import 'package:flutter_screenutil/flutter_screenutil.dart';
- import 'package:dio/dio.dart';
- import '../styles/colors.dart';
- import '../styles/api.dart';
- import 'dart:io';
- import 'dart:async';
- import 'dart:typed_data';
- import 'dart:convert';
- import 'dart:ui';
- import '../styles/totast.dart';
- class Setting extends StatefulWidget {
- @override
- SettingState createState() => SettingState();
- }
- class SettingState extends State<Setting> {
- File _image;
- Map userInfo = {};
- Future getImage() async {
- var image = await ImagePicker.pickImage(source: ImageSource.gallery);
- image.readAsBytes().then((bytes) async {
- String encoded1 = 'data:image/jpeg;base64,' + base64Encode(bytes);
- print(encoded1);
- FormData formData = new FormData.from({"base64": encoded1});
- Toast.show(context, '加载中', -1, 'loading');
- final response =
- await Dio().post(domain + 'assets/uploadImg', data: formData);
- final res = json.decode(response.toString());
- if (res['success']) {
- updateUserInfo(res['data'], 'icon');
- }
- });
- setState(() {
- _image = image;
- });
- }
- @override
- void initState() {
- // TODO: implement initState
- super.initState();
- getUserInfo(false);
- }
- @override
- Widget build(BuildContext context) {
- ScreenUtil.instance = ScreenUtil(width: 375, height: 667)..init(context);
- // TODO: implement build
- return new WillPopScope(
- child: Scaffold(
- appBar: AppBar(
- backgroundColor: PRIMARY_COLOR,
- title: Text('系统设置'),
- centerTitle: true,
- elevation: 0,
- ),
- body: RefreshIndicator(
- color: PRIMARY_COLOR,
- backgroundColor: BG_SUB_COLOR,
- displacement: 10,
- onRefresh: () async {
- await new Future.delayed(const Duration(seconds: 1));
- getUserInfo(false);
- },
- child: SingleChildScrollView(
- physics: AlwaysScrollableScrollPhysics(),
- child: ConstrainedBox(
- constraints: BoxConstraints(
- minHeight: window.physicalSize.height /
- window.devicePixelRatio -
- 56 -
- MediaQueryData.fromWindow(window).padding.top),
- child: Container(
- color: BG_COLOR,
- child: Column(
- children: <Widget>[
- Container(
- color: BG_SUB_COLOR,
- padding: EdgeInsets.only(top: 10),
- child: Column(
- children: <Widget>[_settingItem('头像')],
- ),
- )
- ],
- ),
- ),
- ))),
- floatingActionButton: FloatingActionButton(
- onPressed: () {
- print('点击照片');
- Toast.show(context, '加载中', -1, 'loading');
- },
- tooltip: 'Pick Image',
- child: Icon(Icons.add_a_photo),
- )),
- onWillPop: () {
- Toast.hide();
- print("返回键点击了");
- Navigator.pop(context);
- });
- }
- Widget _settingItem(name) {
- return Container(
- padding: EdgeInsets.only(left: 15, right: 15),
- height: 60,
- color: BG_COLOR,
- child: GestureDetector(
- child: Row(
- children: <Widget>[
- Text(name,
- style: TextStyle(
- color: Colors.white,
- fontSize: 14,
- fontWeight: FontWeight.w400)),
- Expanded(flex: 1, child: _centerWidget('头像')),
- Image.asset(
- 'images/icon_inter.png',
- width: 24,
- )
- ],
- ),
- onTap: () {
- if (name == '头像') {
- getImage();
- }
- },
- ),
- );
- }
- Widget _centerWidget(name) {
- Widget _wiget;
- print(name);
- if (name == '头像') {
- _wiget = UnconstrainedBox(
- alignment: Alignment.centerRight,
- child: SizedBox(
- width: 36,
- height: 36,
- child: CircleAvatar(
- backgroundImage: NetworkImage(
- userInfo.containsKey('icon') ? userInfo['icon'] : '')),
- ),
- );
- }
- return _wiget;
- }
- void updateUserInfo(value, key) async {
- print(value);
- print(key);
- FormData formdata =
- new FormData.from({key: value, "id": isDebug ? debugID : ''});
- print(formdata);
- final response =
- await Dio().post(domain + 'userInfo/update', data: formdata);
- print(response);
- final res = json.decode(response.toString());
- if (res['success']) {
- Toast.hide();
- getUserInfo(false);
- Toast.show(context, '修改成功', 1500, 'success');
- }
- }
- void getUserInfo(isFreash) async {
- Future.delayed(Duration(seconds: 1000), () {
- if (!userInfo.containsKey('nickname')) {
- Toast.show(context, '加载中', -1, 'loading');
- isFreash = true;
- }
- });
- final response = await Dio().get(domain + 'userInfo/getUserInfo',
- data: {"id": isDebug ? debugID : ''});
- final res = json.decode(response.toString());
- if (isFreash) {
- Toast.hide();
- }
- if (res['success']) {
- print(res['data']);
- setState(() {
- userInfo = res['data'];
- });
- }
- }
- }
|