搜索框
效果图
代码
import 'package:flutter/material.dart';
class NovelSearch extends StatefulWidget {
@override
State<StatefulWidget> createState() => _NovelSearchState();
}
class _NovelSearchState extends State<NovelSearch> {
String searchVal = '';
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
titleSpacing: 0,
elevation: 0,
title: SearchAppBar(
hintLabel: "搜索书名",
onSubmitted: (value) {
setState(() {
this.searchVal = value;
});
},
),
),
body: Text("搜索值是:${this.searchVal}"),
);
}
}
class SearchAppBar extends StatefulWidget {
SearchAppBar({Key? key, required this.hintLabel, required this.onSubmitted})
: super(key: key);
final String hintLabel;
final Function(String) onSubmitted;
@override
State<StatefulWidget> createState() => _SearchAppBarState();
}
class _SearchAppBarState extends State<SearchAppBar> {
FocusNode _focusNode = FocusNode();
String searchVal = '';
TextEditingController _controller = TextEditingController();
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
_focusNode.requestFocus();
});
}
@override
Widget build(BuildContext context) {
MediaQueryData queryData = MediaQuery.of(context);
return Container(
width: queryData.size.width * 0.8,
height: 40,
padding: EdgeInsets.only(left: 20),
alignment: Alignment.centerLeft,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10), color: Colors.white),
child: TextField(
controller: _controller,
focusNode: _focusNode,
autofocus: true,
decoration: InputDecoration(
hintText: widget.hintLabel,
hintStyle: TextStyle(color: Colors.grey),
border: InputBorder.none,
icon: Padding(
padding: const EdgeInsets.only(left: 0, top: 0),
child: Icon(
Icons.search,
size: 18,
color: Theme.of(context).primaryColor,
)),
suffixIcon: this.searchVal.isNotEmpty
? IconButton(
icon: Icon(Icons.close),
onPressed: () {
setState(() {
this.searchVal = '';
_controller.clear();
});
},
)
: null),
onChanged: (value) {
setState(() {
this.searchVal = value;
});
},
onSubmitted: (value) {
widget.onSubmitted(value);
},
),
);
}
}
搜索历史
效果
代码
import 'package:flutter/material.dart';
class SearchHistory extends StatefulWidget {
List<String> list;
final Function() clearHistory;
SearchHistory({Key? key, required this.list, required this.clearHistory})
: super(key: key) {
list.removeWhere((element) => element.isEmpty);
}
@override
State<StatefulWidget> createState() => _SearchHistoryState();
}
class _SearchHistoryState extends State<SearchHistory> {
@override
Widget build(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SizedBox(
height: 30,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Padding(
padding: EdgeInsets.only(left: 10),
child: Text(
'搜索历史',
style: TextStyle(color: Colors.grey, fontSize: 12),
),
),
GestureDetector(
onTap: () {
widget.clearHistory();
},
child: Padding(
padding: EdgeInsets.only(right: 10),
child: Text(
widget.list.isNotEmpty ? '清空历史' : '',
style: TextStyle(
fontSize: 12,
color: Colors.grey,
),
),
),
)
],
),
),
Expanded(
child: SingleChildScrollView(
padding: EdgeInsets.all(2),
child: Wrap(
children: List.generate(
widget.list.length, (index) => _listItem(widget.list[index])),
),
),
)
],
);
}
_listItem(String title) {
return FittedBox(
fit: BoxFit.scaleDown,
alignment: Alignment.centerLeft,
child: Container(
padding: EdgeInsets.symmetric(vertical: 5, horizontal: 10),
margin: EdgeInsets.all(5),
decoration: BoxDecoration(
color: Colors.grey.withOpacity(0.5),
borderRadius: BorderRadius.circular(10)),
child: Text(
title,
style: TextStyle(fontSize: 12),
),
),
);
}
}