Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(storage): 调整 UI #171

Merged
merged 3 commits into from
Oct 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 88 additions & 48 deletions lib/storage/view/home_page.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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<ItemEditBloc>(
create: (_) => ItemEditBloc(
storageRepository:
RepositoryProvider.of<StorageRepository>(context),
),
child: const ItemEditPage(
isEditing: false,
),
),
),
);
},
child: const Icon(Icons.storage),
child: const Icon(Icons.add),
),
);
}
Expand Down Expand Up @@ -111,29 +126,56 @@ class _StorageHomeBody extends StatelessWidget {
}

List<Widget> _buildSlivers(BuildContext context, StorageHomeSuccess state) {
final listofWidget = <Widget>[];
List<Widget> 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(_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<Item>? items,
List<Item> items,
ItemType listType,
ItemType currentType,
) {
Expand All @@ -154,40 +196,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: <Widget>[
Text(
headerText,
style: DefaultTextStyle.of(context).style,
),
const Spacer(),
IconButton(
icon: Icon(currentType == ItemType.all
? Icons.expand_more
: Icons.expand_less),
onPressed: () {
BlocProvider.of<StorageHomeBloc>(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: <Widget>[
Text(headerText),
const Spacer(),
IconButton(
icon: Icon(currentType == ItemType.all
? Icons.expand_more
: Icons.expand_less),
onPressed: () {
BlocProvider.of<StorageHomeBloc>(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,
),
),
);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/storage/view/item_edit_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -269,7 +269,7 @@ class _ItemEditPageState extends State<ItemEditPage> {
_numberController = TextEditingController(text: '1');
_descriptionController = TextEditingController();
_priceController = TextEditingController();
storageId = widget.storage!.id;
storageId = widget.storage?.id;
}

_nameFocusNode = FocusNode();
Expand Down
2 changes: 1 addition & 1 deletion lib/storage/view/storage_datail_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
14 changes: 14 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down