根据此为什么当我使用键盘时我的小部件会重建问题是Mediaquery,但我用methodchannel替换它以获得顶部填充的状态栏高度
但是当键盘弹出时它仍然会重建
return Padding(
padding: EdgeInsets.only(
top: MediaQueryData.fromView(View.of(context)).padding.top, //replaced back to mediaquery to avoid boilerplate
// top: statusBarHeight,
left: 10,
right: 10,
),
child: DraggableScrollableSheet(
initialChildSize: 1.0,
minChildSize: 0.99,
maxChildSize: 1.0,
expand: true,
snap: true,
builder: (context, scrollController) {
return Column(
// ignore: prefer_const_literals_to_create_immutables
children: [
// Serarch Bar in Drawer
Padding(
padding: const EdgeInsets.only(top: 10, bottom: 10, left: 20, right: 20),
child: TextField(
controller: widget._textEditingController,
onSubmitted: (value) {
widget.appops.openApps(widget.appops.searchAppList[0]);
// clearText();
// setState(() {
// appops.searchApp('');
// });
},
onChanged: (String value) {
setState(() {
widget.appops.searchApp(context, value);
});
},
style: TextStyle(color: widget.themeTextColor),
decoration: InputDecoration(
contentPadding: const EdgeInsets.only(top: 20, bottom: 20, left: 20, right: 20),
focusColor: widget.themeTextColor,
hintText: "Search",
hintStyle: TextStyle(color: widget.themeTextColor),
filled: true,
// fillColor: sysBrightness ? Colors.grey[900] : Colors.grey[200],
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(30.0),
borderSide: BorderSide.none,
),
suffixIcon: widget._textEditingController.text != ''
? IconButton(
icon: const Icon(Icons.clear),
onPressed: () {
widget.clearText();
setState(() {
widget.appops.searchApp(context, '');
});
},
)
: const Text(''),
prefixIcon: const Icon(Icons.search),
),
),
),
widget.setIcon
? Flexible(
fit: FlexFit.tight,
child: Row(
children: [
SizedBox(
// width: MediaQuery.sizeOf(context).width - 60,
width: MediaQuery.sizeOf(context).width - 20,
// width: constraints.maxWidth - 20,
// child: Scrollbar(
child: Scrollbar(
controller: scrollController,
interactive: true,
// thumbVisibility: true,
radius: const Radius.circular(10),
// trackVisibility: true,
thickness: 10,
child: Padding(
padding: const EdgeInsets.all(10.0),
child: GridView.builder(
cacheExtent: 9999,
controller: scrollController,
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 4,
childAspectRatio: 0.9,
mainAxisSpacing: 30,
crossAxisSpacing: 10,
// mainAxisExtent: 110,
),
itemCount: widget.appops.searchAppList.length,
itemBuilder: (BuildContext context, int index) {
final appsls = widget.appops.searchAppList[index];
// String currentLetter = getFirstLetter(appls.appName);
// bool shouldDisplaySeparator =
// index == 0 || currentLetter != getFirstLetter(appops.searchAppList[index - 1].appName);
if (widget.appops.searchAppList.isNotEmpty) {
return AppGridItem(
appls: appsls,
themeTextColor: widget.themeTextColor,
addToDock: widget.addToDock,
loadApps: widget.loadApps,
appops: widget.appops,
sysBrightness: widget.sysBrightness,
dockIconList: widget.dockIconList,
);
} else {
return const Expanded(
child: Center(
child: Text('Loading...'),
),
);
}
}),
),
)),
],
),
)
: Flexible(
child: Row(
children: [
SizedBox(
// width: MediaQuery.sizeOf(context).width - 60,
width: MediaQuery.sizeOf(context).width - 20,
child: Scrollbar(
controller: scrollController,
interactive: true,
// thumbVisibility: true,
radius: const Radius.circular(10),
// trackVisibility: true,
thickness: 10,
child: ListView.separated(
cacheExtent: 9999,
controller: scrollController,
itemCount: widget.appops.searchAppList.length,
itemBuilder: (BuildContext context, int index) {
final appls = widget.appops.searchAppList[index];
String currentLetter = widget.getFirstLetter(appls.appName);
// Check if the separator should be displayed
bool shouldDisplaySeparator =
index == 0 || currentLetter != widget.getFirstLetter(widget.appops.searchAppList[index - 1].appName);
return widget.appops.searchAppList.isNotEmpty
? AppListItem(
appls: appls,
themeTextColor: widget.themeTextColor,
addToDock: widget.addToDock,
loadApps: widget.loadApps,
appops: widget.appops,
sysBrightness: widget.sysBrightness,
dockIconList: widget.dockIconList,
shouldDisplaySeparator: shouldDisplaySeparator,
currentLetter: currentLetter)
: const Center(
child: Text('Loading...'),
);
},
separatorBuilder: (BuildContext context, int index) {
return const SizedBox.shrink(); // Return an empty separator when the letter doesn't change
// }
// }
},
),
)),
],
)),
],
);
},
),
);
使用 device_apps: ^2.2.0 显示应用程序及其图标
如何防止它重建图标图像/小部件?
如果你返回的是Scaffold并使用AppBar。您的小部件将自动具有顶部填充、与状态栏的距离。
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Text(),
);
}
但是如果您返回的是另一个像 DraggableScrollableSheet 这样的小部件,则必须使用 SafeArea 小部件。该小部件将根据颤振计算给出顶部填充。
@override
Widget build(BuildContext context) {
return SafeArea(
child: DraggableScrollableSheet (),
);
}
当然,在让 flutter 给出顶部屏幕与状态栏之间的距离后,您可以添加填充小部件以给出更大的距离。
@override
Widget build(BuildContext context) {
return SafeArea(
child: Padding(
padding: const EdgeInsets.only(top: 8),
child: DraggableScrollableSheet ();
),
);
}