From fbd129c90bc707fe57c3310debbc88293470a0de Mon Sep 17 00:00:00 2001 From: louis <869322496@qq.com> Date: Tue, 26 Mar 2024 16:36:27 +0800 Subject: [PATCH] feat: date picker on inout page --- .../components/custom_app_bar.dart | 35 ++++--------- .../inventory_inout/components/dates.dart | 9 ++-- .../components/inventory_inout_info.dart | 1 - .../components/inventory_inout_page_View.dart | 2 +- .../inventory_inout/components/task_list.dart | 49 ++++++++++--------- .../components/upper_body.dart | 1 + .../inventory_inout_controller.dart | 40 ++++++++------- lib/screens/landing/landing.dart | 1 - .../new_inventory_inout.dart | 5 -- lib/widgets/core/zt_base_date_picker.dart | 22 ++++++--- lib/widgets/loading_indicator.dart | 4 +- 11 files changed, 82 insertions(+), 87 deletions(-) diff --git a/lib/screens/inventory_inout/components/custom_app_bar.dart b/lib/screens/inventory_inout/components/custom_app_bar.dart index cc3241c..bd151df 100644 --- a/lib/screens/inventory_inout/components/custom_app_bar.dart +++ b/lib/screens/inventory_inout/components/custom_app_bar.dart @@ -59,33 +59,16 @@ class CustomAppBar extends StatelessWidget { showCupertinoModalPopup( context: Get.overlayContext!, builder: (BuildContext context) { - return ZtBaseDatePicker(); + return ZtBaseDatePicker( + initialDate: controller.endTime.value, + endDate: controller.endTime.value, + onDateTimeChanged: (date) { + controller.endTime.value = DateTime.parse(date); + controller.getData(); + }, + ); }, ); - // showCupertinoModalPopup( - // context: Get.overlayContext!, - // builder: (BuildContext context) { - // return Container( - // height: 200, - // color: Colors.white, - // child: CupertinoDatePicker( - // mode: CupertinoDatePickerMode.date, - // initialDateTime: DateTime.now(), - // onDateTimeChanged: (DateTime newDateTime) { - // // 处理用户选择的日期 - // }, - // ), - // ); - // }, - // ); - // final picker = await CupertinoDatePicker( - // context: Get.overlayContext!, - // initialDate: DateTime.now(), - // helpText: '选择的日期', - // confirmText: '确定', - // cancelText: '取消', - // firstDate: DateTime(2000, 1, 1), - // lastDate: DateTime.now().add(const Duration(days: 7))); }, child: Container( height: ScreenAdaper.height(70), @@ -93,7 +76,7 @@ class CustomAppBar extends StatelessWidget { decoration: BoxDecoration( borderRadius: BorderRadius.circular(20), color: AppTheme.primaryColorLight, - gradient: LinearGradient(colors: [ + gradient: const LinearGradient(colors: [ AppTheme.primaryColorLight, AppTheme.primaryColor, ]), diff --git a/lib/screens/inventory_inout/components/dates.dart b/lib/screens/inventory_inout/components/dates.dart index e76c508..4e46626 100644 --- a/lib/screens/inventory_inout/components/dates.dart +++ b/lib/screens/inventory_inout/components/dates.dart @@ -14,7 +14,8 @@ class Dates extends StatelessWidget { children: [ Obx( () => Text( - DateUtil.getMonth(DateTime.now().add(Duration(days: -index))), + DateUtil.getMonth( + controller.endTime.value.add(Duration(days: -index))), style: TextStyle( color: controller.currentIndex.value == index ? Colors.white @@ -26,7 +27,8 @@ class Dates extends StatelessWidget { ), Obx( () => Text( - DateUtil.getDate(DateTime.now().add(Duration(days: -index))), + DateUtil.getDate( + controller.endTime.value.add(Duration(days: -index))), style: TextStyle( color: controller.currentIndex.value == index ? Colors.white @@ -38,7 +40,8 @@ class Dates extends StatelessWidget { ), Obx( () => Text( - DateUtil.getDay(DateTime.now().add(Duration(days: -index))), + DateUtil.getDay( + controller.endTime.value.add(Duration(days: -index))), style: TextStyle( color: controller.currentIndex.value == index ? Colors.white diff --git a/lib/screens/inventory_inout/components/inventory_inout_info.dart b/lib/screens/inventory_inout/components/inventory_inout_info.dart index 8f99894..ef2d854 100644 --- a/lib/screens/inventory_inout/components/inventory_inout_info.dart +++ b/lib/screens/inventory_inout/components/inventory_inout_info.dart @@ -181,7 +181,6 @@ class InventoryInouInfoController extends GetxController { } Future getData() async { - printInfo(info: '$inventoryInoutId'); await Future.delayed(const Duration(milliseconds: 500)); final res = await Api.getInventoryInOutInfo(inventoryInoutId); if (res.data != null) { diff --git a/lib/screens/inventory_inout/components/inventory_inout_page_View.dart b/lib/screens/inventory_inout/components/inventory_inout_page_View.dart index 3ee4da6..f7c31c7 100644 --- a/lib/screens/inventory_inout/components/inventory_inout_page_View.dart +++ b/lib/screens/inventory_inout/components/inventory_inout_page_View.dart @@ -12,7 +12,7 @@ class InventoryInoutView extends StatelessWidget { physics: const BouncingScrollPhysics(), reverse: true, onPageChanged: (index) { - controller.changePage(index); + controller.setIndex(index); }, controller: controller.pageController, children: List.generate( diff --git a/lib/screens/inventory_inout/components/task_list.dart b/lib/screens/inventory_inout/components/task_list.dart index 58568b3..ad76e89 100644 --- a/lib/screens/inventory_inout/components/task_list.dart +++ b/lib/screens/inventory_inout/components/task_list.dart @@ -6,6 +6,7 @@ import 'package:sk_base_mobile/screens/inventory_inout/components/responsive.dar import 'package:sk_base_mobile/screens/inventory_inout/components/inventory_inout_card.dart'; import 'package:sk_base_mobile/screens/inventory_inout/inventory_inout_controller.dart'; import 'package:sk_base_mobile/util/util.dart'; +import 'package:sk_base_mobile/widgets/loading_indicator.dart'; class TaskList extends StatelessWidget { const TaskList({super.key, required this.index}); @@ -43,28 +44,30 @@ class Grid extends StatelessWidget { required this.ind}); @override Widget build(BuildContext context) { - return Obx(() => controller.list[ind].isEmpty - ? Center( - child: Text( - TextEnum.noInventoryInout, - style: TextStyle( - color: AppTheme.black, - fontWeight: FontWeight.w600, - letterSpacing: ScreenAdaper.width(2), - fontSize: ScreenAdaper.sp(30)), - ), - ) - : GridView.builder( - padding: const EdgeInsets.only(top: 40), - itemCount: controller.list[ind].length, - gridDelegate: SliverGridDelegateWithFixedCrossAxisCount( - crossAxisCount: crossAsis, childAspectRatio: ratio), - itemBuilder: (context, index) { - return InventoryInoutCard( - index: index, - ind: ind, - ); - }, - )); + return Obx(() => controller.loading.value + ? const LoadingIndicator(common: true) + : controller.list[ind].isEmpty + ? Center( + child: Text( + TextEnum.noInventoryInout, + style: TextStyle( + color: AppTheme.black, + fontWeight: FontWeight.w600, + letterSpacing: ScreenAdaper.width(2), + fontSize: ScreenAdaper.sp(30)), + ), + ) + : GridView.builder( + padding: const EdgeInsets.only(top: 40), + itemCount: controller.list[ind].length, + gridDelegate: SliverGridDelegateWithFixedCrossAxisCount( + crossAxisCount: crossAsis, childAspectRatio: ratio), + itemBuilder: (context, index) { + return InventoryInoutCard( + index: index, + ind: ind, + ); + }, + )); } } diff --git a/lib/screens/inventory_inout/components/upper_body.dart b/lib/screens/inventory_inout/components/upper_body.dart index 4722c0f..c5647c8 100644 --- a/lib/screens/inventory_inout/components/upper_body.dart +++ b/lib/screens/inventory_inout/components/upper_body.dart @@ -20,6 +20,7 @@ class UperBody extends StatelessWidget { SizedBox( height: ScreenAdaper.height(150), child: ListView.builder( + key: PageStorageKey('${controller.endTime.value}'), controller: controller.scrollController, shrinkWrap: true, reverse: true, diff --git a/lib/screens/inventory_inout/inventory_inout_controller.dart b/lib/screens/inventory_inout/inventory_inout_controller.dart index 635be56..9feac5f 100644 --- a/lib/screens/inventory_inout/inventory_inout_controller.dart +++ b/lib/screens/inventory_inout/inventory_inout_controller.dart @@ -17,7 +17,7 @@ class InventoryInoutController extends GetxController { RxMap userData = {}.obs; RxInt currentIndex = 0.obs; final PageController pageController = PageController(); - final DateTime dateTime = DateTime.now(); + Rx endTime = Rx(DateTime.now()); final DbHelper db = DbHelper(); List> list = [ [].obs, @@ -32,17 +32,13 @@ class InventoryInoutController extends GetxController { RxList model = [].obs; final ScrollController scrollController = ScrollController(); final daysNum = 20; + RxBool loading = RxBool(false); @override onReady() { super.onReady(); getData(); } - Future changePage(int index) async { - LoggerUtil() - .info(DateUtil.format(DateTime.now().add(Duration(days: -index)))); - } - /// 打开出库还是入库选择框 Future showInOrOutPickerDialog() async { showDialog( @@ -253,17 +249,25 @@ class InventoryInoutController extends GetxController { } Future> getInoutHistory() async { - final res = await Api.getInventoryInout({}); - printInfo(info: res.toString()); - if (res.data != null) { - return res.data!.items - .map((e) => InventoryInOutModel.fromJson(e)) - .toList(); + loading.value = true; + await Future.delayed(const Duration(milliseconds: 500)); + final selectedDate = + DateUtil.format(endTime.value.add(Duration(days: -currentIndex.value))); + try { + final res = await Api.getInventoryInout({ + 'time': [selectedDate, selectedDate] + }); + if (res.data != null) { + return res.data!.items + .map((e) => InventoryInOutModel.fromJson(e)) + .toList(); + } + return []; + } catch (e) { + return []; + } finally { + loading.value = false; } - return []; - // final List> queryResult = - // await dbClient!.query('Tasks'); - // return queryResult.map((e) => TaskModel.fromMap(e)).toList(); } getData() async { @@ -277,10 +281,12 @@ class InventoryInoutController extends GetxController { pageController.animateToPage(value, duration: const Duration(milliseconds: 300), curve: Curves.easeIn); currentIndex.value = value; + getData(); } getDateAccordingTabs(int value) { - return DateUtil.format(dateTime.add(Duration(days: -(daysNum - value)))); + return DateUtil.format( + endTime.value.add(Duration(days: -(daysNum - value)))); } getSepretLists() { diff --git a/lib/screens/landing/landing.dart b/lib/screens/landing/landing.dart index 4f9c0ac..1a5f223 100644 --- a/lib/screens/landing/landing.dart +++ b/lib/screens/landing/landing.dart @@ -13,7 +13,6 @@ class LandingPage extends StatelessWidget { @override Widget build(BuildContext context) { - printInfo(info: 'landing'); return Material( child: Stack(children: [ const BackColors(), diff --git a/lib/screens/new_inventory_inout/new_inventory_inout.dart b/lib/screens/new_inventory_inout/new_inventory_inout.dart index 0873f42..5f8e55e 100644 --- a/lib/screens/new_inventory_inout/new_inventory_inout.dart +++ b/lib/screens/new_inventory_inout/new_inventory_inout.dart @@ -327,11 +327,6 @@ class NewInventoryInout extends StatelessWidget { floatingLabelBehavior: FloatingLabelBehavior.always), readOnly: true, onTap: () { - printInfo( - info: firstLevelData - .map((e) => Center(child: (Text(e.label)))) - .toList() - .toString()); ModalUtil.showBottomSheetPicker( title: '库存位置', firstLevel: firstLevelData diff --git a/lib/widgets/core/zt_base_date_picker.dart b/lib/widgets/core/zt_base_date_picker.dart index a81ca25..f1cf476 100644 --- a/lib/widgets/core/zt_base_date_picker.dart +++ b/lib/widgets/core/zt_base_date_picker.dart @@ -5,17 +5,23 @@ import 'package:sk_base_mobile/util/logger_util.dart'; import 'package:sk_base_mobile/util/screen_adaper_util.dart'; class ZtBaseDatePicker extends StatelessWidget { - final Function? onDateTimeChanged; - final yearController = - FixedExtentScrollController(initialItem: DateTime.now().year - 2000); - final monthController = - FixedExtentScrollController(initialItem: DateTime.now().month - 1); - final dayController = - FixedExtentScrollController(initialItem: DateTime.now().day - 1); - ZtBaseDatePicker({super.key, this.onDateTimeChanged}); + final Function(String)? onDateTimeChanged; + late final FixedExtentScrollController yearController; + late final FixedExtentScrollController monthController; + late final FixedExtentScrollController dayController; + DateTime? initialDate = DateTime.now(); + DateTime? endDate = DateTime.now(); + ZtBaseDatePicker( + {super.key, this.onDateTimeChanged, this.initialDate, this.endDate}); @override Widget build(BuildContext context) { + yearController = FixedExtentScrollController( + initialItem: (initialDate ?? DateTime.now()).year - 2000); + monthController = FixedExtentScrollController( + initialItem: (initialDate ?? DateTime.now()).month - 1); + dayController = FixedExtentScrollController( + initialItem: (initialDate ?? DateTime.now()).day - 1); return Container( height: ScreenAdaper.height(250), color: Colors.white, diff --git a/lib/widgets/loading_indicator.dart b/lib/widgets/loading_indicator.dart index 29a9a19..bd899ef 100644 --- a/lib/widgets/loading_indicator.dart +++ b/lib/widgets/loading_indicator.dart @@ -13,11 +13,11 @@ class LoadingIndicator extends StatelessWidget { Widget build(BuildContext context) { return common ? LoadingAnimationWidget.fourRotatingDots( - color: AppTheme.primaryColorLight, size: ScreenAdaper.sp(40)) + color: AppTheme.primaryColorLight, size: ScreenAdaper.sp(50)) : CupertinoActivityIndicator( animating: animating, color: AppTheme.primaryColor, - radius: ScreenAdaper.sp(20), + radius: ScreenAdaper.sp(25), ); } }