Skip to content

Commit

Permalink
pub 0.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
yshye committed Nov 26, 2021
1 parent 110b0f6 commit df0d1a4
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 50 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
## 0.2.0

* TODO: Describe initial release.
## 0.2.0
- add dialog
- add sheet
3 changes: 1 addition & 2 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ void main() {
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);

// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
Expand All @@ -36,7 +35,7 @@ class _MyHomePageState extends State<MyHomePage> {
List<Item> items = List.generate(
100,
(index) => Item(
'JsonYe${index + 1}',
'JsonYe${index + 1}-jsonye',
height: Random().nextDouble() * 100,
sex: Random().nextBool(),
),
Expand Down
33 changes: 17 additions & 16 deletions lib/src/sheet/list_sheet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class MiniListBottomSheet<T> extends StatelessWidget {

final Widget operation;

final String? searchKey;

const MiniListBottomSheet(
{Key? key,
required this.title,
Expand All @@ -36,7 +38,8 @@ class MiniListBottomSheet<T> extends StatelessWidget {
this.onItemClick,
this.operation = const SizedBox(
height: 10,
)})
),
this.searchKey})
: super(key: key);

@override
Expand All @@ -55,21 +58,18 @@ class MiniListBottomSheet<T> extends StatelessWidget {
height: _height,
children: dataSource
.map((item) => ListTile(
onTap: () {
if (onItemClick != null) {
onItemClick!(item);
} else {
Navigator.pop(context, dataSource.indexOf(item));
}
},
title: buildItem != null
? buildItem!(context, item)
: Text(
toLabel != null ? toLabel!(item) : item.toString(),
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
))
onTap: () {
if (onItemClick != null) {
onItemClick!(item);
} else {
Navigator.pop(context, dataSource.indexOf(item));
}
},
title: buildItem != null
? buildItem!(context, item, searchKey)
: buildSearchSpan(
toLabel != null ? toLabel!(item) : item.toString(),
searchKey ?? '')))
.toList(),
operation: operation,
);
Expand Down Expand Up @@ -145,6 +145,7 @@ class _MiniSearchListBottomSheetState<T>
dataSource: _list,
toLabel: widget.toLabel,
buildItem: widget.buildItem,
searchKey: _searchKey,
onItemClick: (item) => Navigator.pop(
context,
widget.dataSource.indexOf(item),
Expand Down
55 changes: 31 additions & 24 deletions lib/src/tools.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,38 +37,45 @@ class UsNumberTextInputFormatter extends TextInputFormatter {
}

/// 创建搜索内容
Widget buildSearchSpan(String content, String searchText,
{Color searchTextColor = Colors.red,
TextStyle style = const TextStyle(color: Colors.black)}) {
int startIndex = content.indexOf(searchText);
int endIndex = -1;
if (startIndex > -1) {
endIndex = startIndex + searchText.length;
return RichText(
text: TextSpan(
text: content.substring(0, startIndex),
style: style,
children: [
TextSpan(
//获取剩下的字符串,并让它变成灰色
text: searchText,
style: style.copyWith(color: searchTextColor)),
TextSpan(
//获取剩下的字符串,并让它变成灰色
text: content.substring(endIndex),
style: style)
]));
} else {
return Text(content, maxLines: null, style: style);
Widget buildSearchSpan(
String content,
String searchText, {
Color searchTextColor = Colors.red,
TextStyle style = const TextStyle(color: Colors.black, fontSize: 16),
bool insensitiveCase = true,
}) {
String _content = insensitiveCase ? content.toLowerCase() : content;
String _searchText = insensitiveCase ? searchText.toLowerCase() : searchText;

List<TextSpan> spans = [];
int _cLength = _content.length;
int _sLength = _searchText.length;
int _start = 0;
for (int i = 0; _sLength > 0 && i <= _cLength - _sLength; i++) {
if (_content.substring(i, i + _sLength) == _searchText) {
spans.addAll([
TextSpan(text: content.substring(_start, i), style: style),
TextSpan(
text: content.substring(i, _sLength + i),
style: style.copyWith(color: searchTextColor))
]);
_start = i + _sLength;
i = _start - 1;
}
}
if (_start != _cLength) {
spans.add(TextSpan(text: content.substring(_start), style: style));
}
return RichText(text: TextSpan(style: style, children: spans));
}

/// 复制到剪切板
void clip(String value) => Clipboard.setData(ClipboardData(text: value));

typedef ToString<T> = String Function(T model);

typedef BuildCheckChild<T> = Widget Function(BuildContext context, T t);
typedef BuildCheckChild<T> = Widget Function(BuildContext context, T t,
[String? highlight]);

/// 对象的模糊查找
typedef Contains<T> = bool Function(T object, String content);
28 changes: 22 additions & 6 deletions test/mini_dialog_test.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
import 'package:flutter_test/flutter_test.dart';

import 'package:mini_dialog/mini_dialog.dart';

void main() {
test('adds one to input values', () {
// expect(calculator.addOne(2), 3);
// expect(calculator.addOne(-7), -6);
// expect(calculator.addOne(0), 1);
test('String', () {
String a = "你好asb1231好";
String s = '好';
String _a = a.toLowerCase();
// List<int> al = _a.codeUnits;
String _s = s.toLowerCase();
// List<int> sl = _s.codeUnits;
print(_a);
print(_s);
int alength = _a.length;
int slength = _s.length;
int start = 0;
for (int i = 0; i < alength; i++) {
if (_a.codeUnitAt(i) == _s.codeUnitAt(0) &&
i + slength <= alength &&
_a.substring(i, slength + i) == _s) {
print("c=${[start, i - 1]}");
print("i=${[i, i + slength]}");
i = i + slength;
start = i;
}
}
});
}

0 comments on commit df0d1a4

Please sign in to comment.