gpt4 book ai didi

内容末尾的 Flutter listview 滚动屏幕

转载 作者:行者123 更新时间:2023-12-02 19:09:38 27 4
gpt4 key购买 nike

我想知道是否可以在 SingleChildScrollView 中拥有固定高度的垂直 ListView 以及垂直 ListView 中何时没有更多内容 滚动应用于整个屏幕 (SingleChildScrollView)

像这样:

SingleChildScrollView(
child: Column(
children: [
// some widgets ....

Container(
constraints: BoxConstraints(maxHeight: 200),
child: ListView.builder(
// other settings
scrollDirection: Axis.vertical,
),
)
]
)
)

最佳答案

我使用 Listview 而不是 SingleChildScrollView 但它是相同的:

import 'dart:async';

import 'package:flutter/material.dart';

void main() {
runApp(MyApp());
}

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(title: 'Flutter Demo', home: MyListView());
}
}

class MyListView extends StatelessWidget {
ScrollController _mainScrollController = ScrollController();
double listHeight = 370;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('AppBar'),
),
body: Container(
height: MediaQuery.of(context).size.height,
child: ListView(
controller: _mainScrollController,
children: <Widget>[

Container(height: listHeight,child: RapportList(parentScrollController: _mainScrollController)),
OtherElement(text: "Other element 1 which will be scrolled",),
OtherElement(text: "Other element 2 which will be scrolled",),
OtherElement(text: "Other element 3 which will be scrolled",),
OtherElement(text: "Other element 4 which will be scrolled",),
OtherElement(text: "Other element 5 which will be scrolled",),
],
),
),
);
}
}

class RapportList extends StatefulWidget {
final ScrollController parentScrollController;
RapportList({@required this.parentScrollController});
@override
_RapportListState createState() => _RapportListState();
}

class _RapportListState extends State<RapportList> {
ScrollPhysics physics = ScrollPhysics();
// NeverScrollableScrollPhysics()
ScrollController _listViewScrollController;


void listViewScrollListener(){
print("smth");
if(_listViewScrollController.offset >= _listViewScrollController.position.maxScrollExtent &&
!_listViewScrollController.position.outOfRange){
if(widget.parentScrollController.offset==0){
widget.parentScrollController.animateTo(50,duration: Duration(milliseconds: 200),curve: Curves.linear);
}
setState((){
physics = NeverScrollableScrollPhysics();
});
print("bottom");
}
}

void mainScrollListener(){
if(widget.parentScrollController.offset <= widget.parentScrollController.position.minScrollExtent &&
!widget.parentScrollController.position.outOfRange){
setState((){
if(physics is NeverScrollableScrollPhysics){
physics = ScrollPhysics();
_listViewScrollController.animateTo(_listViewScrollController.position.maxScrollExtent-50,duration: Duration(milliseconds: 200),curve: Curves.linear);
}
});
print("top");
}
}

@override
void setState(fn) {
super.setState(fn);
}
@override
void initState() {
_listViewScrollController = ScrollController();
_listViewScrollController.addListener(listViewScrollListener);

// TODO: implement initState
super.initState();
}
@override
Widget build(BuildContext context) {

widget.parentScrollController.addListener(mainScrollListener);
return ListView.builder(
controller: _listViewScrollController,
physics: physics,
shrinkWrap: true,
itemCount: 50,
itemBuilder: (context, index) {
return ListTile(
title: GestureDetector(
child: Row(
children: <Widget>[
Container(child: Text("text $index")),
],
),
),
);
},
);
}
}


class OtherElement extends StatelessWidget {
final String text;
OtherElement({this.text});
@override
Widget build(BuildContext context) {
return Container(
height: 100,
child: Center(child: Padding(
padding: const EdgeInsets.symmetric(horizontal:40.0),
child: Text(this.text,style:TextStyle(fontSize: 30)),
)),
);
}
}

github gist .

演示 gif:

enter image description here

关于内容末尾的 Flutter listview 滚动屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64500215/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com