From 12ca107338d17437fc8ba7dd2b43c16a4e365a34 Mon Sep 17 00:00:00 2001 From: hemengyang Date: Sun, 24 Oct 2021 17:40:26 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E9=87=8D=E6=96=B0=E5=8A=A0=E5=9B=9E=20flut?= =?UTF-8?q?ter=5Fsticky=5Fheader?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/storage/view/home_page.dart | 112 +++++++++++++++------------ lib/storage/view/item_edit_page.dart | 4 +- pubspec.lock | 14 ++++ pubspec.yaml | 1 + 4 files changed, 81 insertions(+), 50 deletions(-) diff --git a/lib/storage/view/home_page.dart b/lib/storage/view/home_page.dart index 1012b346..54ddaa1a 100644 --- a/lib/storage/view/home_page.dart +++ b/lib/storage/view/home_page.dart @@ -1,9 +1,12 @@ import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; +import 'package:flutter_sticky_header/flutter_sticky_header.dart'; import 'package:smarthome/core/core.dart'; import 'package:smarthome/routers/delegate.dart'; import 'package:smarthome/storage/bloc/blocs.dart'; import 'package:smarthome/storage/model/models.dart'; +import 'package:smarthome/storage/repository/storage_repository.dart'; +import 'package:smarthome/storage/view/item_edit_page.dart'; import 'package:smarthome/storage/view/widgets/search_icon_button.dart'; import 'package:smarthome/utils/date_format_extension.dart'; import 'package:smarthome/widgets/center_loading_indicator.dart'; @@ -41,11 +44,23 @@ class StorageHomeScreen extends StatelessWidget { ], body: const _StorageHomeBody(), floatingActionButton: FloatingActionButton( - tooltip: '所有位置', + tooltip: '添加物品', onPressed: () async { - MyRouterDelegate.of(context).addStorageGroup(); + await Navigator.of(context).push( + MaterialPageRoute( + builder: (_) => BlocProvider( + create: (_) => ItemEditBloc( + storageRepository: + RepositoryProvider.of(context), + ), + child: const ItemEditPage( + isEditing: false, + ), + ), + ), + ); }, - child: const Icon(Icons.storage), + child: const Icon(Icons.add), ), ); } @@ -111,29 +126,32 @@ class _StorageHomeBody extends StatelessWidget { } List _buildSlivers(BuildContext context, StorageHomeSuccess state) { - final listofWidget = []; + List listofWidget = []; if (state.expiredItems?.isNotEmpty ?? false) { - listofWidget.add(_buildSliverList( - context, state.expiredItems, ItemType.expired, state.itemType)); + listofWidget.add(_buildSliverStickyHeader( + context, state.expiredItems!, ItemType.expired, state.itemType)); } if (state.nearExpiredItems?.isNotEmpty ?? false) { - listofWidget.add(_buildSliverList(context, state.nearExpiredItems, - ItemType.nearExpired, state.itemType)); + listofWidget.add(_buildSliverStickyHeader(context, + state.nearExpiredItems!, ItemType.nearExpired, state.itemType)); } if (state.recentlyEditedItems?.isNotEmpty ?? false) { - listofWidget.add(_buildSliverList(context, state.recentlyEditedItems, - ItemType.recentlyEdited, state.itemType)); + listofWidget.add(_buildSliverStickyHeader(context, + state.recentlyEditedItems!, ItemType.recentlyEdited, state.itemType)); } if (state.recentlyCreatedItems?.isNotEmpty ?? false) { - listofWidget.add(_buildSliverList(context, state.recentlyCreatedItems, - ItemType.recentlyCreated, state.itemType)); + listofWidget.add(_buildSliverStickyHeader( + context, + state.recentlyCreatedItems!, + ItemType.recentlyCreated, + state.itemType)); } return listofWidget; } - SliverList _buildSliverList( + SliverStickyHeader _buildSliverStickyHeader( BuildContext context, - List? items, + List items, ItemType listType, ItemType currentType, ) { @@ -154,40 +172,38 @@ class _StorageHomeBody extends StatelessWidget { case ItemType.all: break; } - return SliverList( - delegate: SliverChildBuilderDelegate( - (BuildContext context, int index) => index == 0 - ? Container( - height: 60.0, - color: DefaultTextStyle.of(context).style.backgroundColor, - padding: const EdgeInsets.symmetric(horizontal: 16.0), - alignment: Alignment.centerLeft, - child: Row( - children: [ - Text( - headerText, - style: DefaultTextStyle.of(context).style, - ), - const Spacer(), - IconButton( - icon: Icon(currentType == ItemType.all - ? Icons.expand_more - : Icons.expand_less), - onPressed: () { - BlocProvider.of(context).add( - StorageHomeFetched( - itemType: currentType != ItemType.all - ? ItemType.all - : listType), - ); - }, - ) - ], - ), - ) - : _buildItemListItem( - context, items![index - 1], listType, currentType), - childCount: items!.length + 1, + return SliverStickyHeader( + header: Container( + height: 60.0, + color: Theme.of(context).colorScheme.surface, + padding: const EdgeInsets.symmetric(horizontal: 16.0), + alignment: Alignment.centerLeft, + child: Row( + children: [ + Text(headerText), + const Spacer(), + IconButton( + icon: Icon(currentType == ItemType.all + ? Icons.expand_more + : Icons.expand_less), + onPressed: () { + BlocProvider.of(context).add( + StorageHomeFetched( + itemType: currentType != ItemType.all + ? ItemType.all + : listType), + ); + }, + ) + ], + ), + ), + sliver: SliverList( + delegate: SliverChildBuilderDelegate( + (BuildContext context, int index) => + _buildItemListItem(context, items[index], listType, currentType), + childCount: items.length, + ), ), ); } diff --git a/lib/storage/view/item_edit_page.dart b/lib/storage/view/item_edit_page.dart index 34a8d29b..4913e3ee 100644 --- a/lib/storage/view/item_edit_page.dart +++ b/lib/storage/view/item_edit_page.dart @@ -25,7 +25,7 @@ class ItemEditPage extends StatefulWidget { required this.isEditing, this.item, this.storage, - }) : assert(item != null || storage != null), + }) : assert(item != null || !isEditing), super(key: key); @override @@ -269,7 +269,7 @@ class _ItemEditPageState extends State { _numberController = TextEditingController(text: '1'); _descriptionController = TextEditingController(); _priceController = TextEditingController(); - storageId = widget.storage!.id; + storageId = widget.storage?.id; } _nameFocusNode = FocusNode(); diff --git a/pubspec.lock b/pubspec.lock index 03421f39..e5980d61 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -403,6 +403,13 @@ packages: url: "https://pub.flutter-io.cn" source: hosted version: "2.0.4" + flutter_sticky_header: + dependency: "direct main" + description: + name: flutter_sticky_header + url: "https://pub.flutter-io.cn" + source: hosted + version: "0.6.0" flutter_test: dependency: "direct dev" description: flutter @@ -1125,6 +1132,13 @@ packages: url: "https://pub.flutter-io.cn" source: hosted version: "3.0.5" + value_layout_builder: + dependency: transitive + description: + name: value_layout_builder + url: "https://pub.flutter-io.cn" + source: hosted + version: "0.3.1" vector_math: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 7071ac6e..6765b965 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -47,6 +47,7 @@ dependencies: cached_network_image: ^3.0.0 settings_ui: ^1.0.0 dropdown_search: ^1.0.0 + flutter_sticky_header: ^0.6.0 dev_dependencies: flutter_test: From 489506d65f1c8db1b385b496aaa7e1a2e27e62da Mon Sep 17 00:00:00 2001 From: hemengyang Date: Sun, 24 Oct 2021 17:50:11 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=89=80=E6=9C=89?= =?UTF-8?q?=E4=BD=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/storage/view/home_page.dart | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/lib/storage/view/home_page.dart b/lib/storage/view/home_page.dart index 54ddaa1a..9ddc9b6b 100644 --- a/lib/storage/view/home_page.dart +++ b/lib/storage/view/home_page.dart @@ -127,6 +127,30 @@ class _StorageHomeBody extends StatelessWidget { List _buildSlivers(BuildContext context, StorageHomeSuccess state) { List listofWidget = []; + + listofWidget.add(SliverToBoxAdapter( + child: InkWell( + onTap: () { + MyRouterDelegate.of(context).addStorageGroup(); + }, + child: Card( + color: Theme.of(context).colorScheme.secondary, + child: ListTile( + title: Text( + '所有位置', + style: TextStyle( + color: Theme.of(context).colorScheme.onSecondary, + ), + ), + trailing: Icon( + Icons.storage, + color: Theme.of(context).colorScheme.onSecondary, + ), + ), + ), + ), + )); + if (state.expiredItems?.isNotEmpty ?? false) { listofWidget.add(_buildSliverStickyHeader( context, state.expiredItems!, ItemType.expired, state.itemType)); From a4302149123db1f4f19af65edfc9781d30bf0890 Mon Sep 17 00:00:00 2001 From: hemengyang Date: Sun, 24 Oct 2021 17:58:57 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E6=89=80=E6=9C=89=E7=95=8C=E9=9D=A2?= =?UTF-8?q?=E5=9D=87=E5=8F=AF=E4=BB=A5=E6=B7=BB=E5=8A=A0=E7=89=A9=E5=93=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/storage/view/storage_datail_page.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/storage/view/storage_datail_page.dart b/lib/storage/view/storage_datail_page.dart index 7a66d17c..3053721e 100644 --- a/lib/storage/view/storage_datail_page.dart +++ b/lib/storage/view/storage_datail_page.dart @@ -284,7 +284,7 @@ class StorageDetailScreen extends StatelessWidget { Widget? _buildFloatingActionButton( BuildContext context, StorageDetailState state) { - if (state is StorageDetailSuccess && state.storage != null) { + if (state is StorageDetailSuccess) { return FloatingActionButton( tooltip: '添加物品', onPressed: () async {