android - 如何创建类 Searchp 扩展 StatelessWidget 实现 SearchDelegate从头开始
问题描述
我是颤振的新手,现在我正在尝试根据完成餐厅应用程序所需的内容来实现搜索代理栏。Flutter 的搜索支持(The Boring Flutter Development Show,Ep. 10)的默认搜索代理不够灵活,因为我的“购物车”是一个可滚动的 BottomSheet,当我点击搜索图标时,我的搜索代理带头我添加了我的购物车在期望滚动底部表的页面上滚动时占据整个屏幕,但工作表无法滚动搜索委托 appBar。自 4 天以来一直坚持这一点,我想实现孔搜索委托以访问构建(BuildContext 上下文)以将 Stack 放在那里,但我不知道如何从头开始实现它(只知道颤振搜索来自Flutter 的搜索支持(The Boring Flutter Development Show,Ep. 10))。如果您有其他方法可以解决我的问题,谢谢。这些是图像:搜索栏和工作表在其初始位置的 初始位置
搜索卡住的应用程序栏搜索委托不允许工作表滚动
我需要什么
这是我现在用于搜索 delagate 的代码:
@override
List<Widget> buildActions(BuildContext context) {
// TODO: implement buildActions
return [
IconButton(
icon: Icon(Icons.clear),
onPressed:(){
query = "";
}
)
];
}
@override
Widget buildLeading(BuildContext context) {
// TODO: implement buildLeading
return IconButton(icon: AnimatedIcon(
icon:AnimatedIcons.menu_arrow,
progress: transitionAnimation,
),
onPressed:(){
close(context,null);
}
);
}
@override
Widget buildResults(BuildContext context) {
// TODO: implement buildResults
final suggestionList =query.isEmpty?food_recent:data.where((p)=> p.toString().contains(query)).toList();
return StatefulBuilder(
builder: (BuildContext context, StateSetter setState) {
return Stack(
children: <Widget>[
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
// SizedBox(height: 8),
AspectRatio(
aspectRatio: 0.8,
child: Opacity(
opacity: 1,
child:ListView.builder(
//padding: EdgeInsets.only(right: 32, top: 128),
//controller: scrollController,
itemCount: suggestionList?.length ?? 0, //20,
itemBuilder: (context, index) {
//Event event = events[index % 3];
Produit event = suggestionList[index];
return MyEventItemProduct(
event: event,
percentageCompleted:0.15,
);
},
),
),
)
//Tabs(),
//SizedBox(height: 8),
//SlidingCardsView(),
],
),
ScrollableExhibitionSheet(),//use this or ScrollableExhibitionSheet
],
);
});
}
@override
Widget buildSuggestions(BuildContext context) {
// TODO: implement buildSuggestions
//new Text(suggestionList[index]["colis"]["libelle_coli"]),
final suggestionList =query.isEmpty?food_recent:data.where((p)=> p.toString().contains(query)).toList();
return StatefulBuilder(
builder: (BuildContext context, StateSetter setState) {
return Stack(
overflow: Overflow.clip,
children: <Widget>[
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
// SizedBox(height: 8),
AspectRatio(
aspectRatio: 0.8,
child: Opacity(
opacity: 1,
child:ListView.builder(
//padding: EdgeInsets.only(right: 32, top: 128),
//controller: scrollController,
itemCount: suggestionList?.length ?? 0, //20,
itemBuilder: (context, index) {
//Event event = events[index % 3];
Produit event = suggestionList[index];
return MyEventItemProduct(
event: event,
percentageCompleted:0.15,
);
},
),
),
)
//Tabs(),
//SizedBox(height: 8),
//SlidingCardsView(),
],
),
ScrollableExhibitionSheet(),//use this or ScrollableExhibitionSheet
],
);
});
}
因为我不知道如何解决我的问题,所以我决定从 srcatch 实现它以拥有构建方法小部件。这就是我想要做的,但我不知道如何实现要求使其像默认应用栏一样工作的 10 种其他方法。
class Searchp extends StatelessWidget implements SearchDelegate <Produit>{
@override
String query;
@override
ThemeData appBarTheme(BuildContext context) {
// TODO: implement appBarTheme
return null;
}
@override
Widget build(BuildContext context) {
// TODO: implement build
return null;
}
@override
List<Widget> buildActions(BuildContext context) {
// TODO: implement buildActions
return null;
}
@override
Widget buildLeading(BuildContext context) {
// TODO: implement buildLeading
return null;
}
@override
Widget buildResults(BuildContext context) {
// TODO: implement buildResults
return null;
}
@override
Widget buildSuggestions(BuildContext context) {
// TODO: implement buildSuggestions
return null;
}
@override
void close(BuildContext context, Produit result) {
// TODO: implement close
}
@override
// TODO: implement keyboardType
TextInputType get keyboardType => null;
@override
// TODO: implement searchFieldLabel
String get searchFieldLabel => null;
@override
void showResults(BuildContext context) {
// TODO: implement showResults
}
@override
void showSuggestions(BuildContext context) {
// TODO: implement showSuggestions
}
@override
// TODO: implement textInputAction
TextInputAction get textInputAction => null;
@override
// TODO: implement transitionAnimation
Animation<double> get transitionAnimation => null;
}
解决方案
finally 不需要从 StatelessWidget 扩展。从src/material/search.dart覆盖 _SearchPageState 类 解决了我的问题。
推荐阅读
- python - 实现 SSL 证书的 Python 套接字
- catboost - 将目标编码值传递给 CatBoost
- openjdk-11 - 使用 Cygwin 在 Windows 上从 openjdk 的源代码构建 jdk11
- amazon-web-services - 通过具有特定角色的 EKSCTL 创建 EKS 集群
- python - Ape 尝试制作密码生成器:“if 语句”和“try & except”函数太难了(Python)
- excel - 如何使用 IFNA,如果 NA 作为文本字符串加上顺序编号,则创建值
- excel - VBA循环要求用户输入新的工作表名称
- java - Java 泛型类型继承
- python - 如何使用 React 为 FastApi 配置 .htaccess
- javascript - 在 Visual Studio Code 中使用 IntelliSense 时 TypeScript React 道具优先级