Skip to content

Commit

Permalink
fix(storage): 修复无法选择 家 这个位置的问题 (#279)
Browse files Browse the repository at this point in the history
* 添加根节点 家

* 统一用到 家 的地方
  • Loading branch information
he0119 authored Oct 22, 2022
1 parent afeee8b commit 9a05f93
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 35 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/lang/zh-CN/
### Fixed

- 修复 Windows 上的字体显示问题
- 修复无法选择 家 这个位置的问题

## [0.9.0] - 2022-10-02

Expand Down
9 changes: 5 additions & 4 deletions lib/storage/bloc/storage_detail/storage_detail_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:equatable/equatable.dart';
import 'package:smarthome/core/core.dart';
import 'package:smarthome/storage/model/storage.dart';
import 'package:smarthome/storage/repository/storage_repository.dart';
import 'package:smarthome/utils/constants.dart';
import 'package:smarthome/utils/exceptions.dart';

part 'storage_detail_event.dart';
Expand Down Expand Up @@ -34,15 +35,15 @@ class StorageDetailBloc extends Bloc<StorageDetailEvent, StorageDetailState> {
// 如果不需要刷新,不是首次启动,或遇到错误,并且有下一页
// 则获取下一页
final storage = state.storage;
if (storage.id == '') {
if (storage.id == homeStorage.id) {
final results = await storageRepository.rootStorage(
after: state.storagePageInfo.endCursor,
cache: false,
);
emit(
state.copyWith(
storage: state.storage.copyWith(
name: '家',
name: homeStorage.name,
children: state.storage.children! + results.item1,
items: [],
),
Expand All @@ -69,14 +70,14 @@ class StorageDetailBloc extends Bloc<StorageDetailEvent, StorageDetailState> {
}
} else {
// 其他情况根据设置看是否需要打开缓存,并获取第一页
if (event.id == '') {
if (event.id == homeStorage.id) {
final results =
await storageRepository.rootStorage(cache: event.cache);
emit(
state.copyWith(
status: StorageDetailStatus.success,
storage: state.storage.copyWith(
name: '家',
name: homeStorage.name,
children: results.item1,
items: [],
),
Expand Down
3 changes: 2 additions & 1 deletion lib/storage/view/home_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:smarthome/routers/delegate.dart';
import 'package:smarthome/storage/storage.dart';
import 'package:smarthome/storage/view/item_edit_page.dart';
import 'package:smarthome/storage/view/widgets/search_icon_button.dart';
import 'package:smarthome/utils/constants.dart';
import 'package:smarthome/utils/date_format_extension.dart';
import 'package:smarthome/widgets/center_loading_indicator.dart';
import 'package:smarthome/widgets/error_message_button.dart';
Expand Down Expand Up @@ -149,7 +150,7 @@ class _StorageHomeBody extends StatelessWidget {
child: InkWell(
onTap: () {
MyRouterDelegate.of(context)
.push(StorageDetailPage(storageId: ''));
.push(StorageDetailPage(storageId: homeStorage.id));
},
child: Card(
color: Theme.of(context).colorScheme.secondary,
Expand Down
3 changes: 2 additions & 1 deletion lib/storage/view/item_edit_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:smarthome/storage/bloc/blocs.dart';
import 'package:smarthome/storage/model/models.dart';
import 'package:smarthome/storage/view/widgets/storage_picker_formfield.dart';
import 'package:smarthome/utils/constants.dart';
import 'package:smarthome/utils/date_format_extension.dart';
import 'package:smarthome/utils/show_snack_bar.dart';
import 'package:smarthome/widgets/rounded_raised_button.dart';
Expand Down Expand Up @@ -139,7 +140,7 @@ class _ItemEditPageState extends State<ItemEditPage> {
validator: (value) {
if (value == null) {
return '请选择一个存放位置';
} else if (value.id == '') {
} else if (value.id == homeStorage.id) {
return '家不能作为存放位置';
}
return null;
Expand Down
8 changes: 5 additions & 3 deletions lib/storage/view/storage_datail_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:smarthome/storage/view/storage_edit_page.dart';
import 'package:smarthome/storage/view/widgets/add_storage_icon_button.dart';
import 'package:smarthome/storage/view/widgets/search_icon_button.dart';
import 'package:smarthome/storage/view/widgets/storage_item_list.dart';
import 'package:smarthome/utils/constants.dart';
import 'package:smarthome/utils/show_snack_bar.dart';
import 'package:smarthome/widgets/center_loading_indicator.dart';
import 'package:smarthome/widgets/error_message_button.dart';
Expand Down Expand Up @@ -101,7 +102,7 @@ class StorageDetailScreen extends StatelessWidget {

AppBar _buildAppBar(BuildContext context, StorageDetailState state) {
final paths = state.storage.ancestors ?? [];
if (!paths.contains(state.storage) && state.storage.id != '') {
if (!paths.contains(state.storage) && state.storage.id != homeStorage.id) {
// 防止重复添加相同名称的位置
// 因为无限列表重新获取时,位置对象虽然名字不会变,但是内容改变
if (paths.isEmpty || paths.last.name != state.storage.name) {
Expand All @@ -115,7 +116,7 @@ class StorageDetailScreen extends StatelessWidget {
storage: state.storage,
),
const SearchIconButton(),
if (state.storage.id != '')
if (state.storage.id != homeStorage.id)
PopupMenuButton<Menu>(
onSelected: (value) async {
if (value == Menu.edit) {
Expand Down Expand Up @@ -263,7 +264,8 @@ class StorageDetailScreen extends StatelessWidget {

Widget? _buildFloatingActionButton(
BuildContext context, StorageDetailState state) {
if (state.status == StorageDetailStatus.success && state.storage.id != '') {
if (state.status == StorageDetailStatus.success &&
state.storage.id != homeStorage.id) {
return FloatingActionButton(
tooltip: '添加物品',
onPressed: () async {
Expand Down
12 changes: 7 additions & 5 deletions lib/storage/view/storage_edit_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:smarthome/storage/bloc/blocs.dart';
import 'package:smarthome/storage/model/models.dart';
import 'package:smarthome/storage/view/widgets/storage_picker_formfield.dart';
import 'package:smarthome/utils/constants.dart';
import 'package:smarthome/utils/show_snack_bar.dart';
import 'package:smarthome/widgets/rounded_raised_button.dart';

Expand Down Expand Up @@ -40,9 +41,10 @@ class _StorageEditPageState extends State<StorageEditPage> {
} else {
_nameController = TextEditingController();
_descriptionController = TextEditingController();
// 如果是家的话,需要特殊处理,因为之前家的 parent 是空字符串
// 如果是家的话,需要特殊处理,因为家的 id 是空字符串
// 服务器只能接受 null
parentId = widget.storage?.id != '' ? widget.storage?.id : null;
parentId =
widget.storage?.id != homeStorage.id ? widget.storage?.id : null;
}

_nameFocusNode = FocusNode();
Expand Down Expand Up @@ -145,19 +147,19 @@ class _StorageEditPageState extends State<StorageEditPage> {
context, _nameFocusNode!, _descriptionFocusNode);
},
),
// FIXME: 无法选择 家 这个位置
StorageFormField(
decoration: const InputDecoration(
labelText: '属于',
),
autovalidateMode: AutovalidateMode.onUserInteraction,
onChanged: (Storage? value) {
// 家的 id 是空字符串,服务器只能接受 null
if (value != null) {
parentId = value.id;
parentId = value.id == homeStorage.id ? null : value.id;
}
},
initialValue: widget.isEditing
? widget.storage!.parent
? widget.storage!.parent ?? homeStorage
: widget.storage,
validator: (value) {
if (value == null) {
Expand Down
41 changes: 20 additions & 21 deletions lib/storage/view/widgets/storage_picker_formfield.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_treeview/flutter_treeview.dart';
import 'package:smarthome/storage/storage.dart';
import 'package:smarthome/utils/constants.dart';
import 'package:smarthome/widgets/substring_highlight.dart';

List<Node> childrenNode(String? key, List<Storage> storages) {
final children = storages.where((storage) => storage.parent?.id == key);
final children = storages
.where((storage) => (storage.parent?.id ?? homeStorage.id) == key);
if (children.isNotEmpty) {
final childNodes = children
.map(
Expand All @@ -23,24 +25,15 @@ List<Node> childrenNode(String? key, List<Storage> storages) {
}

List<Node> generateNodes(List<Storage> storages) {
List<Node> nodes = [];
for (final storage in storages) {
// 排除父节点在列表中的位置
// 因为从父节点找下来的时候自然会找到这个位置
final storageIndex =
storages.indexWhere((element) => element.id == storage.parent?.id);
if (storageIndex == -1) {
nodes.add(
Node(
key: storage.id,
label: storage.name,
expanded: true,
children: childrenNode(storage.id, storages),
),
);
}
}
return nodes;
// 家应该最为根节点,这样才能选中
return [
Node(
key: homeStorage.id,
label: homeStorage.name,
expanded: true,
children: childrenNode('', storages),
)
];
}

class StorageDialog extends StatefulWidget {
Expand Down Expand Up @@ -111,8 +104,14 @@ class _StorageDialogState extends State<StorageDialog> {
),
allowParentSelect: true,
onNodeTap: (node) {
final storage =
widget.storages.firstWhere((storage) => storage.id == node);
// 针对家做特殊处理,家应该为一个 id 为 '' 的 storage
Storage storage;
if (node == homeStorage.id) {
storage = homeStorage;
} else {
storage = widget.storages
.firstWhere((storage) => storage.id == node);
}
Navigator.of(context).pop(storage);
},
),
Expand Down
4 changes: 4 additions & 0 deletions lib/utils/constants.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import 'package:smarthome/storage/model/models.dart';

/// 家位置
const Storage homeStorage = Storage(id: '', name: '家');

0 comments on commit 9a05f93

Please sign in to comment.