forked from toly1994328/FlutterUnit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcategory_end_drawer.dart
158 lines (142 loc) · 4.71 KB
/
category_end_drawer.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_unit/blocs/category/category_bloc.dart';
import 'package:flutter_unit/blocs/category/category_state.dart';
import 'package:flutter_unit/blocs/category_widget/category_widget_bloc.dart';
import 'package:flutter_unit/blocs/category_widget/category_widget_event.dart';
import 'package:flutter_unit/components/permanent/circle.dart';
import 'package:flutter_unit/components/permanent/panel.dart';
import 'package:flutter_unit/model/category_model.dart';
import 'package:flutter_unit/model/widget_model.dart';
import 'package:flutter_unit/repositories/itf/category_repository.dart';
import 'package:flutter_unit/views/common/unit_drawer_header.dart';
/// create by 张风捷特烈 on 2020-04-22
/// contact me by email 1981462002@qq.com
/// 说明:
const _kTextShadowStyle= TextStyle(color: Colors.grey, shadows: [
Shadow(
color: Colors.white, offset: Offset(.5, .5), blurRadius: .5)
]);
class CategoryEndDrawer extends StatelessWidget {
final WidgetModel widget;
CategoryEndDrawer({this.widget});
@override
Widget build(BuildContext context) {
return Drawer(
child: Container(
child: ListView(padding: EdgeInsets.zero, children: <Widget>[
UnitDrawerHeader(color: Theme.of(context).primaryColor),
Padding(
padding: const EdgeInsets.all(10.0),
child: Row(
children: <Widget>[
Circle(color: widget.color,),
SizedBox(width: 10,),
Text(widget.name)
],
),
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 15.0),
child: Panel(
child: Text(
widget.info,
style:_kTextShadowStyle
),
),
),
Divider(),
_buildTitle(context),
Divider(),
CategoryInfo(widget.id)
])),
);
}
Widget _buildTitle(BuildContext context) {
return Padding(
padding: const EdgeInsets.only(top: 5.0, bottom: 8),
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Circle(
color: Theme.of(context).primaryColor,
radius: 5,
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 8),
child: Text(
'当前组件收藏情况',
style: TextStyle(fontSize: 16, shadows: [
Shadow(
color: Colors.white, offset: Offset(.5, .5), blurRadius: 1)
]),
),
),
Circle(
color: Theme.of(context).primaryColor,
radius: 5,
),
],
),
);
}
}
class CategoryInfo extends StatefulWidget {
final int id;
CategoryInfo(this.id);
@override
_CategoryInfoState createState() => _CategoryInfoState();
}
class _CategoryInfoState extends State<CategoryInfo> {
List<int> categoryIds = [];
List<CategoryModel> _categories = [];
@override
void didChangeDependencies() {
_loadCategoryIds();
super.didChangeDependencies();
}
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 10.0),
child: Wrap(
alignment: WrapAlignment.spaceBetween,
spacing: 5,
children: categories.map((e) => _buildItem(e)).toList(),
),
);
}
Widget _buildItem(CategoryModel category) {
bool inHere = categoryIds.contains(category.id);
return FilterChip(
backgroundColor: Theme.of(context).primaryColor.withAlpha(33),
selectedColor: Colors.orange.withAlpha(120),
shadowColor: Theme.of(context).primaryColor,
elevation: 1,
avatar: Circle(
radius: 13,
color: category.color,
),
selected: inHere,
label: Text(category.name),
onSelected: (v) async {
await repository.toggleCategory(category.id, widget.id);
_loadCategoryIds();
BlocProvider.of<CategoryWidgetBloc>(context).add(EventLoadCategoryWidget(category.id));
});
}
CategoryRepository get repository => BlocProvider.of<CategoryBloc>(context).repository;
List<CategoryModel> get categories {
var state = BlocProvider.of<CategoryBloc>(context).state;
if (state is CategoryLoadedState) {
_categories = state.categories;
}
return _categories;
}
void _loadCategoryIds() async {
categoryIds = await repository.getCategoryByWidget(widget.id);
if(mounted)
setState(() {});
}
}