搜索框输入字符串来匹配,就是输入字符串,下面显示相关的文字
static Widget textRich(String title,String searchStr){
String value = title;
String startStr = '';
String endStr = '';
if (value.length < searchStr.length) {
} else {
String _lowValue = value.toLowerCase();
var startIndex = _lowValue.indexOf(searchStr.toLowerCase());
if(startIndex == -1){
endStr = value;
} else {
startStr = value.substring(0, startIndex);
endStr = value.substring(startIndex + searchStr.length, value.length);
}
}
return Container(
height: 40,
child: Text.rich(
TextSpan(text: startStr, children: [
TextSpan(
text: searchStr,
style: const TextStyle(fontSize: 18, color: Color(0xff0075FF),fontWeight: FontWeight.w500),
),
TextSpan(
text: endStr,
style: const TextStyle(fontSize: 18, color: colorBlack,fontWeight: FontWeight.w500),
),
]),
style: const TextStyle(fontSize: 18, color: colorBlack,fontWeight: FontWeight.w500),
),
);
}
SearchAppBar(
(searStr) {
setState(() {
});
},
() {
Navigator.pop(context);
},
hintLabel: widget.title,
rightBtn: '',
appBack: false,
searchChange: (str) {
print('searchChange str:$str');
searchStr = str;
if (str.isEmpty) {
setState(() {
});
} else {
_loadData(str);
}
},
)
class SearchAppBar extends StatefulWidget {
final Function() backPress;
final Function(String) searchStr;
final Function(String)? searchChange;
final String? hintLabel;
final String? rightBtn;
final bool appBack;
const SearchAppBar(this.searchStr, this.backPress, {
Key? key, this.hintLabel, this.rightBtn, this.searchChange, this.appBack = true}) : super(key: key);
@override
State<StatefulWidget> createState() {
return SearchAppBarState();
}
}
class SearchAppBarState extends State<SearchAppBar> {
late FocusNode _focusNode;
bool _offstage = true;
final TextEditingController _textEditingController = TextEditingController();
@override
void initState() {
super.initState();
_focusNode = FocusNode();
_textEditingController.addListener(() {
var isVisible = _textEditingController.text.isNotEmpty;
_updateDelIconVisible(isVisible);
widget.searchChange!(_textEditingController.text);
});
}
_updateDelIconVisible(bool isVisible) {
setState(() {
_offstage = !isVisible;
});
}
@override
Widget build(BuildContext context) {
String rightBtn = widget.rightBtn ?? '';
return Container(
width: double.infinity,
height: 50,
padding: const EdgeInsets.only(left: 10, right: 10),
margin: const EdgeInsets.only(top: 8, bottom: 8),
child: Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
widget.appBack ?
GestureDetector(
child: SvgPicture.asset(
'assets/images/back_icon.svg',
width: 24,
height: 24,
color: colorBlack,
),
onTap: () {
widget.backPress();
},
) : Container(),
Expanded(
flex: 1,
child: Container(
height: double.infinity,
margin: const EdgeInsets.only(left: 12),
decoration: BoxDecoration(color: widget.appBack ? Colors.grey.withAlpha(20) : Colors.white,
borderRadius:widget.appBack? BorderRadius.circular(4):BorderRadius.circular(10)),
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
const Padding(padding: EdgeInsets.only(left: 8)),
widget.appBack ?
const Icon(
Icons.search,
color: Colors.grey,
size: 24,
):Container(),
const Padding(padding: EdgeInsets.only(left: 8)),
Expanded(
flex: 1,
child: TextField(
controller: _textEditingController,
autofocus: false,
focusNode: _focusNode,
style: const TextStyle(fontSize: 16, color: colorBlack, height: 1.1),
decoration: InputDecoration(
hintText: widget.hintLabel,
hintStyle: const TextStyle(fontSize: 16, color: colorGrey, height: 1.1),
border: InputBorder.none,
),
maxLines: 1,
),
),
const Padding(padding: EdgeInsets.only(right: 8)),
Offstage(
offstage: _offstage,
child: GestureDetector(
onTap: () => {
_textEditingController.clear()},
child: const Icon(
Icons.cancel,
color: Colors.grey,
size: 24,
),
),
),
const Padding(padding: EdgeInsets.only(right: 8)),
],
),
),
),
if (rightBtn.isNotEmpty)
GestureDetector(
onTap: () {
widget.searchStr(_textEditingController.text);
_focusNode.unfocus();
},
child: Container(
padding: const EdgeInsets.only(left: 16, right: 12),
child: Text(rightBtn, style: TextStyle(fontSize: 16, color: colorBlue)),
),
)
else
const Padding(padding: EdgeInsets.only(right:5)),
GestureDetector(
child: const Icon(Icons.keyboard_voice_sharp,size: 30,),
onTap: () {
Navigator.of(context).push(PageRouteBuilder(
opaque: false,
pageBuilder: (context, animation, secondaryAnimation) {
return Voice2Words();
})).then((value) {
if (value != null) {
setState(() {
_textEditingController.text = value['msg'] ?? '';
});
}
});
},
)
],
),
);
}
@override
void dispose() {
super.dispose();
_focusNode.unfocus();
}
}