我正在尝试制作一个自动滚动到最新数据点的 ListView。我厌倦了通过使用 .jumpTo 方法制作一个 _scrollToBottom 函数来做到这一点。
但是我在应用程序和'child.parentData != null': is not true.调试控制台中看到一个空白屏幕
。
关于如何实现自动滚动的任何建议?
这是我当前代码的相关部分:
ScrollController _scrollController = ScrollController();
_scrollToBottom(){ _scrollController.jumpTo(_scrollController.position.maxScrollExtent);
}
@override
Widget build(BuildContext context) {
return StreamBuilder(
stream: DataShareWidget.of(context).stream,
builder: (BuildContext context, AsyncSnapshot snapshot){
if(snapshot.hasError){ return Text(snapshot.error);}
if(snapshot.hasData){
_dataFormat(snapshot.data);
return ListView.builder(
itemCount: _listViewData.length,
controller: _scrollController,
reverse: true,
shrinkWrap: true,
itemBuilder: (context, index) {
_scrollToBottom();
return ListTile(
title: AutoSizeText(_listViewData[index], maxLines: 2),
dense: true,
);
},
);
}
}
);
}
Run Code Online (Sandbox Code Playgroud) 我想显示大小可变的小部件列表。
Flutter 的 Wrap 非常适合我的布局需求。
final List<dynamic> someItems;
return SingleChildScrollView(
child: Wrap(
children: someItems.map((item) => _createTile(context, item)).toList(),
),
scrollDirection: Axis.vertical,
);
Run Code Online (Sandbox Code Playgroud)
但是一个致命的问题是,Wrap 不能懒惰地创建小部件。例如,如果上图中显示的 Tile 小部件少于 100 个,但数据列表的数量更多,Wrap 甚至会创建不可见的小部件。
// The result of taking a log once for each item when configuring Wrap with a list of 1000 data.
I/flutter ( 4437): create Widget 0
I/flutter ( 4437): create Widget 1
I/flutter ( 4437): create Widget 2
I/flutter ( 4437): create Widget 3
I/flutter ( 4437): create Widget 4
I/flutter ( …Run Code Online (Sandbox Code Playgroud) 我有一个包含每个项目的音乐的列表。如果我关闭屏幕,我会更改index当前位置BottomNavigationPage并调用停止函数来停止音频,但有时这不起作用。如果歌曲正在加载过程中或用户速度非常快,歌曲将继续在不同页面上播放。
我使用https://pub.dev/packages/audioplayers插件。
这是我的缩小代码示例完整工作演示:编辑::
因此,我按照 @Ringil 的提示提供了一个完整的工作示例,其中包含我在这里面临的实际问题。这是我的代码:
import 'package:audioplayers/audioplayers.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int currentIndex =0;
@override
Widget build(BuildContext context) …Run Code Online (Sandbox Code Playgroud) dart flutter flutter-layout flutter-listview flutter-pageview
现在我正在使用一列来显示几个小部件:
Column(
children: [
Text('1'),
Text('2'),
Spacer(),
Text('3'),
Text('4'),
],
),
Run Code Online (Sandbox Code Playgroud)
所以屏幕看起来像这样:
---
1
2
3
4
---
Run Code Online (Sandbox Code Playgroud)
它允许我始终在小部件的顶部和底部对齐'1'和,而不管其大小。'2''3''4'
然而,当小部件变得太小时,它就会溢出:
---
1
2
3
---
4 // <- Overflow
Run Code Online (Sandbox Code Playgroud)
因此,我想用 so 达到相同的结果ListView:
'1' '2'顶部对齐和'3' '4'底部对齐。'2'当小部件不够高时,和之间不再有多余的空间'3',但用户可以向下滚动。我无法使用Spacera ListView。它向我抛出错误:
Failed assertion: line 1940 pos 12: 'hasSize'
Run Code Online (Sandbox Code Playgroud)
实施它的正确方法是什么?
我希望创建以下内容:当到达内部 Listview 的顶部或底部时,我想继续在顶部 Listview 中滚动。见动图:
一个选项是在到达底部时将内部 Listview 的物理设置为 NeverScrollablePhysics(使用控制器的监听器),但是如果您想再次向上滚动,这将不起作用。
请参阅下面的代码,提前致谢!
class TestAppHomePage extends StatefulWidget {
@override
TestAppHomePageState createState() => new TestAppHomePageState();
}
class TestAppHomePageState extends State<TestAppHomePage> {
ScrollController _scrollController = ScrollController();
@override
void initState() {
print('set up');
}
@override
Widget build(BuildContext context) {
return new Scaffold(
body: Container(
color: Colors.green,
child: ListView(
primary: false,
// controller: _scrollController,
children: <Widget>[topWidget(), topWidget(), topWidget(), topWidget()],
),
),
);
}
Widget topWidget() {
return Card(
color: Colors.purple,
margin: EdgeInsets.all(16),
child: Container(
height: …Run Code Online (Sandbox Code Playgroud) 我正在使用scrollable_positioned_list包,并让它渲染一个大的动态列表。它运作得很好。但是,我需要我的列表有一个滚动条(类似这样)。到目前为止,这是不可能的。
\n有谁知道该怎么做?
\n我的代码如下所示:
\nScrollbar(\n child: ScrollablePositionedList.builder(\n physics: const ClampingScrollPhysics(\n parent: AlwaysScrollableScrollPhysics(),\n ),\n itemCount: posts.length + 1,\n itemBuilder: (context, index) {\n if (index == 0) {\n return Container(\n height: 200,\n color: Colors.green,\n child: const Center(\n child: Text(\'post content\'),\n ),\n );\n } else if (posts[index - 1].isRoot) {\n return Container(\n padding: const EdgeInsets.symmetric(vertical: 15),\n margin: const EdgeInsets.symmetric(vertical: 5),\n color: Colors.redAccent,\n child: Text(\'ROOT COMMENT, index: ${index - 1}\'),\n );\n } else {\n return Container(\n padding: const …Run Code Online (Sandbox Code Playgroud) dart flutter flutter-layout flutter-listview flutter-scrollbar
我有一个 ListView.builder,它返回一个复选框及其标签。此列表视图将返回将显示在屏幕上的复选框和标签的列表。目前,我只是创建一个全局变量 isChecked 并使用 onChanged 回调和该值更改复选框的值。但它会更改屏幕上显示的所有复选框的值。
我需要一种方法来单独处理每个复选框而不修改其他复选框值。并记录勾选复选框的数量。我为此使用了 checkboxtile 小部件。
我想创建一个水平列表视图构建器,每页显示一个项目(同时显示下一个和上一个项目的提示),如下图所示:
我当前的代码是:
Container(
height: 240,
alignment: Alignment.center,
child: ListView.builder(
scrollDirection: Axis.horizontal,
shrinkWrap: true,
physics: PageScrollPhysics(),
itemCount: homeState.questionList.length,
itemBuilder: (context, index) =>
Container(
width: width,
child: Center(
child: Container(
width: width*0.9,
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(17.7)),
color: Color(0xff181818),
),
child: Text(questionList[index].text)
),
),
);
),
)
Run Code Online (Sandbox Code Playgroud)
我当前的代码能够在每页显示一个项目,但它不会显示下一个项目的提示,如照片中的箭头所示。欢迎任何建议。谢谢。
我在我的 flutter 应用程序中使用 PageView 构建器来显示可滚动卡片。为此,我首先创建了一个小部件_buildCarousel和小部件_buildCarouselItem,以使用我为卡片定义的小部件定义的方式显示它。我还为事件数据定义了一个地图列表 ( eventsData )。
Widget _buildCarousel(BuildContext context) {
List<Map> EventsData = [
{
"title": "Event ABC",
"subTitle": "Organised by XYZ",
"desc": "Event description.",
"date": "13th Jan, 2022",
"time": "4:00 pm",
"lastDate": "11th Jan, 2022"
},
{
"title": "Event MNT",
"subTitle": "Organised by XYZ",
"desc": "Event description.",
"date": "13th Jan, 2022",
"time": "4:00 pm",
"lastDate": "11th Jan, 2022"
},
{
"title": "Event WOQ",
"subTitle": "Organised by STU",
"desc": "Event description." ,
"date": "13th …Run Code Online (Sandbox Code Playgroud) 如何在滚动过程中保持某些元素固定(以及可滚动部分之上)?它是否需要位于 SingleChildScrollView 内?
为我的问题做了一个直观的例子:
我需要蓝色部分可滚动,并且它位于需要保持固定的绿色部分后面。
这是我的代码的简化部分:
body: SingleChildScrollView(
child: ListView(
children: [
Obx(()=>Container(THIS CONTAINER SHOULD STAY FIXED DURING SCROLL)),
Obx(()=>Column(THIS PART SHOULD BE SCROLLABLE)),
Obx(()=>Text(THIS PART SHOULD BE SCROLLABLE)),
Obx(()=>Row(THIS PART SHOULD BE SCROLLABLE))
],
)
),
Run Code Online (Sandbox Code Playgroud) scroll flutter flutter-layout flutter-listview singlechildscrollview