搜索框与抽屉冲突问题

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, controller: _searchController,
isShowSearchButton: false, isShowSearchButton: false,
onSearch: (keyboard) { onSearch: (keyboard) {
}, },
), ),
), ),

View File

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

View File

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

View File

@ -76,22 +76,25 @@ class _CheckRecordPageState extends State<CheckRecordPage> {
Widget build(BuildContext context) { Widget build(BuildContext context) {
// //
final double screenWidth = MediaQuery.of(context).size.width; final double screenWidth = MediaQuery.of(context).size.width;
final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
return Scaffold( return Scaffold(
key: _scaffoldKey, // key
appBar: MyAppbar( appBar: MyAppbar(
title: appBarTitle, title: appBarTitle,
actions: [ actions: [
TextButton( // Builder Scaffold context
onPressed: () { Builder(
_scaffoldKey.currentState?.openEndDrawer(); 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), padding: EdgeInsets.all(10),
child: SearchBarWidget( child: SearchBarWidget(
controller: _searchController, controller: _searchController,
autoFocus: true,
onSearch: (keyboard) { onSearch: (keyboard) {
// //
_page=1; _page=1;
searchKey=keyboard; searchKey=keyboard;

View File

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