我正在使用模態底部表來顯示用戶面臨的挑戰串列。當用戶點擊一個圖示時:底部作業表出現并建立串列。這一切都很好。
我在主螢屏上使用滾動控制器來獲取另一個串列。當用戶添加一個專案時,我呼叫 _scrollDown(),這是我創建的一個用于自動向下滾動(在主螢屏上)的方法,它作業正常。我希望在底部作業表中具有相同的行為。問題是我不知道在哪里可以呼叫我的 _scrollDown() 方法。用戶單擊“我的挑戰”,然后出現底部作業表并建立串列....然后它應該向下滾動...但是我看不到代碼中可以添加方法的位置...
這是模式表代碼:(手勢檢測器位于用于獲取挑戰串列的圖示上)
GestureDetector(
onTap: () {
showModalBottomSheet<void>(
isScrollControlled: true,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(20.0),
topRight: Radius.circular(20.0),
),
),
context: context,
builder: (BuildContext context) {
return Container(
height: MediaQuery.of(context).size.height - 80,
child: SingleChildScrollView(
padding: EdgeInsets.only(
bottom:
MediaQuery.of(context).viewInsets.bottom),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
alignment: Alignment.topRight,
child: IconButton(
icon: Icon(
Icons.cancel,
color: Colors.red[900],
size: uD.largeFont3,
),
onPressed: () {
Navigator.pop(context);
}),
),
Container(
alignment: Alignment.topCenter,
margin: const EdgeInsets.only(bottom: 20),
child: Text(
uD.selectedIoLanguage == Language.french
? 'Mes défis'.toUpperCase()
: 'My Challenges'.toUpperCase(),
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.indigo[900],
fontSize: uD.largeFont3,
),
),
),
Container(
decoration: BoxDecoration(
border: Border(
top: BorderSide(
color: Colors.orange[200]!,
width: 3,
)),
gradient: LinearGradient(
colors: [
Colors.orange[200]!,
Colors.orange[50]!,
Colors.orange[100]!,
Colors.orange[200]!
],
begin: Alignment.topLeft,
end: Alignment.bottomLeft,
stops: [0, 0.2, 0.5, 0.8])),
height:
MediaQuery.of(context).size.height - 163,
child: uD.defis.length > 0
? ListView.builder(
controller: _scrollController,
cacheExtent: 2000,
padding: const EdgeInsets.all(20),
itemCount: uD.defis.length,
itemBuilder: (context, index) {
return MyDefisCard(
index: index,
score: uD.defis[index].score,
date: uD.defis[index].date,
time: uD.defis[index].time,
uj5u.com熱心網友回復:
將您的底部作業表生成器結果移動到 StatefulWidget 中,假設它被稱為BottomSheetContent
showModalBottomSheet<void>(
isScrollControlled: true,
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(20.0),
topRight: Radius.circular(20.0),
),
),
context: context,
builder: (BuildContext context) => BottomSheetContent(),
);
在新小部件內,創建一個新小部件ScrollController(請注意,在您的代碼中,您使用的是與上一條路線中的串列相同的 ScrollController)
class _BottomSheetContentState extends State<BottomSheetContent> {
ScrollController _scrollController = ScrollController();
@override
void initState() {
super.initState();
WidgetsBinding.instance!.addPostFrameCallback((_) {
// _scrollController.animateTo()
// same logic as in _scrollDown
});
}
@override
Widget build(BuildContext context) {
// return bottom sheet content
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/437677.html
