提交 19fa402c authored 作者: shixiaochen's avatar shixiaochen

1、增加SearchAppBar

上级 ea42cd1d
......@@ -6,3 +6,4 @@ export 'utils/image_utils.dart';
export 'widget/my_app_bar.dart';
export 'widget/my_scaffold.dart';
export 'widget/my_scroll_view.dart';
export 'widget/search_app_bar.dart';
import 'package:flutter/material.dart';
import 'package:keyboard_actions/keyboard_actions.dart';
class KeyBoardUtils {
static KeyboardActionsConfig getKeyboardActionsConfig(
BuildContext context, List<FocusNode> list) {
return KeyboardActionsConfig(
keyboardBarColor: Colors.grey[200],
nextFocus: true,
actions: List.generate(
list.length,
(i) => KeyboardActionsItem(
focusNode: list[i],
toolbarButtons: [
(node) {
return GestureDetector(
onTap: () => node.unfocus(),
child: const Padding(
padding: EdgeInsets.only(right: 16.0),
child: Text('关闭'),
),
);
},
],
)),
);
}
//隐藏键盘
static void hideKeyboard() {
FocusManager.instance.primaryFocus?.unfocus();
}
}
import 'package:flutter/material.dart';
bool isNullOrEmpty(String? txt) {
return txt == null || txt.isEmpty;
}
bool isMobile(String? mobile) {
return !isNullOrEmpty(mobile) &&
mobile!.length == 11 &&
mobile.startsWith("1");
}
///判断文字是否为空
String txtIsNull(txt, {nullTxt = "-"}) {
if (txt == null || txt == "") {
return nullTxt;
} else {
return txt;
}
}
String txtIsNullWithTitle(txt, title, {nullTxt = "-"}) {
if (txt == null || txt == "") {
return nullTxt;
} else {
return title + ": " + txt;
}
}
///设置文字长度
String txtLength(String? txt, {int? length, nullTxt = ""}) {
if (txt == null || txt == "") {
return nullTxt;
} else {
if (length != null && txt.length > length) {
return "${txt.substring(0, length)}...";
}
return txt;
}
}
//时间去除.0
String? rightTime(String? time) {
String? rightTime = time;
if (time != null && time != "" && time.contains(".")) {
rightTime = time.split(".")[0];
}
return rightTime;
}
//获取当前时间
String? getNowTime() {
return rightTime(DateTime.now().toString());
}
//获取输入框内的信息
String getControllerString(TextEditingController controller) {
return controller.value.text.toString().trim();
}
//获取输入框内的文字长度
int getControllerStringLength(TextEditingController controller) {
return getControllerString(controller).length;
}
bool checkEasyPwd(String input,String phone) {
//1:不能出现连续的字母或者数字
var allMatchLength = input.length - 1; // 匹配全部
var preMatchLength = input.length - 2; // 匹配N-1位
RegExp allSameChar = new RegExp("([0-9a-zA-Z])\\1{$allMatchLength}"); // 匹配全为连续字符
RegExp preSameChar = new RegExp("([0-9a-zA-Z])\\1{$preMatchLength}"); // 匹配n-1位位连续字符
//2:不能出现顺子(123456或654321一类) 连续的数字
// allSuccessiveNum 顺子
// startCharSuccessiveNum 第一位字母 后面顺子
// endCharSuccessiveNum 最后一位字母 前面顺子
RegExp allSuccessiveNum = new RegExp("(?:0(?=1)|1(?=2)|2(?=3)|3(?=4)|4(?=5)|5(?=6)|6(?=7)|7(?=8)|8(?=9)|9(?=0)){$allMatchLength}\\d|(?:9(?=8)|8(?=7)|7(?=6)|6(?=5)|5(?=4)|4(?=3)|3(?=2)|2(?=1)|1(?=0)){$allMatchLength}\\d");
RegExp startCharSuccessiveNum = new RegExp("[A-Za-z](?:0(?=1)|1(?=2)|2(?=3)|3(?=4)|4(?=5)|5(?=6)|6(?=7)|7(?=8)|8(?=9)){$preMatchLength}\\d|[A-Za-z](?:9(?=8)|8(?=7)|7(?=6)|6(?=5)|5(?=4)|4(?=3)|3(?=2)|2(?=1)|1(?=0)){$preMatchLength}\\d");
RegExp endCharSuccessiveNum = new RegExp("((?:0(?=1)|1(?=2)|2(?=3)|3(?=4)|4(?=5)|5(?=6)|6(?=7)|7(?=8)|8(?=9)){$preMatchLength}\\d)[A-Za-z]|((?:9(?=8)|8(?=7)|7(?=6)|6(?=5)|5(?=4)|4(?=3)|3(?=2)|2(?=1)|1(?=0)){$preMatchLength}\\d)[A-Za-z]");
// print('----- allSameChar: $allSameChar');
// print('----- preSameChar: $preSameChar');
// print('----- allSuccessiveNum: $allSuccessiveNum');
// print('----- startCharSuccessiveNum: $startCharSuccessiveNum');
// print('----- endCharSuccessiveNum: $endCharSuccessiveNum');
// print('----- allSameChar: $input ${allSameChar.hasMatch(input)}');
// print('----- preSameChar: $input ${preSameChar.hasMatch(input)}');
// print('----- allSuccessiveNum: $input ${allSuccessiveNum.hasMatch(input)}');
// print('----- startCharSuccessiveNum: $input ${startCharSuccessiveNum.hasMatch(input)}');
// print('----- endCharSuccessiveNum: $input ${endCharSuccessiveNum.hasMatch(input)}');
return allSameChar.hasMatch(input) ||
preSameChar.hasMatch(input) ||
allSuccessiveNum.hasMatch(input) ||
startCharSuccessiveNum.hasMatch(input) ||
endCharSuccessiveNum.hasMatch(input) ||
phone.contains(input);
}
import 'package:flutter/material.dart';
import 'package:flutter_clx_base/common/gaps.dart';
import 'package:flutter_clx_base/utils/keyboard_utils.dart';
import 'package:flutter_clx_base/utils/string_util.dart';
import 'package:flutter_clx_base/utils/toast_util.dart';
import 'package:get/get.dart';
import 'package:keyboard_actions/keyboard_actions.dart';
class SearchAppBar extends StatefulWidget implements PreferredSizeWidget {
final Function? onCallback;
final String? hintText;
final bool showLeading;
final Color backgroundColor;
final bool primary;
const SearchAppBar({
Key? key,
this.onCallback,
this.hintText = "请输入搜索内容",
this.showLeading = true,
this.backgroundColor = Colors.blue,
this.primary = true,
}) : super(key: key);
@override
State<SearchAppBar> createState() => _SearchAppBarState();
@override
Size get preferredSize => const Size(double.infinity, 55.0);
}
class _SearchAppBarState extends State<SearchAppBar> {
final searchInfoController = TextEditingController();
final searchFocusNode = FocusNode();
@override
Widget build(BuildContext context) {
return KeyboardActions(
config:
KeyBoardUtils.getKeyboardActionsConfig(context, [searchFocusNode]),
tapOutsideBehavior: TapOutsideBehavior.opaqueDismiss,
child: AppBar(
backgroundColor: widget.backgroundColor,
titleSpacing: 0.0,
primary: widget.primary,
leading: widget.showLeading
? InkWell(
onTap: () => Get.back(),
child: const Icon(Icons.arrow_back_ios, color: Colors.white),
)
: Container(),
leadingWidth: widget.showLeading ? null : 0.0,
title: Container(
color: Colors.blue,
child: Row(
children: <Widget>[
Expanded(
child: Container(
decoration: const BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(20.0)),
color: Color(0xFFEEEEEE),
),
child: Row(
children: <Widget>[
hGap10,
Icon(Icons.search, color: Colors.grey.shade300),
hGap5,
Expanded(
child: TextField(
maxLines: 1,
focusNode: searchFocusNode,
style: const TextStyle(fontSize: 14),
textInputAction: TextInputAction.search,
controller: searchInfoController,
onSubmitted: (value) => _search(),
decoration: InputDecoration(
contentPadding: const EdgeInsets.symmetric(
horizontal: 0.0, vertical: 8.0),
isDense: true,
counterText: "",
hintText: widget.hintText,
hintStyle: const TextStyle(
fontSize: 14.0, color: Color(0xFF999999)),
border: InputBorder.none, //去掉下划线
//hintStyle: TextStyles.textGrayC14
),
),
),
],
),
),
),
InkWell(
onTap: () => _search(),
child: Container(
width: 60.0,
alignment: Alignment.center,
child: const Text(
"搜索",
style: TextStyle(color: Color(0xFFFFFFFF), fontSize: 14.0),
),
),
)
],
),
),
),
);
}
/// 校验
bool _checkInfo() {
if (isNullOrEmpty(getControllerString(searchInfoController))) {
ToastUtil.showToast("请输入搜索内容");
return false;
}
return true;
}
/// 搜索
void _search() {
if (_checkInfo()) {
KeyBoardUtils.hideKeyboard();
if (widget.onCallback != null) {
widget.onCallback!(getControllerString(searchInfoController));
}
}
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论