帮我把下面的代码按照data目录ui目录service目录model目录将代码分开并且给出对应的文件名和文件的代码import dartasync;import packagediodiodart;import packagefluttermaterialdart;import packagemuse_nepu_courseglobaldart;import packagepretty_qr_co
data目录:global.dart
import 'package:shared_preferences/shared_preferences.dart';
class Global {
static String password = '';
static String qrcode = '无';
static Color home_currentcolor = Colors.blue;
static SharedPreferences prefs;
//初始化
static Future init() async {
prefs = await SharedPreferences.getInstance();
//读取本地存储的账号密码
password = prefs.getString('password') ?? '';
}
//保存账号密码
static void saveaccount() {
prefs.setString('password', password);
}
//获取二维码
Future getqr() async {
//使用dio请求数据
Dio dio = new Dio();
Response response = await dio.get(
'http://jwkq.xupt.edu.cn:8080/User/GetUserImage?code=' + password);
String data = response.data;
//判断是否请求成功
if (data.contains('http://jwkq.xupt.edu.cn:8080/')) {
qrcode = data;
} else {
qrcode = '获取失败';
}
}
//获取二维码链接
static String qrcodegetter() {
return qrcode;
}
}
ui目录:qr_code.dart
import 'package:flutter/material.dart';
import 'package:pretty_qr_code/pretty_qr_code.dart';
import 'package:slide_countdown/slide_countdown.dart';
import '../global.dart';
class QRCode extends StatefulWidget {
@override
_QRCodeState createState() => _QRCodeState();
}
class _QRCodeState extends State<QRCode> {
final streamDuration = StreamDuration(Duration(seconds: 50));
void initState() {
super.initState();
Global().getqr();
}
String passtemp = "";
void refresh() {
setState(() {});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
backgroundColor: Global.home_currentcolor,
//设置
actions: <Widget>[
IconButton(
icon: Icon(Icons.settings),
onPressed: () {
//弹窗一个文本框加上一个确认框
showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: Text("修改你的一卡通密码\n如果没有错误请不要修改"),
content: TextField(
onChanged: (value) {
passtemp = value;
},
),
actions: <Widget>[
TextButton(
child: Text("确认"),
onPressed: () {
Global.password = passtemp;
Global.saveaccount();
Navigator.of(context).pop();
},
)
],
);
});
},
),
],
),
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('点击二维码可手动刷新\n当前是' + Global.qrcode),
Align(
alignment: Alignment.topCenter,
child: SlideCountdown(
decoration: BoxDecoration(
color: Global.home_currentcolor,
borderRadius: BorderRadius.circular(10)),
durationTitle: DurationTitle(
days: "天", hours: "时", minutes: "分", seconds: "秒"),
// This duration no effect if you customize stream duration
streamDuration: streamDuration,
onChanged: (Duration remaining) {
print("剩余" + remaining.inSeconds.toString()); //这里可以获取到剩余时间
//如果剩余时间等于1,恢复倒计时为50s
if (remaining.inSeconds <= 1) {
streamDuration.change(Duration(seconds: 51));
Global().getqr().then((value) {
setState(() {});
});
}
},
),
),
Expanded(
child: GestureDetector(
onTap: () {
print('被点击了');
Global().getqr().then((value) {
streamDuration.change(Duration(seconds: 51));
setState(() {});
});
},
child: PrettyQr(
typeNumber: 3,
size: 200,
data: Global.qrcodegetter(),
errorCorrectLevel: QrErrorCorrectLevel.M,
roundEdges: true,
),
),
),
],
),
),
);
}
}
service目录:qr_code_service.dart
import 'package:dio/dio.dart';
import '../global.dart';
class QRCodeService {
//获取二维码
Future getQRCode() async {
//使用dio请求数据
Dio dio = new Dio();
Response response = await dio.get(
'http://jwkq.xupt.edu.cn:8080/User/GetUserImage?code=' + Global.password);
String data = response.data;
//判断是否请求成功
if (data.contains('http://jwkq.xupt.edu.cn:8080/')) {
Global.qrcode = data;
} else {
Global.qrcode = '获取失败';
}
}
}
model目录:user.dart
class User {
String password;
String qrcode;
User({this.password, this.qrcode});
User.fromJson(Map<String, dynamic> json) {
password = json['password'];
qrcode = json['qrcode'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['password'] = this.password;
data['qrcode'] = this.qrcode;
return data;
}
}
``
原文地址: https://www.cveoy.top/t/topic/fm0b 著作权归作者所有。请勿转载和采集!