feat(date):更新日期组件

This commit is contained in:
2026-04-16 10:35:06 +08:00
parent 7b2dd68ab3
commit 6da78ca2a5
5 changed files with 258 additions and 297 deletions

View File

@@ -1,11 +1,6 @@
import 'package:dio/dio.dart';
import 'package:example/exif/customer_exif_Page.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/services.dart'; import 'package:flutter_common/flutter_common.dart';
import 'package:flutter_common/upload_image/look_images_widget.dart';
import 'package:flutter_common/upload_image/upload_images_tool.dart';
import 'package:get/get.dart'; import 'package:get/get.dart';
import 'package:image_editor_plus/image_editor_plus.dart';
void main() { void main() {
runApp(const MyApp()); runApp(const MyApp());
@@ -14,167 +9,189 @@ void main() {
class MyApp extends StatelessWidget { class MyApp extends StatelessWidget {
const MyApp({super.key}); const MyApp({super.key});
// This widget is the root of your application.
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return GetMaterialApp( return GetMaterialApp(
title: '熊猫文旅通', title: 'flutter_common Demo',
debugShowCheckedModeBanner: false, debugShowCheckedModeBanner: false,
theme: ThemeData( theme: ThemeData(
appBarTheme: AppBarTheme(surfaceTintColor: Colors.transparent), scaffoldBackgroundColor: const Color(0xFFF5F7FB),
scaffoldBackgroundColor: const Color(0xffF5F5F5), colorScheme: ColorScheme.fromSeed(seedColor: const Color(0xFF4D6FD5)),
// useMaterial3: true, cardTheme: const CardThemeData(
// colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple) color: Colors.white,
// .copyWith(background: const Color(0xffF5F5F5)), margin: EdgeInsets.zero,
elevation: 0,
),
), ),
home: const MyHomePage(title: '编辑图片'), home: const MyHomePage(),
); );
} }
} }
/// Dio 最简版:网络图片转 Uint8List
Future<Uint8List?> networkImageToUint8ListWithDio(String imageUrl) async {
final dio = Dio(); // 初始化 Dio 实例
try {
// 发起 GET 请求,响应类型设为字节数组(关键)
final response = await dio.get<List<int>>(
imageUrl,
options: Options(responseType: ResponseType.bytes),
);
// 响应成功且数据非空时,直接转为 Uint8List
return response.statusCode == 200 && response.data != null
? Uint8List.fromList(response.data!)
: null;
} catch (e) {
print('图片转换失败:$e'); // 捕获网络错误、URL 非法等异常
return null;
}
}
class MyHomePage extends StatefulWidget { class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title}); const MyHomePage({super.key});
// This widget is the home page of your application. It is stateful, meaning
// that it has a State object (defined below) that contains fields that affect
// how it looks.
// This class is the configuration for the state. It holds the values (in this
// case the title) provided by the parent (in this case the App widget) and
// used by the build method of the State. Fields in a Widget subclass are
// always marked "final".
final String title;
@override @override
State<MyHomePage> createState() => _MyHomePageState(); State<MyHomePage> createState() => _MyHomePageState();
} }
class _MyHomePageState extends State<MyHomePage> { class _MyHomePageState extends State<MyHomePage> {
final int _counter = 0; String _singleDateText = '未选择';
String _rangeDateText = '未选择';
Future<Uint8List?> editImage({required String url}) async {
Uint8List? imageBytes = await networkImageToUint8ListWithDio(url);
ImageEditor.setI18n({
'crop': '裁剪',
'rotate left': '左旋转',
'rotate right': '右旋转',
'flip': '水平翻转',
'brush': '涂抹',
'link': '链接',
'save': '保存',
'text': '文本',
'blur': '模糊',
'filter': '滤镜',
'size': '大小',
'color': '颜色',
'background color': '背景颜色',
'background opacity': '背景透明度',
'reset': '重置',
'freeform': '自由裁剪',
'remove': '移除',
'emoji': '表情',
'slider color': '滑块颜色',
'color opacity': '透明度',
'blur radius': '模糊半径',
});
if (mounted) {
Uint8List? editedImage = await Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ImageEditor(
image: imageBytes,
blurOption: null,
filtersOption: null,
brushOption: null,
textOption: null,
emojiOption: null,
),
),
);
return editedImage;
}
return null;
}
Future<void> _incrementCounter() async {}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
// This method is rerun every time setState is called, for instance as done
// by the _incrementCounter method above.
//
// The Flutter framework has been optimized to make rerunning build methods
// fast, so that you can just rebuild anything that needs updating rather
// than having to individually change instances of widgets.
return Scaffold( return Scaffold(
appBar: AppBar( appBar: AppBar(
// TRY THIS: Try changing the color here to a specific color (to title: const Text('flutter_common 示例首页'),
// Colors.amber, perhaps?) and trigger a hot reload to see the AppBar centerTitle: true,
// change color while the other colors stay the same.
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text(widget.title),
), ),
body: Center( body: SafeArea(
// Center is a layout widget. It takes a single child and positions it child: ListView(
// in the middle of the parent. padding: const EdgeInsets.all(16),
child: Column( children: [
// Column is also a layout widget. It takes a list of children and _buildIntroCard(),
// arranges them vertically. By default, it sizes itself to fit its const SizedBox(height: 16),
// children horizontally, and tries to be as tall as its parent. _buildDemoCard(
// title: '单日选择组件',
// Column has various properties to control how it sizes itself and description: '使用 `CalendarChooseWidget` 的单选模式,适合筛选某一天的数据。',
// how it positions its children. Here we use mainAxisAlignment to child: CalendarChooseWidget(
// center the children vertically; the main axis here is the vertical chooseIndex: 1,
// axis because Columns are vertical (the cross axis would be selectedDate: DateTime.now(),
// horizontal). dateTimeUtilsType: DateTimeUtilsType.yearMonthDayWord,
// fontSize: 18,
// TRY THIS: Invoke "debug painting" (choose the "Toggle Debug Paint" fontWeight: FontWeight.w600,
// action in the IDE, or press "p" in the console), to see the tapAction: (value) {
// wireframe for each widget. final startTime = value['startTime'] as DateTime?;
mainAxisAlignment: MainAxisAlignment.center, setState(() {
children: <Widget>[ _singleDateText = _formatDate(startTime);
const Text( });
'You have pushed the button this many times:', },
),
resultText: _singleDateText,
), ),
Text( const SizedBox(height: 16),
'$_counter', _buildDemoCard(
style: Theme.of(context).textTheme.headlineMedium, title: '区间选择组件',
description: '默认展示开始和结束日期,适合做报表、订单或运营时间筛选。',
child: CalendarChooseWidget(
selectedDate: DateTime.now(),
dateTimeUtilsType: DateTimeUtilsType.yearMonthDay,
fontSize: 18,
fontWeight: FontWeight.w600,
tapAction: (value) {
final startTime = value['startTime'] as DateTime?;
final endTime = value['endTime'] as DateTime?;
setState(() {
_rangeDateText =
'${_formatDate(startTime)}${_formatDate(endTime)}';
});
},
),
resultText: _rangeDateText,
), ),
TextButton(
onPressed: () {},
child: Text(
"data",
))
], ],
), ),
), ),
floatingActionButton: FloatingActionButton( );
onPressed: _incrementCounter, }
tooltip: 'Increment',
child: const Icon(Icons.add), Widget _buildIntroCard() {
), // This trailing comma makes auto-formatting nicer for build methods. return Card(
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(20)),
child: Padding(
padding: const EdgeInsets.all(20),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: const [
Text(
'项目结构速览',
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.w700,
color: Color(0xFF1A1A1A),
),
),
SizedBox(height: 12),
Text(
'lib/calendarcalendar 里提供了日期选择相关组件,'
'lib/upload_image 聚合图片上传与预览能力,'
'lib/utils 则放了日期、弹窗等通用工具。',
style: TextStyle(
fontSize: 15,
height: 1.6,
color: Color(0xFF4F5B67),
),
),
],
),
),
);
}
Widget _buildDemoCard({
required String title,
required String description,
required Widget child,
required String resultText,
}) {
return Card(
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(20)),
child: Padding(
padding: const EdgeInsets.all(20),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
title,
style: const TextStyle(
fontSize: 18,
fontWeight: FontWeight.w700,
color: Color(0xFF1A1A1A),
),
),
const SizedBox(height: 8),
Text(
description,
style: const TextStyle(
fontSize: 14,
height: 1.6,
color: Color(0xFF5C6670),
),
),
const SizedBox(height: 16),
Container(
width: double.infinity,
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 18),
decoration: BoxDecoration(
color: const Color(0xFFF7F9FC),
borderRadius: BorderRadius.circular(16),
border: Border.all(color: const Color(0xFFD9E1F2)),
),
child: child,
),
const SizedBox(height: 14),
Text(
'当前结果:$resultText',
style: const TextStyle(
fontSize: 14,
fontWeight: FontWeight.w500,
color: Color(0xFF4D6FD5),
),
),
],
),
),
);
}
String _formatDate(DateTime? date) {
if (date == null) {
return '未选择';
}
return DateTimeUtils.dateTimeUtilsTool(
dateTime: date.toIso8601String(),
dateTimeUtilsType: DateTimeUtilsType.yearMonthDay,
); );
} }
} }

View File

@@ -463,14 +463,6 @@ packages:
url: "https://pub.flutter-io.cn" url: "https://pub.flutter-io.cn"
source: hosted source: hosted
version: "10.12.0" version: "10.12.0"
freezed_annotation:
dependency: transitive
description:
name: freezed_annotation
sha256: "7294967ff0a6d98638e7acb774aac3af2550777accd8149c90af5b014e6d44d8"
url: "https://pub.flutter-io.cn"
source: hosted
version: "3.1.0"
get: get:
dependency: "direct main" dependency: "direct main"
description: description:
@@ -639,14 +631,6 @@ packages:
url: "https://pub.flutter-io.cn" url: "https://pub.flutter-io.cn"
source: hosted source: hosted
version: "0.7.2" version: "0.7.2"
json_annotation:
dependency: transitive
description:
name: json_annotation
sha256: "1ce844379ca14835a50d2f019a3099f419082cfdd231cd86a142af94dd5c6bb1"
url: "https://pub.flutter-io.cn"
source: hosted
version: "4.9.0"
leak_tracker: leak_tracker:
dependency: transitive dependency: transitive
description: description:
@@ -759,14 +743,6 @@ packages:
url: "https://pub.flutter-io.cn" url: "https://pub.flutter-io.cn"
source: hosted source: hosted
version: "2.1.0" version: "2.1.0"
omni_video_player:
dependency: transitive
description:
name: omni_video_player
sha256: e01ce74413c2eb1cfe042c81507ef2573af66e7ee2984b9ee45808d35a3ea9da
url: "https://pub.flutter-io.cn"
source: hosted
version: "3.7.2"
package_info_plus: package_info_plus:
dependency: transitive dependency: transitive
description: description:
@@ -1095,14 +1071,6 @@ packages:
url: "https://pub.flutter-io.cn" url: "https://pub.flutter-io.cn"
source: hosted source: hosted
version: "2.4.1" version: "2.4.1"
simple_sparse_list:
dependency: transitive
description:
name: simple_sparse_list
sha256: aa648fd240fa39b49dcd11c19c266990006006de6699a412de485695910fbc1f
url: "https://pub.flutter-io.cn"
source: hosted
version: "0.1.4"
sky_engine: sky_engine:
dependency: transitive dependency: transitive
description: flutter description: flutter
@@ -1236,14 +1204,6 @@ packages:
url: "https://pub.flutter-io.cn" url: "https://pub.flutter-io.cn"
source: hosted source: hosted
version: "1.4.0" version: "1.4.0"
unicode:
dependency: transitive
description:
name: unicode
sha256: a6f7bcfc8ea1d5ce1f6c0b1c39117a9919f4953edd9fd7a64090a9796c499b57
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.1.9"
url_launcher: url_launcher:
dependency: transitive dependency: transitive
description: description:
@@ -1364,14 +1324,6 @@ packages:
url: "https://pub.flutter-io.cn" url: "https://pub.flutter-io.cn"
source: hosted source: hosted
version: "2.4.0" version: "2.4.0"
visibility_detector:
dependency: transitive
description:
name: visibility_detector
sha256: dd5cc11e13494f432d15939c3aa8ae76844c42b723398643ce9addb88a5ed420
url: "https://pub.flutter-io.cn"
source: hosted
version: "0.4.0+2"
vm_service: vm_service:
dependency: transitive dependency: transitive
description: description:
@@ -1380,14 +1332,6 @@ packages:
url: "https://pub.flutter-io.cn" url: "https://pub.flutter-io.cn"
source: hosted source: hosted
version: "15.0.2" version: "15.0.2"
volume_controller:
dependency: transitive
description:
name: volume_controller
sha256: "5c1a13d2ea99d2f6753e7c660d0d3fab541f36da3999cafeb17b66fe49759ad7"
url: "https://pub.flutter-io.cn"
source: hosted
version: "3.4.1"
wakelock_plus: wakelock_plus:
dependency: transitive dependency: transitive
description: description:
@@ -1452,14 +1396,6 @@ packages:
url: "https://pub.flutter-io.cn" url: "https://pub.flutter-io.cn"
source: hosted source: hosted
version: "3.1.3" version: "3.1.3"
youtube_explode_dart:
dependency: transitive
description:
name: youtube_explode_dart
sha256: "3d731d71df9901b1915bae806781df519cff32517e36db279f844ae619669e45"
url: "https://pub.flutter-io.cn"
source: hosted
version: "3.0.5"
sdks: sdks:
dart: ">=3.10.3 <4.0.0" dart: ">=3.10.3 <4.0.0"
flutter: ">=3.38.4" flutter: ">=3.38.4"

View File

@@ -1,5 +1,6 @@
/// A Calculator. library flutter_common;
class Calculator {
/// Returns [value] plus 1. export 'calendarcalendar/calendar_choose_widget.dart';
int addOne(int value) => value + 1; export 'calendarcalendar/custom_calendar_range_picker_widget.dart';
} export 'calendarcalendar/custom_date_picker.dart';
export 'utils/date_utils.dart';

View File

@@ -59,100 +59,106 @@ class ToastUtils {
bool isShowConfirm = false, bool isShowConfirm = false,
Color? barrierColor, Color? barrierColor,
EdgeInsetsGeometry? padding, EdgeInsetsGeometry? padding,
bool useSafeArea = true, bool useSafeArea = false,
}) { }) {
cancelToast(); cancelToast();
return showDialog( return showDialog(
useSafeArea: useSafeArea, useSafeArea: useSafeArea,
context: context, context: context,
builder: (BuildContext ctx) { builder: (BuildContext ctx) {
return Container( final dialogHeight = height ?? MediaQuery.of(ctx).size.height / 2;
width: double.infinity, final bottomInset = MediaQuery.of(ctx).padding.bottom;
height: MediaQuery.of(context).size.height / 2, return Material(
margin: EdgeInsets.only( type: MaterialType.transparency,
top: height == null child: Align(
? MediaQuery.of(context).size.height / 2 alignment: Alignment.bottomCenter,
: (MediaQuery.of(context).size.height - height), child: Container(
), width: double.infinity,
padding: padding ?? const EdgeInsets.only(bottom: 40), height: dialogHeight,
decoration: const BoxDecoration( padding: (padding ?? const EdgeInsets.only(bottom: 40))
color: Colors.white, .add(EdgeInsets.only(bottom: bottomInset)),
borderRadius: BorderRadius.only( decoration: const BoxDecoration(
topLeft: Radius.circular(12), color: Colors.white,
topRight: Radius.circular(12), borderRadius: BorderRadius.only(
), topLeft: Radius.circular(12),
), topRight: Radius.circular(12),
child: Column( ),
children: [ ),
header ?? child: Column(
Container( children: [
padding: const EdgeInsets.only(bottom: 5), header ??
decoration: const BoxDecoration( Container(
border: Border( padding: const EdgeInsets.only(bottom: 5),
bottom: decoration: const BoxDecoration(
BorderSide(color: Color(0xffE1E1E1), width: 0.5), border: Border(
), bottom: BorderSide(
), color: Color(0xffE1E1E1),
child: Row( width: 0.5,
children: [
GestureDetector(
onTap: () => Navigator.of(context).pop(),
child: Container(
padding:
const EdgeInsets.only(left: 6, right: 10),
color: Colors.transparent,
child: Icon(
Icons.keyboard_arrow_down_rounded,
size: leftIconSize ?? 40,
), ),
), ),
), ),
Expanded( child: Row(
child: Container( children: [
alignment: Alignment.center, GestureDetector(
child: Text( onTap: () => Navigator.of(ctx).pop(),
title ?? '头部', child: Container(
style: TextStyle( padding:
color: const Color(0xff333333), const EdgeInsets.only(left: 6, right: 10),
fontSize: titleFontSize ?? 18, color: Colors.transparent,
fontWeight: FontWeight.bold, child: Icon(
Icons.keyboard_arrow_down_rounded,
size: leftIconSize ?? 40,
),
), ),
), ),
), Expanded(
child: Container(
alignment: Alignment.center,
child: Text(
title ?? '头部',
style: TextStyle(
color: const Color(0xff333333),
fontSize: titleFontSize ?? 18,
fontWeight: FontWeight.bold,
),
),
),
),
GestureDetector(
onTap: () {
if (isShowConfirm) {
if (onConfirm != null) {
onConfirm();
Navigator.of(ctx).pop();
}
}
},
child: Container(
padding: const EdgeInsets.only(
left: 10,
top: 8,
bottom: 8,
right: 18,
),
alignment: Alignment.center,
color: Colors.transparent,
child: Text(
'确定',
style: TextStyle(
color: isShowConfirm
? const Color(0xff4D6FD5)
: Colors.transparent,
fontSize: 16),
),
),
)
],
), ),
GestureDetector( ),
onTap: () { Expanded(child: contentWidget ?? const SizedBox())
if (isShowConfirm) { ],
if (onConfirm != null) { ),
onConfirm(); ),
Navigator.pop(context);
}
}
},
child: Container(
padding: const EdgeInsets.only(
left: 10,
top: 8,
bottom: 8,
right: 18,
),
alignment: Alignment.center,
color: Colors.transparent,
child: Text(
'确定',
style: TextStyle(
color: isShowConfirm
? const Color(0xff4D6FD5)
: Colors.transparent,
fontSize: 16),
),
),
)
],
),
),
Expanded(child: contentWidget ?? const SizedBox())
],
), ),
); );
}); });

View File

@@ -1,12 +1,13 @@
import 'package:flutter_common/flutter_common.dart';
import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_test/flutter_test.dart';
import 'package:flutter_common/flutter_common.dart';
void main() { void main() {
test('adds one to input values', () { test('exports date utility helpers', () {
final calculator = Calculator(); final formatted = DateTimeUtils.dateTimeUtilsTool(
expect(calculator.addOne(2), 3); dateTime: DateTime(2026, 4, 15).toIso8601String(),
expect(calculator.addOne(-7), -6); dateTimeUtilsType: DateTimeUtilsType.yearMonthDay,
expect(calculator.addOne(0), 1); );
expect(formatted, '2026-04-15');
}); });
} }