-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: multiple optimization refactoring
- Loading branch information
Showing
24 changed files
with
738 additions
and
1,125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
), | ||
) | ||
], | ||
), | ||
), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
), | ||
), | ||
], | ||
), | ||
), | ||
); | ||
} | ||
} |
Oops, something went wrong.