reback_page.dart 6.53 KB
import 'dart:async';
import 'dart:io';
import 'dart:math';

import 'package:device_info_plus/device_info_plus.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:wow_english/common/core/app_config_helper.dart';
import 'package:wow_english/common/extension/string_extension.dart';
import 'package:wow_english/route/route.dart';
import 'package:wow_english/utils/color_util.dart';

import 'package:wow_english/common/request/dao/user_dao.dart';
import 'package:wow_english/common/widgets/we_app_bar.dart';
import 'package:wow_english/utils/toast_util.dart';

class ReBackPage extends StatefulWidget {
  const ReBackPage({super.key});

  @override
  State<StatefulWidget> createState() {
    return ReBackPageState();
  }
}

class ReBackPageState extends State<ReBackPage> {
  late bool _canEnsure;
  int _textCount = 0;
  String _textInputStr = "";

  @override
  void initState() {
    super.initState();
    _canEnsure = false;
    _textCount = 0;
    _textInputStr = "";
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: const WEAppBar(
          titleText: '我要反馈',
        ),
        body: Container(
          color: Colors.white,
          padding: EdgeInsets.symmetric(horizontal: 10.w),
          child: SafeArea(
            child: LayoutBuilder(builder: (context, constraints) {
              return SingleChildScrollView(
                child: ConstrainedBox(
                  constraints: BoxConstraints(
                    minHeight: constraints.maxHeight,
                  ),
                  child: IntrinsicHeight(
                    child: Column(
                      children: [
                        20.verticalSpace,
                        Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Text(
                              '请输入您要反馈的问题和意见,10-500个字',
                              textAlign: TextAlign.left,
                              style: TextStyle(
                                  fontSize: 19.sp, color: HexColor('#333333')),
                            ),
                            Text(
                              '$_textCount/500',
                              textAlign: TextAlign.right,
                              style: TextStyle(
                                  fontSize: 19.sp, color: HexColor('#333333')),
                            )
                          ],
                        ),
                        9.5.verticalSpace,
                        Expanded(
                          child: Container(
                            decoration: BoxDecoration(
                                image: DecorationImage(
                                    image: AssetImage('bg_reback'.assetPng),
                                    fit: BoxFit.fill)),
                            child: Padding(
                              padding: const EdgeInsets.symmetric(
                                  vertical: 10, horizontal: 16),
                              // 设置对称内边距
                              child: TextField(
                                inputFormatters: [
                                  LengthLimitingTextInputFormatter(500, maxLengthEnforcement: MaxLengthEnforcement.truncateAfterCompositionEnds),
                                ],
                                onChanged: (String value) {
                                  setState(() {
                                    _textInputStr = value;
                                    _textCount = min(value.length, 500);
                                    _canEnsure = value.length >= 10;
                                  });
                                },
                                maxLines: null,
                                textInputAction: TextInputAction.done,
                                decoration: InputDecoration(
                                    border: InputBorder.none,
                                    hintStyle: TextStyle(
                                        fontSize: 16.sp,
                                        color: const Color(0xFF999999))),
                              ),
                            ),
                          ),
                        ),
                        4.5.verticalSpace,
                        GestureDetector(
                          onTap: () {
                            if (_textInputStr.length < 10) {
                              // 输入范围为10-450以内
                              return;
                            }
                            userFeedBack(_textInputStr);
                          },
                          child: Container(
                            decoration: BoxDecoration(
                                image: DecorationImage(
                                    fit: BoxFit.fill,
                                    image: AssetImage(_canEnsure
                                        ? 're_button'.assetPng
                                        : 're_button_dis'.assetPng))),
                            alignment: Alignment.center,
                            width: 91.w,
                            height: 45.h,
                            child: Text(
                              '提交',
                              textAlign: TextAlign.center,
                              style:
                                  TextStyle(color: Colors.white, fontSize: 17.sp),
                            ),
                          ),
                        )
                      ],
                    ),
                  ),
                ),
              );
            }),
          ),
        ));
  }

   Future<void> userFeedBack(content) async {
    String deviceModel = "";
    String osType = AppConfigHelper.isIosPlatform() ? "ios" : "android";
    String osVersion = "";
    if (AppConfigHelper.isIosPlatform()) {
      final iosDeviceInfo = await DeviceInfoPlugin().iosInfo;
      osVersion = iosDeviceInfo.systemVersion;
      deviceModel = iosDeviceInfo.model;
    } else {
      final androidInfo = await DeviceInfoPlugin().androidInfo;
      osVersion = androidInfo.version.release.toString();
      deviceModel = androidInfo.manufacturer;
    }
    EasyLoading.show();
    await UserDao.feedBack(content, deviceModel, osType, osVersion);
    EasyLoading.dismiss();
    showToast('提交成功');
    popPage();
  }
}