forked from lxm_flutter/FlutterUnit
✨ 修复收藏集详情页bug
This commit is contained in:
@@ -1,7 +1,12 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:flutter_unit/app/api/category_api.dart';
|
||||
import 'package:flutter_unit/app/res/style/unit_color.dart';
|
||||
import 'package:flutter_unit/app/utils/color_utils.dart';
|
||||
import 'package:flutter_unit/app/utils/http_utils/result_bean.dart';
|
||||
import 'package:flutter_unit/model/category_model.dart';
|
||||
import 'package:flutter_unit/repositories/itf/category_repository.dart';
|
||||
import 'package:flutter_unit/storage/po/category_po.dart';
|
||||
|
||||
@@ -15,31 +20,33 @@ import 'category_state.dart';
|
||||
class CategoryBloc extends Bloc<CategoryEvent, CategoryState> {
|
||||
final CategoryRepository repository;
|
||||
|
||||
CategoryBloc({@required this.repository}):super(CategoryEmptyState());
|
||||
|
||||
CategoryBloc({@required this.repository}) : super(const CategoryEmptyState());
|
||||
|
||||
@override
|
||||
Stream<CategoryState> mapEventToState(CategoryEvent event) async* {
|
||||
if (event is EventLoadCategory) {
|
||||
// 使用 repository 加载 收藏集数据
|
||||
final category = await repository.loadCategories();
|
||||
yield category.isEmpty
|
||||
? CategoryEmptyState()
|
||||
? const CategoryEmptyState()
|
||||
: CategoryLoadedState(category);
|
||||
}
|
||||
|
||||
if (event is EventDeleteCategory) {
|
||||
await repository.deleteCategory(event.id);
|
||||
add(EventLoadCategory());
|
||||
add(const EventLoadCategory());
|
||||
}
|
||||
|
||||
if (event is EventToggleWidget) {
|
||||
await repository.toggleCategory(event.categoryId, event.widgetId);
|
||||
add(EventLoadCategory());
|
||||
add(const EventLoadCategory());
|
||||
}
|
||||
|
||||
if (event is EventAddCategory) {
|
||||
CategoryPo categoryPo = CategoryPo(
|
||||
name: event.name,
|
||||
color: event.color ?? ColorUtils.colorString(UnitColor.collectColorSupport[0]),
|
||||
color: event.color ??
|
||||
ColorUtils.colorString(UnitColor.collectColorSupport[0]),
|
||||
info: event.info ?? '这里什么都没有...',
|
||||
created: DateTime.now(),
|
||||
updated: DateTime.now());
|
||||
@@ -47,10 +54,10 @@ class CategoryBloc extends Bloc<CategoryEvent, CategoryState> {
|
||||
final success = await repository.addCategory(categoryPo);
|
||||
|
||||
if (success) {
|
||||
yield AddCategorySuccess();
|
||||
add(EventLoadCategory());
|
||||
yield const AddCategorySuccess();
|
||||
add(const EventLoadCategory());
|
||||
} else {
|
||||
yield AddCategoryFailed();
|
||||
yield const AddCategoryFailed();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -60,7 +67,8 @@ class CategoryBloc extends Bloc<CategoryEvent, CategoryState> {
|
||||
name: event.name,
|
||||
priority: event.priority ?? 0,
|
||||
image: event.image ?? '',
|
||||
color: event.color ?? ColorUtils.colorString(UnitColor.collectColorSupport[0]),
|
||||
color: event.color ??
|
||||
ColorUtils.colorString(UnitColor.collectColorSupport[0]),
|
||||
info: event.info ?? '这里什么都没有...',
|
||||
updated: DateTime.now());
|
||||
|
||||
@@ -68,10 +76,19 @@ class CategoryBloc extends Bloc<CategoryEvent, CategoryState> {
|
||||
|
||||
if (success) {
|
||||
// yield AddCategorySuccess();
|
||||
add(EventLoadCategory());
|
||||
add(const EventLoadCategory());
|
||||
} else {
|
||||
// yield AddCategoryFailed();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
List<CategoryModel> get categories {
|
||||
if(state is CategoryLoadedState){
|
||||
return (state as CategoryLoadedState).categories;
|
||||
}else{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,48 +3,53 @@ import 'package:flutter/material.dart';
|
||||
|
||||
/// create by 张风捷特烈 on 2020-04-21
|
||||
/// contact me by email 1981462002@qq.com
|
||||
/// 说明:
|
||||
/// 说明: 收藏集相关事件
|
||||
|
||||
abstract class CategoryEvent extends Equatable{
|
||||
const CategoryEvent();
|
||||
@override
|
||||
List<Object> get props => [];
|
||||
}
|
||||
|
||||
// 加载 收藏集 事件
|
||||
class EventLoadCategory extends CategoryEvent{
|
||||
const EventLoadCategory();
|
||||
@override
|
||||
List<Object> get props => [];
|
||||
}
|
||||
|
||||
// 将一个 widget 添加/移除 收藏集
|
||||
class EventToggleWidget extends CategoryEvent{
|
||||
final int widgetId;
|
||||
final int categoryId;
|
||||
EventToggleWidget({this.widgetId, this.categoryId});
|
||||
const EventToggleWidget({this.widgetId, this.categoryId});
|
||||
|
||||
@override
|
||||
List<Object> get props => [widgetId,categoryId];
|
||||
}
|
||||
|
||||
// 删除 收藏集
|
||||
class EventDeleteCategory extends CategoryEvent{
|
||||
final int id;
|
||||
|
||||
EventDeleteCategory({@required this.id});
|
||||
const EventDeleteCategory({@required this.id});
|
||||
|
||||
@override
|
||||
List<Object> get props => [id];
|
||||
}
|
||||
|
||||
|
||||
// 添加 收藏集
|
||||
class EventAddCategory extends CategoryEvent{
|
||||
final String name;
|
||||
final String info;
|
||||
final String color;
|
||||
|
||||
EventAddCategory({@required this.name, this.info, this.color});
|
||||
const EventAddCategory({@required this.name, this.info, this.color});
|
||||
|
||||
@override
|
||||
List<Object> get props => [name,info,color];
|
||||
}
|
||||
|
||||
// 更新 收藏集
|
||||
class EventUpdateCategory extends CategoryEvent {
|
||||
final int id;
|
||||
final String name;
|
||||
@@ -53,8 +58,8 @@ class EventUpdateCategory extends CategoryEvent {
|
||||
final int priority;
|
||||
final String image;
|
||||
|
||||
EventUpdateCategory({@required this.name, this.info, this.color,this.priority,this.image,this.id});
|
||||
const EventUpdateCategory({@required this.name, this.info, this.color,this.priority,this.image,this.id});
|
||||
|
||||
@override
|
||||
List<Object> get props => [name, info, color,priority,image,id];
|
||||
}
|
||||
}
|
||||
@@ -3,32 +3,33 @@ import 'package:flutter_unit/model/category_model.dart';
|
||||
|
||||
/// create by 张风捷特烈 on 2020-04-21
|
||||
/// contact me by email 1981462002@qq.com
|
||||
/// 说明:
|
||||
/// 说明:
|
||||
|
||||
class CategoryState extends Equatable {
|
||||
const CategoryState();
|
||||
|
||||
class CategoryState extends Equatable{
|
||||
@override
|
||||
List<Object> get props => [];
|
||||
|
||||
}
|
||||
|
||||
class CategoryLoadedState extends CategoryState {
|
||||
final List<CategoryModel> categories;
|
||||
|
||||
CategoryLoadedState(this.categories);
|
||||
List<Object> get props => [categories];
|
||||
const CategoryLoadedState(this.categories);
|
||||
|
||||
List<Object> get props => [categories];
|
||||
}
|
||||
|
||||
class CategoryEmptyState extends CategoryState{
|
||||
class CategoryEmptyState extends CategoryState {
|
||||
const CategoryEmptyState();
|
||||
|
||||
List<Object> get props => [];
|
||||
}
|
||||
|
||||
|
||||
class AddCategorySuccess extends CategoryState{
|
||||
|
||||
class AddCategorySuccess extends CategoryState {
|
||||
const AddCategorySuccess();
|
||||
}
|
||||
|
||||
|
||||
class AddCategoryFailed extends CategoryState{
|
||||
|
||||
class AddCategoryFailed extends CategoryState {
|
||||
const AddCategoryFailed();
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ class Panel extends StatelessWidget {
|
||||
Widget build(BuildContext context) {
|
||||
return Container(
|
||||
child: child,
|
||||
padding: EdgeInsets.all(10),
|
||||
padding: const EdgeInsets.all(10),
|
||||
decoration: BoxDecoration(
|
||||
color: color ?? Color(0xffF6F8FA),
|
||||
borderRadius: BorderRadius.all(Radius.circular(radius))),
|
||||
|
||||
@@ -9,6 +9,7 @@ import 'package:flutter_unit/views/components/permanent/circle_text.dart';
|
||||
import 'package:flutter_unit/views/components/permanent/feedback_widget.dart';
|
||||
import 'package:flutter_unit/model/category_model.dart';
|
||||
import 'package:flutter_unit/model/widget_model.dart';
|
||||
import 'package:flutter_unit/views/widgets/exp/render_object_unit.dart';
|
||||
|
||||
/// create by 张风捷特烈 on 2020-04-22
|
||||
/// contact me by email 1981462002@qq.com
|
||||
@@ -28,16 +29,14 @@ class CategoryShow extends StatelessWidget {
|
||||
if (state is CategoryWidgetLoadedState) {
|
||||
return _buildWidgetList(state.widgets);
|
||||
}
|
||||
return Container();
|
||||
return const SizedBox();
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildWidgetList(List<WidgetModel> widgets) {
|
||||
return ListView.separated(
|
||||
separatorBuilder: (_, index) => Divider(
|
||||
height: 1,
|
||||
),
|
||||
separatorBuilder: (_, index) => const Divider(height: 1),
|
||||
itemBuilder: (context, index) => Dismissible(
|
||||
direction: DismissDirection.endToStart,
|
||||
key: ValueKey(widgets[index].id),
|
||||
@@ -45,7 +44,7 @@ class CategoryShow extends StatelessWidget {
|
||||
padding: EdgeInsets.only(right: 20),
|
||||
alignment: Alignment.centerRight,
|
||||
color: Colors.red,
|
||||
child: Icon(
|
||||
child: const Icon(
|
||||
CupertinoIcons.delete_solid,
|
||||
color: Colors.white,
|
||||
size: 30,
|
||||
@@ -60,7 +59,9 @@ class CategoryShow extends StatelessWidget {
|
||||
child: FeedbackWidget(
|
||||
duration: Duration(milliseconds: 200),
|
||||
onPressed: () => _toDetailPage(context, widgets[index]),
|
||||
child: SimpleWidgetItem(
|
||||
child:
|
||||
// Container(height: 60,)
|
||||
SimpleWidgetItem(
|
||||
data: widgets[index],
|
||||
)),
|
||||
),
|
||||
@@ -86,21 +87,8 @@ class SimpleWidgetItem extends StatelessWidget {
|
||||
height: 75,
|
||||
child: Row(
|
||||
children: <Widget>[
|
||||
SizedBox(
|
||||
width: 10,
|
||||
),
|
||||
Column(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
|
||||
children: <Widget>[
|
||||
_buildLeading(),
|
||||
StarScore(
|
||||
star: Star(
|
||||
emptyColor: Colors.white, size: 12, fillColor: data.color),
|
||||
score: data.lever,
|
||||
),
|
||||
],
|
||||
),
|
||||
SizedBox(
|
||||
_buildLeading(),
|
||||
const SizedBox(
|
||||
width: 20,
|
||||
),
|
||||
Expanded(
|
||||
@@ -116,45 +104,51 @@ class SimpleWidgetItem extends StatelessWidget {
|
||||
}
|
||||
|
||||
Widget _buildTitle() {
|
||||
return Text(data.name,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
style: TextStyle(
|
||||
fontSize: 17,
|
||||
fontWeight: FontWeight.bold,
|
||||
shadows: [Shadow(color: Colors.white, offset: Offset(.3, .3))]));
|
||||
return Row(
|
||||
children: [
|
||||
Text(data.name,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
style: const TextStyle(
|
||||
fontSize: 17,
|
||||
fontWeight: FontWeight.bold,
|
||||
shadows: [
|
||||
Shadow(color: Colors.white, offset: Offset(.3, .3))
|
||||
])),
|
||||
const SizedBox(width: 15),
|
||||
StarScore(
|
||||
star: Star(emptyColor: Colors.white, size: 12, fillColor: data.color),
|
||||
score: data.lever,
|
||||
)
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildLeading() => Padding(
|
||||
padding: const EdgeInsets.only(left: 5, right: 5),
|
||||
child: Hero(
|
||||
tag: "hero_widget_image_${data.id}",
|
||||
child: data.image == null
|
||||
? Material(
|
||||
color: Colors.transparent,
|
||||
child: CircleText(
|
||||
text: data.name,
|
||||
size: 50,
|
||||
color: data.color,
|
||||
),
|
||||
)
|
||||
: CircleImage(
|
||||
image: data.image,
|
||||
size: 50,
|
||||
child: data.image == null
|
||||
? Material(
|
||||
color: Colors.transparent,
|
||||
child: CircleText(
|
||||
text: data.name,
|
||||
size: 60,
|
||||
color: data.color,
|
||||
),
|
||||
),
|
||||
)
|
||||
: CircleImage(
|
||||
image: data.image,
|
||||
size: 60,
|
||||
),
|
||||
);
|
||||
|
||||
Widget _buildSummary() {
|
||||
return Container(
|
||||
child: Text(
|
||||
data.info,
|
||||
maxLines: 2,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
style: TextStyle(
|
||||
color: Colors.grey[600],
|
||||
fontSize: 14,
|
||||
shadows: [Shadow(color: Colors.white, offset: Offset(.5, .5))]),
|
||||
),
|
||||
return Text(
|
||||
data.info,
|
||||
maxLines: 2,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
style: const TextStyle(
|
||||
color: Color(0xFF757575),
|
||||
fontSize: 14,
|
||||
shadows: [Shadow(color: Colors.white, offset: Offset(.5, .5))]),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ import 'package:flutter_unit/views/components/project/dialogs/delete_category_di
|
||||
import 'package:flutter_unit/views/components/project/items/category_list_item.dart';
|
||||
|
||||
import 'edit_category_panel.dart';
|
||||
import 'empty_category.dart';
|
||||
|
||||
class CategoryPage extends StatelessWidget {
|
||||
|
||||
@@ -30,7 +31,6 @@ class CategoryPage extends StatelessWidget {
|
||||
SliverOverlapInjector(
|
||||
handle: NestedScrollView.sliverOverlapAbsorberHandleFor(ctx),
|
||||
),
|
||||
|
||||
_buildContent(context, state),
|
||||
SliverToBoxAdapter(
|
||||
child: NoMoreWidget(),
|
||||
@@ -38,7 +38,7 @@ class CategoryPage extends StatelessWidget {
|
||||
],
|
||||
);
|
||||
}
|
||||
return Container();
|
||||
return EmptyCategory();
|
||||
});
|
||||
}
|
||||
|
||||
@@ -123,8 +123,7 @@ class CategoryPage extends StatelessWidget {
|
||||
}
|
||||
|
||||
_toDetailPage(BuildContext context, CategoryModel model) {
|
||||
BlocProvider.of<CategoryWidgetBloc>(context)
|
||||
.add(EventLoadCategoryWidget(model.id));
|
||||
BlocProvider.of<CategoryWidgetBloc>(context).add(EventLoadCategoryWidget(model.id));
|
||||
Navigator.pushNamed(context, UnitRouter.category_show, arguments: model);
|
||||
}
|
||||
|
||||
|
||||
64
lib/views/pages/category/empty_category.dart
Normal file
64
lib/views/pages/category/empty_category.dart
Normal file
@@ -0,0 +1,64 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:flutter_unit/app/res/color_unit.dart';
|
||||
import 'package:flutter_unit/blocs/bloc_exp.dart';
|
||||
import 'package:flutter_unit/views/components/permanent/feedback_widget.dart';
|
||||
import 'package:flutter_unit/views/components/permanent/panel.dart';
|
||||
import 'package:sqflite/sqflite.dart';
|
||||
import 'package:path/path.dart' as path;
|
||||
/// create by 张风捷特烈 on 2021/2/25
|
||||
/// contact me by email 1981462002@qq.com
|
||||
/// 说明:
|
||||
|
||||
class EmptyCategory extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Center(
|
||||
child: Panel(
|
||||
radius: 15,
|
||||
color: ColorUnit.warning_color.withOpacity(0.3),
|
||||
child: Wrap(
|
||||
alignment: WrapAlignment.center,
|
||||
crossAxisAlignment: WrapCrossAlignment.center,
|
||||
direction: Axis.vertical,
|
||||
spacing: 5,
|
||||
children: [
|
||||
const Text(
|
||||
" 您还没有收藏集! ",
|
||||
style: TextStyle(fontSize: 18,color: ColorUnit.head_text_color),
|
||||
),
|
||||
SizedBox(
|
||||
width: 10,
|
||||
),
|
||||
FeedbackWidget(
|
||||
onPressed: ()=>_recallDatabase(context),
|
||||
child: Icon(
|
||||
Icons.refresh,
|
||||
textDirection: TextDirection.rtl,
|
||||
color: Colors.blue,
|
||||
size: 36,
|
||||
),
|
||||
),
|
||||
const Text(
|
||||
"恢复默认",
|
||||
style: TextStyle(fontSize: 14,color: ColorUnit.input_hit_text_color),
|
||||
),
|
||||
],
|
||||
),
|
||||
));
|
||||
}
|
||||
|
||||
_recallDatabase(BuildContext context) async{
|
||||
String databasesPath = await getDatabasesPath();
|
||||
String dbPath = path.join(databasesPath, "flutter.db");
|
||||
ByteData data = await rootBundle.load(path.join("assets", "flutter.db"));
|
||||
List<int> bytes =
|
||||
data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
|
||||
await File(dbPath).writeAsBytes(bytes, flush: true);
|
||||
print("==== debug ===== assets ======拷贝完成====");
|
||||
BlocProvider.of<CategoryBloc>(context).add(EventLoadCategory());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user