搜索框与抽屉冲突问题

main
hs 2025-07-30 11:20:11 +08:00
parent fa4b765565
commit e7fbd5e12c
7 changed files with 250 additions and 226 deletions

View File

@ -184,6 +184,7 @@ class _DepartmentPickerState extends State<DepartmentPicker> {
controller: _searchController,
isShowSearchButton: false,
onSearch: (keyboard) {
},
),
),

View File

@ -1,43 +1,59 @@
import 'package:flutter/material.dart';
class SearchBarWidget extends StatelessWidget {
class SearchBarWidget extends StatefulWidget {
final String hintText;
final String buttonText;
final ValueChanged<String> onSearch;
final TextEditingController controller; //
final bool autoFocus;
final TextEditingController controller;
final bool showResetButton;
final String resetButtonText;
final VoidCallback? onReset;
final bool isClickableOnly;
final VoidCallback? onInputTap;
final ValueChanged<String>? onTextChanged; //
final ValueChanged<String>? onTextChanged;
final bool isShowSearchButton;
const SearchBarWidget({
Key? key,
required this.onSearch,
required this.controller, // controller
required this.controller,
this.hintText = '请输入关键字',
this.buttonText = '搜索',
this.autoFocus = false,
this.showResetButton = false,
this.resetButtonText = '重置',
this.onReset,
this.isClickableOnly = false,
this.onInputTap,
this.onTextChanged, //
this.onTextChanged,
this.isShowSearchButton = true,
}) : super(key: key);
//
@override
_SearchBarWidgetState createState() => _SearchBarWidgetState();
}
class _SearchBarWidgetState extends State<SearchBarWidget> {
late FocusNode _focusNode;
@override
void initState() {
super.initState();
_focusNode = FocusNode();
}
@override
void dispose() {
_focusNode.dispose();
super.dispose();
}
///
void updateText(String newText) {
controller.text = newText;
controller.selection = TextSelection.fromPosition(
widget.controller.text = newText;
widget.controller.selection = TextSelection.fromPosition(
TextPosition(offset: newText.length),
);
//
onTextChanged?.call(newText);
widget.onTextChanged?.call(newText);
}
@override
@ -45,39 +61,38 @@ class SearchBarWidget extends StatelessWidget {
return Row(
children: [
Expanded(
child: GestureDetector(
onTap: isClickableOnly ? onInputTap : null,
child: AbsorbPointer(
absorbing: isClickableOnly,
child: TextField(
controller: controller,
autofocus: autoFocus,
readOnly: isClickableOnly,
style: const TextStyle(fontSize: 15),
onChanged: onTextChanged,
//
decoration: InputDecoration(
filled: true,
fillColor: const Color(0xFFF5F5F5),
prefixIcon: const Icon(Icons.search_rounded),
hintText: hintText,
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(5),
borderSide: BorderSide.none,
),
isDense: true,
contentPadding: const EdgeInsets.symmetric(vertical: 8),
),
onSubmitted: onSearch,
child: TextField(
focusNode: _focusNode,
controller: widget.controller,
readOnly: widget.isClickableOnly,
autofocus: false,
style: const TextStyle(fontSize: 15),
onChanged: widget.onTextChanged,
onTap: () {
if (widget.isClickableOnly) {
//
widget.onInputTap?.call();
}
},
decoration: InputDecoration(
filled: true,
fillColor: const Color(0xFFF5F5F5),
prefixIcon: const Icon(Icons.search_rounded),
hintText: widget.hintText,
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(5),
borderSide: BorderSide.none,
),
isDense: true,
contentPadding: const EdgeInsets.symmetric(vertical: 8),
),
onSubmitted: widget.onSearch,
),
),
const SizedBox(width: 10),
if (isShowSearchButton)
//
if (widget.isShowSearchButton)
ElevatedButton(
onPressed: () => onSearch(controller.text.trim()),
onPressed: () => widget.onSearch(widget.controller.text.trim()),
style: ElevatedButton.styleFrom(
backgroundColor: Colors.green,
padding: const EdgeInsets.symmetric(horizontal: 15, vertical: 8),
@ -88,17 +103,16 @@ class SearchBarWidget extends StatelessWidget {
shadowColor: Colors.black45,
),
child: Text(
buttonText,
widget.buttonText,
style: const TextStyle(color: Colors.white, fontSize: 16),
),
),
//
if (showResetButton) const SizedBox(width: 10),
if (showResetButton)
if (widget.showResetButton) const SizedBox(width: 10),
if (widget.showResetButton)
ElevatedButton(
onPressed: () {
updateText(''); // 使
onReset?.call();
updateText('');
widget.onReset?.call();
},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.blue,
@ -110,8 +124,8 @@ class SearchBarWidget extends StatelessWidget {
shadowColor: Colors.black26,
),
child: Text(
resetButtonText,
style: TextStyle(color: Colors.white, fontSize: 16),
widget.resetButtonText,
style: const TextStyle(color: Colors.white, fontSize: 16),
),
),
],

View File

@ -73,7 +73,7 @@ class MyApp extends StatelessWidget {
behavior: HitTestBehavior.translucent, //
onTap: () {
//
FocusScope.of(context).requestFocus(_blankFocusNode);
FocusScope.of(context).unfocus();
},
child: child,
);

View File

@ -76,22 +76,25 @@ class _CheckRecordPageState extends State<CheckRecordPage> {
Widget build(BuildContext context) {
//
final double screenWidth = MediaQuery.of(context).size.width;
final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
return Scaffold(
key: _scaffoldKey, // key
appBar: MyAppbar(
title: appBarTitle,
actions: [
TextButton(
onPressed: () {
_scaffoldKey.currentState?.openEndDrawer();
// Builder Scaffold context
Builder(
builder: (innerContext) {
return TextButton(
onPressed: () {
// innerContext openEndDrawer
Scaffold.of(innerContext).openEndDrawer();
},
child: Text(
"查询",
style: TextStyle(color: Colors.white, fontSize: 16),
),
);
},
child: Text(
"查询",
style: TextStyle(color: Colors.white, fontSize: 16),
),
),
],
),
@ -145,8 +148,9 @@ class _CheckRecordPageState extends State<CheckRecordPage> {
padding: EdgeInsets.all(10),
child: SearchBarWidget(
controller: _searchController,
autoFocus: true,
onSearch: (keyboard) {
//
_page=1;
searchKey=keyboard;

View File

@ -60,7 +60,6 @@ class _DangerWaitListPageState extends State<DangerWaitListPage> {
//
final List<String> _levelTexts = ["重大隐患","一般隐患"];
final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
@override
void initState() {
@ -101,21 +100,24 @@ class _DangerWaitListPageState extends State<DangerWaitListPage> {
final double screenWidth = MediaQuery.of(context).size.width;
return Scaffold(
key: _scaffoldKey, // key
appBar: MyAppbar(
title: widget.dangerType.displayName,
actions: [
if(1==widget.appItem)
TextButton(
onPressed: () {
//
_scaffoldKey.currentState?.openEndDrawer();
},
child: Text(
"查询",
style: TextStyle(color: Colors.white, fontSize: 16),
if (1 == widget.appItem)
Builder(
builder: (innerContext) {
return TextButton(
onPressed: () {
// innerContext ScaffoldState
Scaffold.of(innerContext).openEndDrawer();
},
child: Text(
"查询",
style: TextStyle(color: Colors.white, fontSize: 16),
),
);
},
),
),
],
),

View File

@ -39,7 +39,6 @@ class _RiskControlPageState extends State<RiskControlPage> {
//
final List<String> _levelTexts = ["重大风险/A级", "较大风险/B级", "一般风险/C级", "低风险/D级"];
final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
@override
@ -59,19 +58,23 @@ class _RiskControlPageState extends State<RiskControlPage> {
final double screenWidth = MediaQuery.of(context).size.width;
return Scaffold(
key: _scaffoldKey, // key
appBar: MyAppbar(
title: "风险分布",
actions: [
TextButton(
onPressed: () {
//
_scaffoldKey.currentState?.openEndDrawer();
// Builder Scaffold context
Builder(
builder: (innerContext) {
return TextButton(
onPressed: () {
// innerContext openEndDrawer
Scaffold.of(innerContext).openEndDrawer();
},
child: Text(
"查询",
style: TextStyle(color: Colors.white, fontSize: 16),
),
);
},
child: Text(
"查询",
style: TextStyle(color: Colors.white, fontSize: 16),
),
),
],
),

File diff suppressed because it is too large Load Diff