diff --git a/example/lib/main.dart b/example/lib/main.dart index 09e31a1..06fa806 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -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/services.dart'; -import 'package:flutter_common/upload_image/look_images_widget.dart'; -import 'package:flutter_common/upload_image/upload_images_tool.dart'; +import 'package:flutter_common/flutter_common.dart'; import 'package:get/get.dart'; -import 'package:image_editor_plus/image_editor_plus.dart'; void main() { runApp(const MyApp()); @@ -14,167 +9,189 @@ void main() { class MyApp extends StatelessWidget { const MyApp({super.key}); - // This widget is the root of your application. @override Widget build(BuildContext context) { return GetMaterialApp( - title: '熊猫文旅通', + title: 'flutter_common Demo', debugShowCheckedModeBanner: false, theme: ThemeData( - appBarTheme: AppBarTheme(surfaceTintColor: Colors.transparent), - scaffoldBackgroundColor: const Color(0xffF5F5F5), - // useMaterial3: true, - // colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple) - // .copyWith(background: const Color(0xffF5F5F5)), + scaffoldBackgroundColor: const Color(0xFFF5F7FB), + colorScheme: ColorScheme.fromSeed(seedColor: const Color(0xFF4D6FD5)), + cardTheme: const CardThemeData( + color: Colors.white, + margin: EdgeInsets.zero, + elevation: 0, + ), ), - home: const MyHomePage(title: '编辑图片'), + home: const MyHomePage(), ); } } -/// Dio 最简版:网络图片转 Uint8List -Future networkImageToUint8ListWithDio(String imageUrl) async { - final dio = Dio(); // 初始化 Dio 实例 - try { - // 发起 GET 请求,响应类型设为字节数组(关键) - final response = await dio.get>( - 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 { - const MyHomePage({super.key, required this.title}); - - // 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; + const MyHomePage({super.key}); @override State createState() => _MyHomePageState(); } class _MyHomePageState extends State { - final int _counter = 0; - - Future 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 _incrementCounter() async {} + String _singleDateText = '未选择'; + String _rangeDateText = '未选择'; @override 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( appBar: AppBar( - // TRY THIS: Try changing the color here to a specific color (to - // Colors.amber, perhaps?) and trigger a hot reload to see the AppBar - // 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), + title: const Text('flutter_common 示例首页'), + centerTitle: true, ), - body: Center( - // Center is a layout widget. It takes a single child and positions it - // in the middle of the parent. - child: Column( - // Column is also a layout widget. It takes a list of children and - // arranges them vertically. By default, it sizes itself to fit its - // children horizontally, and tries to be as tall as its parent. - // - // Column has various properties to control how it sizes itself and - // how it positions its children. Here we use mainAxisAlignment to - // center the children vertically; the main axis here is the vertical - // axis because Columns are vertical (the cross axis would be - // horizontal). - // - // TRY THIS: Invoke "debug painting" (choose the "Toggle Debug Paint" - // action in the IDE, or press "p" in the console), to see the - // wireframe for each widget. - mainAxisAlignment: MainAxisAlignment.center, - children: [ - const Text( - 'You have pushed the button this many times:', + body: SafeArea( + child: ListView( + padding: const EdgeInsets.all(16), + children: [ + _buildIntroCard(), + const SizedBox(height: 16), + _buildDemoCard( + title: '单日选择组件', + description: '使用 `CalendarChooseWidget` 的单选模式,适合筛选某一天的数据。', + child: CalendarChooseWidget( + chooseIndex: 1, + selectedDate: DateTime.now(), + dateTimeUtilsType: DateTimeUtilsType.yearMonthDayWord, + fontSize: 18, + fontWeight: FontWeight.w600, + tapAction: (value) { + final startTime = value['startTime'] as DateTime?; + setState(() { + _singleDateText = _formatDate(startTime); + }); + }, + ), + resultText: _singleDateText, ), - Text( - '$_counter', - style: Theme.of(context).textTheme.headlineMedium, + const SizedBox(height: 16), + _buildDemoCard( + 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), - ), // This trailing comma makes auto-formatting nicer for build methods. + ); + } + + Widget _buildIntroCard() { + 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, ); } } diff --git a/example/pubspec.lock b/example/pubspec.lock index f0a18ee..04221c6 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -463,14 +463,6 @@ packages: url: "https://pub.flutter-io.cn" source: hosted 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: dependency: "direct main" description: @@ -639,14 +631,6 @@ packages: url: "https://pub.flutter-io.cn" source: hosted 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: dependency: transitive description: @@ -759,14 +743,6 @@ packages: url: "https://pub.flutter-io.cn" source: hosted 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: dependency: transitive description: @@ -1095,14 +1071,6 @@ packages: url: "https://pub.flutter-io.cn" source: hosted 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: dependency: transitive description: flutter @@ -1236,14 +1204,6 @@ packages: url: "https://pub.flutter-io.cn" source: hosted 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: dependency: transitive description: @@ -1364,14 +1324,6 @@ packages: url: "https://pub.flutter-io.cn" source: hosted 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: dependency: transitive description: @@ -1380,14 +1332,6 @@ packages: url: "https://pub.flutter-io.cn" source: hosted 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: dependency: transitive description: @@ -1452,14 +1396,6 @@ packages: url: "https://pub.flutter-io.cn" source: hosted 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: dart: ">=3.10.3 <4.0.0" flutter: ">=3.38.4" diff --git a/lib/flutter_common.dart b/lib/flutter_common.dart index 298576d..24eb7ea 100644 --- a/lib/flutter_common.dart +++ b/lib/flutter_common.dart @@ -1,5 +1,6 @@ -/// A Calculator. -class Calculator { - /// Returns [value] plus 1. - int addOne(int value) => value + 1; -} +library flutter_common; + +export 'calendarcalendar/calendar_choose_widget.dart'; +export 'calendarcalendar/custom_calendar_range_picker_widget.dart'; +export 'calendarcalendar/custom_date_picker.dart'; +export 'utils/date_utils.dart'; diff --git a/lib/utils/toast_utils.dart b/lib/utils/toast_utils.dart index 739e072..5ddce9f 100755 --- a/lib/utils/toast_utils.dart +++ b/lib/utils/toast_utils.dart @@ -59,100 +59,106 @@ class ToastUtils { bool isShowConfirm = false, Color? barrierColor, EdgeInsetsGeometry? padding, - bool useSafeArea = true, + bool useSafeArea = false, }) { cancelToast(); return showDialog( useSafeArea: useSafeArea, context: context, builder: (BuildContext ctx) { - return Container( - width: double.infinity, - height: MediaQuery.of(context).size.height / 2, - margin: EdgeInsets.only( - top: height == null - ? MediaQuery.of(context).size.height / 2 - : (MediaQuery.of(context).size.height - height), - ), - padding: padding ?? const EdgeInsets.only(bottom: 40), - decoration: const BoxDecoration( - color: Colors.white, - borderRadius: BorderRadius.only( - topLeft: Radius.circular(12), - topRight: Radius.circular(12), - ), - ), - child: Column( - children: [ - header ?? - Container( - padding: const EdgeInsets.only(bottom: 5), - decoration: const BoxDecoration( - border: Border( - bottom: - BorderSide(color: Color(0xffE1E1E1), width: 0.5), - ), - ), - child: Row( - 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, + final dialogHeight = height ?? MediaQuery.of(ctx).size.height / 2; + final bottomInset = MediaQuery.of(ctx).padding.bottom; + return Material( + type: MaterialType.transparency, + child: Align( + alignment: Alignment.bottomCenter, + child: Container( + width: double.infinity, + height: dialogHeight, + padding: (padding ?? const EdgeInsets.only(bottom: 40)) + .add(EdgeInsets.only(bottom: bottomInset)), + decoration: const BoxDecoration( + color: Colors.white, + borderRadius: BorderRadius.only( + topLeft: Radius.circular(12), + topRight: Radius.circular(12), + ), + ), + child: Column( + children: [ + header ?? + Container( + padding: const EdgeInsets.only(bottom: 5), + decoration: const BoxDecoration( + border: Border( + bottom: BorderSide( + color: Color(0xffE1E1E1), + width: 0.5, ), ), ), - Expanded( - child: Container( - alignment: Alignment.center, - child: Text( - title ?? '头部', - style: TextStyle( - color: const Color(0xff333333), - fontSize: titleFontSize ?? 18, - fontWeight: FontWeight.bold, + child: Row( + children: [ + GestureDetector( + onTap: () => Navigator.of(ctx).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: 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: () { - 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()) - ], + ), + Expanded(child: contentWidget ?? const SizedBox()) + ], + ), + ), ), ); }); diff --git a/test/flutter_common_test.dart b/test/flutter_common_test.dart index e4946b1..bac4698 100644 --- a/test/flutter_common_test.dart +++ b/test/flutter_common_test.dart @@ -1,12 +1,13 @@ +import 'package:flutter_common/flutter_common.dart'; import 'package:flutter_test/flutter_test.dart'; -import 'package:flutter_common/flutter_common.dart'; - void main() { - test('adds one to input values', () { - final calculator = Calculator(); - expect(calculator.addOne(2), 3); - expect(calculator.addOne(-7), -6); - expect(calculator.addOne(0), 1); + test('exports date utility helpers', () { + final formatted = DateTimeUtils.dateTimeUtilsTool( + dateTime: DateTime(2026, 4, 15).toIso8601String(), + dateTimeUtilsType: DateTimeUtilsType.yearMonthDay, + ); + + expect(formatted, '2026-04-15'); }); }