我的 listView 包含許多專案,如下所示
Scaffold(
backgroundColor:Colors.blue
appbar:AppBar()
body: ListView(
children: <Widget>[
Text('hello'),
Divider(height: 2, color: Colors.black,),
Text('hello'),
Divider(height: 2, color: Colors.black,),
Text('hello'),
Divider(height: 2, color: Colors.black,),
Text('hello'),,
],
),
)
現在我需要隱藏第一個索引或使其不可見或位于應用欄或螢屏頂部,直到用戶向下滾動(例如正常滾動)時才會顯示..
我嘗試了以下但如果用戶向下滾動它不會再次顯示。我用 Stack 蓋住它,并用相同 Scaffold 顏色的容器欺騙它
Stack(
children[
Text('hello'),
Container(
color:Colors.blue
)
]
)
我知道這根本不是好方法。因為它將永遠隱藏
我該如何處理呢,謝謝
uj5u.com熱心網友回復:
要做到這一點,如果你想在應用欄下的第一個元素。請檢查我的代碼
var itemCount = 31; // change this value to view different result
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('hi'),
),
body: SingleChildScrollView(
child: Stack(children: [
Container(
height: MediaQuery.of(context).size.height,
width: MediaQuery.of(context).size.width,
),
Positioned(
top: itemCount <= 20 ? -15 : null,
right: 0,
child: Container(
height: MediaQuery.of(context).size.height,
width: MediaQuery.of(context).size.width,
child: ListView.builder(
padding: EdgeInsets.only(bottom: 70),
controller: ScrollController(
initialScrollOffset: itemCount < 31 ? itemCount 1 : 31),
shrinkWrap: true,
itemCount: itemCount,
itemBuilder: (BuildContext context, int index) {
return Column(
children: [
Container(child: Text('hello $index')),
Divider(
height: 2,
color: Colors.black,
),
Text(''),
],
);
},
),
))
])));
它會起作用,請更改 var itemCount = 31 ;值以查看結果
使用flutter_screenutil插件。所以它會適應所有的螢屏尺寸
Note That
my device display height: 756.0
my device display width : 360.0
my device display aspectRatio : 0.47619047619047616
so my ScrollOffset should be 31 , so i am use this value
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/450768.html
上一篇:如何在帶有列和list.builder的singlechildscrollview上方添加帶有textformfield的行?
下一篇:Tabview滾動行為
