Skip to content

Commit

Permalink
format code
Browse files Browse the repository at this point in the history
  • Loading branch information
xSILENCEx committed May 30, 2024
1 parent 6b2a53c commit f502047
Show file tree
Hide file tree
Showing 16 changed files with 240 additions and 141 deletions.
40 changes: 29 additions & 11 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,12 @@ class _HomePageState extends State<HomePage> {
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
IconButton(onPressed: () => Navigator.pop(context, true), icon: const Icon(Icons.check)),
IconButton(onPressed: () => Navigator.pop(context, false), icon: const Icon(Icons.clear)),
IconButton(
onPressed: () => Navigator.pop(context, true),
icon: const Icon(Icons.check)),
IconButton(
onPressed: () => Navigator.pop(context, false),
icon: const Icon(Icons.clear)),
],
),
],
Expand Down Expand Up @@ -145,7 +149,8 @@ class _HomePageState extends State<HomePage> {
StackImageItem(
size: const Size(300, 85),
content: ImageItemContent(
url: 'https://files.flutter-io.cn/images/branding/flutterlogo/flutter-cn-logo.png',
url:
'https://files.flutter-io.cn/images/branding/flutterlogo/flutter-cn-logo.png',
),
),
);
Expand All @@ -158,7 +163,8 @@ class _HomePageState extends State<HomePage> {

/// Add custom item
void _addCustomItem() {
final Color color = Colors.primaries[Random().nextInt(Colors.primaries.length)];
final Color color =
Colors.primaries[Random().nextInt(Colors.primaries.length)];
_boardController.addItem(
ColorStackItem(
size: const Size.square(100),
Expand All @@ -169,9 +175,12 @@ class _HomePageState extends State<HomePage> {

/// Add custom item
Future<void> _generateFromJson() async {
final String jsonString = (await Clipboard.getData(Clipboard.kTextPlain))?.text ?? '';
final String jsonString =
(await Clipboard.getData(Clipboard.kTextPlain))?.text ?? '';
if (jsonString.isEmpty) {
_showAlertDialog(title: 'Clipboard is empty', content: 'Please copy the json string to the clipboard first');
_showAlertDialog(
title: 'Clipboard is empty',
content: 'Please copy the json string to the clipboard first');
return;
}
try {
Expand Down Expand Up @@ -227,7 +236,9 @@ class _HomePageState extends State<HomePage> {
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
IconButton(onPressed: () => Navigator.pop(context), icon: const Icon(Icons.check)),
IconButton(
onPressed: () => Navigator.pop(context),
icon: const Icon(Icons.check)),
],
),
],
Expand Down Expand Up @@ -285,13 +296,20 @@ class _HomePageState extends State<HomePage> {
child: Row(
children: <Widget>[
const SizedBox(width: 25),
FloatingActionButton(onPressed: _addTextItem, child: const Icon(Icons.border_color)),
FloatingActionButton(
onPressed: _addTextItem,
child: const Icon(Icons.border_color)),
_spacer,
FloatingActionButton(onPressed: _addImageItem, child: const Icon(Icons.image)),
FloatingActionButton(
onPressed: _addImageItem, child: const Icon(Icons.image)),
_spacer,
FloatingActionButton(onPressed: _addDrawItem, child: const Icon(Icons.color_lens)),
FloatingActionButton(
onPressed: _addDrawItem,
child: const Icon(Icons.color_lens)),
_spacer,
FloatingActionButton(onPressed: _addCustomItem, child: const Icon(Icons.add_box)),
FloatingActionButton(
onPressed: _addCustomItem,
child: const Icon(Icons.add_box)),
],
),
),
Expand Down
50 changes: 33 additions & 17 deletions lib/src/core/stack_board_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ class StackBoardController extends SafeValueNotifier<StackConfig> {

final Map<String, int> _indexMap = <String, int>{};

static final StackBoardController _defaultController = StackBoardController(tag: 'def');
static final StackBoardController _defaultController =
StackBoardController(tag: 'def');

List<StackItem<StackItemContent>> get innerData => value.data;

Expand All @@ -70,7 +71,8 @@ class StackBoardController extends SafeValueNotifier<StackConfig> {

/// * 重排索引
/// * reorder index
List<StackItem<StackItemContent>> _reorder(List<StackItem<StackItemContent>> data) {
List<StackItem<StackItemContent>> _reorder(
List<StackItem<StackItemContent>> data) {
for (int i = 0; i < data.length; i++) {
_indexMap[data[i].id] = i;
}
Expand All @@ -86,7 +88,8 @@ class StackBoardController extends SafeValueNotifier<StackConfig> {
return;
}

final List<StackItem<StackItemContent>> data = List<StackItem<StackItemContent>>.from(innerData);
final List<StackItem<StackItemContent>> data =
List<StackItem<StackItemContent>>.from(innerData);

// Initial offset
// `TODO`: transform this to parameters
Expand Down Expand Up @@ -114,7 +117,7 @@ class StackBoardController extends SafeValueNotifier<StackConfig> {
}

data.add(item.copyWith(
offset: Offset(
offset: Offset(
item.size.width / 2 +
baseOffset +
deltaOffsetMultiplicator * deltaOffset,
Expand Down Expand Up @@ -150,7 +153,8 @@ class StackBoardController extends SafeValueNotifier<StackConfig> {
/// * 移除 item
/// * remove item
void removeItem(StackItem<StackItemContent> item) {
final List<StackItem<StackItemContent>> data = List<StackItem<StackItemContent>>.from(innerData);
final List<StackItem<StackItemContent>> data =
List<StackItem<StackItemContent>>.from(innerData);

data.remove(item);
_indexMap.remove(item.id);
Expand All @@ -165,7 +169,8 @@ class StackBoardController extends SafeValueNotifier<StackConfig> {
void removeById(String id) {
if (!_indexMap.containsKey(id)) return;

final List<StackItem<StackItemContent>> data = List<StackItem<StackItemContent>>.from(innerData);
final List<StackItem<StackItemContent>> data =
List<StackItem<StackItemContent>>.from(innerData);

data.removeAt(_indexMap[id]!);
_indexMap.remove(id);
Expand All @@ -187,7 +192,8 @@ class StackBoardController extends SafeValueNotifier<StackConfig> {
return;
}

final List<StackItem<StackItemContent>> data = List<StackItem<StackItemContent>>.from(innerData);
final List<StackItem<StackItemContent>> data =
List<StackItem<StackItemContent>>.from(innerData);

data.removeAt(index);

Expand All @@ -208,7 +214,8 @@ class StackBoardController extends SafeValueNotifier<StackConfig> {
/// * 取消选中所有 item
/// * unselect all items
void unSelectAll() {
final List<StackItem<StackItemContent>> data = List<StackItem<StackItemContent>>.from(innerData);
final List<StackItem<StackItemContent>> data =
List<StackItem<StackItemContent>>.from(innerData);

for (int i = 0; i < data.length; i++) {
final StackItem<StackItemContent> item = data[i];
Expand All @@ -226,8 +233,9 @@ class StackBoardController extends SafeValueNotifier<StackConfig> {
void updateBasic(String id,
{Size? size, Offset? offset, double? angle, StackItemStatus? status}) {
if (!_indexMap.containsKey(id)) return;

final List<StackItem<StackItemContent>> data = List<StackItem<StackItemContent>>.from(innerData);

final List<StackItem<StackItemContent>> data =
List<StackItem<StackItemContent>>.from(innerData);

data[_indexMap[id]!] = data[_indexMap[id]!].copyWith(
size: size,
Expand All @@ -244,7 +252,8 @@ class StackBoardController extends SafeValueNotifier<StackConfig> {
void updateItem(StackItem<StackItemContent> item) {
if (!_indexMap.containsKey(item.id)) return;

final List<StackItem<StackItemContent>> data = List<StackItem<StackItemContent>>.from(innerData);
final List<StackItem<StackItemContent>> data =
List<StackItem<StackItemContent>>.from(innerData);

data[_indexMap[item.id]!] = item;

Expand All @@ -263,21 +272,26 @@ class StackBoardController extends SafeValueNotifier<StackConfig> {
Map<String, dynamic>? getSelectedData() {
return innerData
.firstWhereOrNull(
(StackItem<StackItemContent> item) => item.status == StackItemStatus.selected,
(StackItem<StackItemContent> item) =>
item.status == StackItemStatus.selected,
)
?.toJson();
}

/// * 通过 id 获取数据 json
/// * get data json by id
Map<String, dynamic>? getDataById(String id) {
return innerData.firstWhereOrNull((StackItem<StackItemContent> item) => item.id == id)?.toJson();
return innerData
.firstWhereOrNull((StackItem<StackItemContent> item) => item.id == id)
?.toJson();
}

/// * 通过类型获取数据 json 列表
/// * get data json list by type
List<Map<String, dynamic>> getTypeData<T extends StackItem<StackItemContent>>() {
final List<StackItem<StackItemContent>> data = List<StackItem<StackItemContent>>.from(innerData);
List<Map<String, dynamic>>
getTypeData<T extends StackItem<StackItemContent>>() {
final List<StackItem<StackItemContent>> data =
List<StackItem<StackItemContent>>.from(innerData);

final List<Map<String, dynamic>> list = <Map<String, dynamic>>[];

Expand All @@ -295,7 +309,8 @@ class StackBoardController extends SafeValueNotifier<StackConfig> {
/// * 获取数据 json 列表
/// * get data json list
List<Map<String, dynamic>> getAllData() {
final List<StackItem<StackItemContent>> data = List<StackItem<StackItemContent>>.from(innerData);
final List<StackItem<StackItemContent>> data =
List<StackItem<StackItemContent>>.from(innerData);

final List<Map<String, dynamic>> list = <Map<String, dynamic>>[];

Expand All @@ -312,7 +327,8 @@ class StackBoardController extends SafeValueNotifier<StackConfig> {
int get hashCode => _tag.hashCode;

@override
bool operator ==(Object other) => other is StackBoardController && _tag == other._tag;
bool operator ==(Object other) =>
other is StackBoardController && _tag == other._tag;

@override
void dispose() {
Expand Down
30 changes: 20 additions & 10 deletions lib/src/stack_board.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ class StackBoardConfig extends InheritedWidget {
final CaseStyle? caseStyle;

static StackBoardConfig of(BuildContext context) {
final StackBoardConfig? result = context.dependOnInheritedWidgetOfExactType<StackBoardConfig>();
final StackBoardConfig? result =
context.dependOnInheritedWidgetOfExactType<StackBoardConfig>();
assert(result != null, 'No StackBoardConfig found in context');
return result!;
}
Expand Down Expand Up @@ -74,19 +75,22 @@ class StackBoard extends StatelessWidget {
/// * 返回值可控制是否继续进行
/// * size changed callback
/// * return value can control whether to continue
final bool? Function(StackItem<StackItemContent> item, Size size)? onSizeChanged;
final bool? Function(StackItem<StackItemContent> item, Size size)?
onSizeChanged;

/// * 位置变化回调
/// * 返回值可控制是否继续进行
/// * offset changed callback
/// * return value can control whether to continue
final bool? Function(StackItem<StackItemContent> item, Offset offset)? onOffsetChanged;
final bool? Function(StackItem<StackItemContent> item, Offset offset)?
onOffsetChanged;

/// * 角度变化回调
/// * 返回值可控制是否继续进行
/// * angle changed callback
/// * return value can control whether to continue
final bool? Function(StackItem<StackItemContent> item, double angle)? onAngleChanged;
final bool? Function(StackItem<StackItemContent> item, double angle)?
onAngleChanged;

/// * 操作状态回调
/// * 返回值可控制是否继续进行
Expand All @@ -98,13 +102,15 @@ class StackBoard extends StatelessWidget {

/// * 操作层构建器
/// * actions builder
final Widget Function(StackItemStatus operatState, CaseStyle caseStyle)? actionsBuilder;
final Widget Function(StackItemStatus operatState, CaseStyle caseStyle)?
actionsBuilder;

/// * 边框构建器
/// * border builder
final Widget Function(StackItemStatus operatState)? borderBuilder;

StackBoardController get _controller => controller ?? StackBoardController.def();
StackBoardController get _controller =>
controller ?? StackBoardController.def();

@override
Widget build(BuildContext context) {
Expand All @@ -116,14 +122,16 @@ class StackBoard extends StatelessWidget {
behavior: HitTestBehavior.opaque,
child: ExBuilder<StackConfig>(
valueListenable: _controller,
shouldRebuild: (StackConfig p, StackConfig n) => p.indexMap != n.indexMap,
shouldRebuild: (StackConfig p, StackConfig n) =>
p.indexMap != n.indexMap,
builder: (StackConfig sc) {
return Stack(
fit: StackFit.expand,
children: <Widget>[
const SizedBox.expand(),
if (background != null) background!,
for (final StackItem<StackItemContent> item in sc.data) _itemBuilder(item),
for (final StackItem<StackItemContent> item in sc.data)
_itemBuilder(item),
],
);
},
Expand All @@ -141,8 +149,10 @@ class StackBoard extends StatelessWidget {
onDel: () => onDel?.call(item),
onTap: () => onTap?.call(item),
onSizeChanged: (Size size) => onSizeChanged?.call(item, size) ?? true,
onOffsetChanged: (Offset offset) => onOffsetChanged?.call(item, offset) ?? true,
onAngleChanged: (double angle) => onAngleChanged?.call(item, angle) ?? true,
onOffsetChanged: (Offset offset) =>
onOffsetChanged?.call(item, offset) ?? true,
onAngleChanged: (double angle) =>
onAngleChanged?.call(item, angle) ?? true,
onStatusChanged: (StackItemStatus operatState) =>
onStatusChanged?.call(item, operatState) ?? true,
actionsBuilder: actionsBuilder,
Expand Down
15 changes: 10 additions & 5 deletions lib/src/stack_board_items/item_case/stack_draw_case.dart
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ class _StackDrawCaseState extends State<StackDrawCase> {
controller: _controller,
boardPanEnabled: false,
boardScaleEnabled: false,
background: widget.background ?? Container(width: _size, height: _size, color: Colors.white),
background: widget.background ??
Container(width: _size, height: _size, color: Colors.white),
),
_tools(),
_actions(),
Expand All @@ -106,7 +107,8 @@ class _StackDrawCaseState extends State<StackDrawCase> {
/// * Tool bar
Widget _tools() {
return _configBuilder(
shouldRebuild: (DrawConfig p, DrawConfig n) => p.fingerCount != n.fingerCount || p.contentType != n.contentType,
shouldRebuild: (DrawConfig p, DrawConfig n) =>
p.fingerCount != n.fingerCount || p.contentType != n.contentType,
builder: (DrawConfig dc, Widget? child) {
final bool isPen = _isEditing && dc.fingerCount == 1;

Expand Down Expand Up @@ -152,7 +154,8 @@ class _StackDrawCaseState extends State<StackDrawCase> {
/// * Operation bar
Widget _actions() {
return _configBuilder(
shouldRebuild: (DrawConfig p, DrawConfig n) => p.fingerCount != n.fingerCount,
shouldRebuild: (DrawConfig p, DrawConfig n) =>
p.fingerCount != n.fingerCount,
builder: (DrawConfig dc, Widget? child) {
final bool isPen = dc.fingerCount == 1;

Expand Down Expand Up @@ -198,13 +201,15 @@ class _StackDrawCaseState extends State<StackDrawCase> {
trackHeight: 2,
),
child: _configBuilder(
shouldRebuild: (DrawConfig p, DrawConfig n) => p.strokeWidth != n.strokeWidth,
shouldRebuild: (DrawConfig p, DrawConfig n) =>
p.strokeWidth != n.strokeWidth,
builder: (DrawConfig dc, _) {
return Slider(
value: dc.strokeWidth,
max: 40,
min: 1,
onChanged: (double v) => _controller.setStyle(strokeWidth: v),
onChanged: (double v) =>
_controller.setStyle(strokeWidth: v),
);
},
),
Expand Down
4 changes: 3 additions & 1 deletion lib/src/stack_board_items/item_case/stack_text_case.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ class StackTextCase extends StatelessWidget {

@override
Widget build(BuildContext context) {
return item.status == StackItemStatus.editing ? _buildEditing(context) : _buildNormal(context);
return item.status == StackItemStatus.editing
? _buildEditing(context)
: _buildNormal(context);
}

/// * 构建文本
Expand Down
Loading

0 comments on commit f502047

Please sign in to comment.