Skip to content

Commit

Permalink
feat: multiple optimization refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
bymoye committed Dec 19, 2024
1 parent 368dd75 commit bcfc662
Show file tree
Hide file tree
Showing 24 changed files with 738 additions and 1,125 deletions.
9 changes: 6 additions & 3 deletions lib/agent/abstract.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ class HttpAgent {
final String baseUrl;
final FetchClient client = FetchClient(mode: RequestMode.cors);

HttpAgent(this.baseUrl);
HttpAgent(this.baseUrl) {
_uri = Uri.parse(baseUrl);
}

late final Uri _uri;

Uri buildUri(String path, Map<String, String>? queryParameters) {
return Uri.parse(baseUrl)
.replace(path: path, queryParameters: queryParameters);
return _uri.replace(path: path, queryParameters: queryParameters);
}

Future<Response> get({
Expand Down
22 changes: 12 additions & 10 deletions lib/agent/api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,28 @@ class APIProvider {
}

static Future<(DateTime, double)> getSysTime() async {
/// 请求开始时间
final int t1 = DateTime.now().millisecondsSinceEpoch;
/// 创建一个Stopwatch实例
final stopwatch = Stopwatch()..start();

/// 发起请求
var r = await httpAgent.get(path: "/timestamp");

/// 请求结束时间
final int t3 = DateTime.now().millisecondsSinceEpoch;
/// 停止Stopwatch
stopwatch.stop();
if (r.statusCode == 204) {
/// 从响应头中获取接收时间
final int serverTime = int.parse(r.headers["timestamp"]!);
/// 从响应头中获取服务器时间戳(毫秒级时间戳)
final int serverTimeMs = int.parse(r.headers["timestamp"]!);

/// 延迟
final int delay = (t3 - t1) ~/ 2;
/// 延迟时间,以毫秒为单位
final int delayMs = stopwatch.elapsedMilliseconds ~/ 2;

return (
DateTime.fromMillisecondsSinceEpoch(serverTime, isUtc: true),
delay / 1000
/// 将毫秒时间戳转换为 DateTime
DateTime.fromMillisecondsSinceEpoch(serverTimeMs, isUtc: true),
delayMs / 1000 // 转换为秒作为延迟时间
);
}

return (DateTime.now().toUtc(), 0.0);
}
}
8 changes: 4 additions & 4 deletions lib/base_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ class BasePage extends StatelessWidget {
filter: ImageFilter.blur(sigmaX: 10, sigmaY: 10),
child: Container(
decoration: BoxDecoration(
color: Colors.white.withOpacity(0.2),
color: Colors.white.withAlpha(100),
borderRadius: BorderRadius.circular(20),
border: Border.all(
color: Colors.white.withOpacity(0.2)),
border:
Border.all(color: Colors.white.withAlpha(100)),
),
child: TextField(
onChanged: GlobalVariable.searchRx.call,
Expand Down Expand Up @@ -104,7 +104,7 @@ class BasePage extends StatelessWidget {
.map((e) => ListTile(
title: Text(e.name),
tileColor: e.route == Get.currentRoute
? Colors.blue.withOpacity(0.2)
? Colors.blue.withAlpha(100)
: null,
onTap: e.onTap))
.toList(),
Expand Down
105 changes: 105 additions & 0 deletions lib/date_calculation/date_difference.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import 'package:flutter/material.dart';
import 'package:tools/utils/date/date_picker.dart';

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

@override
State<DateDifference> createState() => _DateDifferenceState();
}

class _DateDifferenceState extends State<DateDifference> {
DateTime firstDay = DateTime.now();
DateTime secondDay = DateTime.now();

Widget _buildDateInput(
{required BuildContext context,
required String title,
required DateTime date,
required Function() onTap}) {
return Wrap(
crossAxisAlignment: WrapCrossAlignment.center,
children: [
Text(
title,
style: TextStyle(
fontWeight: FontWeight.bold,
color: Theme.of(context).textTheme.bodyMedium!.color),
),
InkWell(
onTap: onTap,
child: Container(
padding: const EdgeInsets.all(10),
margin: const EdgeInsets.symmetric(horizontal: 10),
decoration: BoxDecoration(
border: Border.all(
color: Theme.of(context).dividerColor,
),
borderRadius: BorderRadius.circular(5),
),
child: Text(
formatDate(date),
),
),
),
],
);
}

@override
Widget build(BuildContext context) {
return Card(
elevation: 5,
child: Container(
width: MediaQuery.of(context).size.width * 0.9,
padding: const EdgeInsets.all(16),
child: Column(
children: [
const Text(
"计算日期差",
style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
),
Padding(
padding: const EdgeInsets.symmetric(vertical: 20),
child: _buildDateInput(
context: context,
title: "第一个日期: ",
date: firstDay,
onTap: () async {
final selectDate =
await openDatePicker(context, initialDate: firstDay);
if (selectDate != null) {
firstDay = selectDate;
setState(() {});
}
}),
),
Padding(
padding: const EdgeInsets.symmetric(vertical: 20),
child: _buildDateInput(
context: context,
title: "第二个日期: ",
date: secondDay,
onTap: () async {
final selectDate =
await openDatePicker(context, initialDate: secondDay);
if (selectDate != null) {
secondDay = selectDate;
setState(() {});
}
}),
),
Padding(
padding: const EdgeInsets.symmetric(vertical: 10),
child: Text(
"相差 ${firstDay.difference(secondDay).inDays.abs()} 天",
style:
const TextStyle(fontWeight: FontWeight.bold, fontSize: 24),
),
)
],
),
),
);
}
}
135 changes: 135 additions & 0 deletions lib/date_calculation/date_offset.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:tools/utils/date/date_picker.dart';

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

@override
State<DateOffset> createState() => _DateOffsetState();
}

class _DateOffsetState extends State<DateOffset> {
/// 计算几天后的日期
DateTime _date = DateTime.now();

/// 计算几天后的日期
int offsetDays = 0;

/// x天后的日期
String get _afterDaysDate {
try {
DateTime newDate = _date.add(Duration(days: offsetDays));
return formatDate(newDate);
} catch (e) {
return "日期计算错误";
}
}

@override
Widget build(BuildContext context) {
return Card(
elevation: 5,
child: Container(
width: MediaQuery.of(context).size.width * 0.9,
padding: const EdgeInsets.all(16),
child: Column(
children: [
const Text(
"计算日期偏移",
style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
),
Padding(
padding: const EdgeInsets.symmetric(vertical: 20),
child: Wrap(
crossAxisAlignment: WrapCrossAlignment.center,
children: [
Text(
"当前日期:",
style: TextStyle(
fontWeight: FontWeight.bold,
color: Theme.of(context).textTheme.bodyMedium!.color),
),
InkWell(
onTap: () async {
DateTime? selectDate =
await openDatePicker(context, initialDate: _date);
if (selectDate != null) {
_date = selectDate;
setState(() {});
}
},
child: Container(
padding: const EdgeInsets.all(10),
margin: const EdgeInsets.symmetric(horizontal: 10),
decoration: BoxDecoration(
border: Border.all(
color: Theme.of(context).dividerColor,
),
borderRadius: BorderRadius.circular(5),
),
child: Text(
formatDate(_date),
style: const TextStyle(fontSize: 16),
),
),
),
],
),
),
Wrap(
crossAxisAlignment: WrapCrossAlignment.center,
children: [
const Text(
"相差: ",
style: TextStyle(fontWeight: FontWeight.bold),
),
Padding(
padding:
const EdgeInsets.symmetric(horizontal: 10, vertical: 5),
child: SizedBox(
width: 100,
child: TextField(
keyboardType: TextInputType.number,
inputFormatters: [
FilteringTextInputFormatter.allow(
RegExp(r"^-?[0-9]*$")),
],
textAlign: TextAlign.center,
style: const TextStyle(fontWeight: FontWeight.bold),
onChanged: (String value) {
offsetDays = int.tryParse(value) ?? 0;
setState(() {});
},
decoration: const InputDecoration(
border: OutlineInputBorder(),
contentPadding: EdgeInsets.symmetric(vertical: 8),
),
),
),
),
const Text(
"天",
style: TextStyle(fontWeight: FontWeight.bold),
),
],
),
Text(
"*输入负数表示之前的日期",
style: TextStyle(
color: Colors.grey[600], fontStyle: FontStyle.italic),
),
if (offsetDays != 0)
Padding(
padding: const EdgeInsets.symmetric(vertical: 20),
child: Text(
"${offsetDays.abs()}天${offsetDays.isNegative ? "前" : "后"}的日期是: $_afterDaysDate",
style: const TextStyle(fontSize: 18),
),
),
],
),
),
);
}
}
Loading

0 comments on commit bcfc662

Please sign in to comment.