在 SingleChildScrollView 中讓背景漸變填充整個螢屏的最佳方式是什么?我希望螢屏僅在必要時滾動(當專案和底部文本溢位螢屏時)。注意 Back 按鈕是一個 FAB。
注意:我嘗試過ConstrainedBox(但是當串列視圖中沒有溢位時,滾動很奇怪)
1) 完全填充背景和 2) 僅在串列視圖溢位時才激活滾動活動的最佳方法是什么?
我的代碼:
Widget body() {
return SingleChildScrollView(
child: Container(
decoration: background(),
child: Column(children: [
listViewReminderBuilder(),
SizedBox(height: 10),
Container(
padding: EdgeInsets.all(5),
child: bottomText()),
SizedBox(height: 10),
])));
}
BoxDecoration Background() {
return BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topRight,
end: Alignment.bottomLeft,
colors: [
topColor,
bottomColor,
],
));
}
這是上面代碼的結果如下所示。滾動按我的意愿作業(僅在串列視圖項溢位時滾動),但顯然背景不會填滿螢屏。

許多在線解決方案將我帶到了 ConstrainedBox。我的代碼和結果如下:
Widget body() {
_screenHeight = MediaQuery.of(context).size.height;
return SingleChildScrollView(
child: Container(
decoration: background(),
child: ConstrainedBox(
constraints: BoxConstraints(minHeight: _screenHeight),
child: Column(children: [
listViewReminderBuilder(),
SizedBox(height: 10),
Container(
padding: EdgeInsets.all(5),
child: bottomText()),
SizedBox(height: 10),
]))));
}
// background() was the same
結果。在這種情況下,背景很好地填滿了螢屏,但螢屏總是可滾動的,如果串列視圖項沒有溢位,螢屏看起來很奇怪。

1) 完全填充背景和 2) 僅在串列視圖溢位時才激活滾動活動的最佳方法是什么?
uj5u.com熱心網友回復:
首先:包裝不是一個好習慣ListView。SingleChildScrollView
解決方法:
僅將容器用于背景。通過設定寬度無窮大來填充整個螢屏。SingleChildScrollView如果您有ListView. 只需使用擴展小部件。
Widget body() {
return Container(
width: double.infinity,
// here you can set height if you want.
// but since we use Column,by default it will expand the maximum height.
decoration: background(),
child: Column(children: [
// using Listview will make you widget not overflowing.
// wrap with expanded to make it fill available screen.
Expanded(child: listViewReminderBuilder(),),
SizedBox(height: 10),
Container(padding: EdgeInsets.all(5), child: Text('Back')),
SizedBox(height: 10),
]));
}
我剛在 dartpad 上試過。它作業正常。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/521293.html
標籤:安卓扑镖移动的颤振布局
