相关文章推荐
没人理的大熊猫  ·  金华开发区农事服务让粮食颗粒归仓·  6 月前    · 
飞奔的铁板烧  ·  thymeleaf ...·  7 月前    · 
爱运动的灌汤包  ·  国家金融监督管理总局·  7 月前    · 
酷酷的橙子  ·  “丑娘”张少华:人丑心更丑,年轻时做下恶事, ...·  7 月前    · 
一直单身的丝瓜  ·  阿里云验证码功能对接,使用临时凭证时遇到的坑 ...·  8 月前    · 
Code  ›  Flutter ScrollController不能正常工作开发者社区
https://cloud.tencent.com/developer/ask/sof/1365653/answer/1879707
率性的大熊猫
11 月前
首页
学习
活动
专区
工具
TVP 最新优惠活动
返回腾讯云官网
提问

问 Flutter ScrollController不能正常工作

Stack Overflow用户
提问于 2021-06-04 22:09:46
EN

在我的flutter应用程序中,我尝试使用scrollController在listBuilder中滚动列表视图,但ScrollController无法正常工作。它不会显示错误或异常,但列表不会滚动。即使我使用滚动控制器jumpTo或animateTo,它也不能工作。即使我给出了最大值,比如animateTo和jumpTo中的20000和1000,它也不会滚动。

代码语言: javascript
复制
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
class Class extends StatefulWidget {
final int data;
final List list;
Class({required this.data, required this.list});
@override
_ClassState createState() => _ClassState();
class _ClassState extends State<Class> with TickerProviderStateMixin {
late Animation<double> animation;
late AnimationController animationController;
late double height = 0;
final List lst = [
 [true, true, true, true],
[false, false, false, false],
[true, false, false, false],
[true, false, false, false],
[true, false, false, false],
[false, false, false, false],
[true, false, false, false],
[false, false, false, false],
[true, false, false, false],
[false, false, false, false],
[true, false, false, false],
[false, false, false, false],
[true, false, false, false],
[false, false, false, false],
[true, false, false, false],
void initState() {
  super.initState();
animationController =
    AnimationController(vsync: this, duration: Duration(seconds: 5));
animation = Tween<double>(begin: 0, end: 200).animate(animationController)
  ..addListener(() {
    setState(() {});
  ..addStatusListener((status) {});
animationController.forward();
Widget container(data) {
return Stack(
  children: [
    GestureDetector(
      onTap: () {
        setState(() {
          animationController.forward();
      child: Transform.translate(
        offset: Offset(0, animation.value),
        child: Container(
          color: lst[data][0] ? Colors.black : Colors.transparent,
          height: MediaQuery.of(context).size.height / 4,
          width: MediaQuery.of(context).size.width / 4,
          child: Text('$data'),
 @override
 Widget build(BuildContext context) {
return container(widget.data);
 @override
 void dispose() {
super.dispose();
animationController.dispose();
class Yes extends StatefulWidget {
 @override
_YesState createState() => _YesState();
 class _YesState extends State<Yes> {
   List lst = [
[true, true, true, true],
[false, false, false, false],
[true, false, false, false],
[true, false, false, false],
[true, false, false, false],
[false, false, false, false],
[true, false, false, false],
[false, false, false, false],
[true, false, false, false],
[false, false, false, false],
[true, false, false, false],
[false, false, false, false],
[true, false, false, false],
[false, false, false, false],
[true, false, false, false],
 ScrollController _scrollController = ScrollController();
 void initState() {
if (_scrollController.hasClients) {
  super.initState();
  // _scrollController.animateTo(0,
  //     // _scrollController.offset + MediaQuery.of(context).size.height/6,
  //     duration: Duration(seconds: 2),
  //     curve: Curves.linear);
  _scrollController.jumpTo(_scrollController.position.maxScrollExtent);
 @override
 Widget build(BuildContext context) {
    return ListView(
  controller: _scrollController,
  //  physics: NeverScrollableScrollPhysics(),
  children: [
    ListView.builder(
          // physics: NeverScrollableScrollPhysics(),
        reverse: true,
        itemCount: lst.length,
        controller: _scrollController,
        itemBuilder: (BuildContext context, int position) {
          return Stack(children: [Class(data: position, list: lst)]);
   }
2 757 0 票数 1
EN
flutter
flutter-animation
flutter-listview
flutter-scrollbar

Stack Overflow用户

发布于 2021-06-05 02:27:35

您的代码不够清楚,但作为示例尝试一下,效果很好

代码语言: javascript
复制
  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: ListExample(),
class ListExample extends StatelessWidget {
  ListExample({Key? key}) : super(key: key);
  final ScrollController controller = ScrollController();
  double pos = 0;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: [
          Expanded(
            child: ListView.builder(
              controller: controller,
              itemCount: 10000,
              itemBuilder: (context, index) {
                return Text('item $index');
          ElevatedButton(
              onPressed: () {
                controller.jumpTo(pos+=250);
 
推荐文章
没人理的大熊猫  ·  金华开发区农事服务让粮食颗粒归仓
6 月前
飞奔的铁板烧  ·  thymeleaf th:each循环js语句 - CSDN文库
7 月前
爱运动的灌汤包  ·  国家金融监督管理总局
7 月前
酷酷的橙子  ·  “丑娘”张少华:人丑心更丑,年轻时做下恶事,晚年悔恨交加
7 月前
一直单身的丝瓜  ·  阿里云验证码功能对接,使用临时凭证时遇到的坑,报错code: 400, Specified header x-acs-security-token is mandatory for this acti
8 月前
今天看啥   ·   Py中国   ·   codingpro   ·   小百科   ·   link之家   ·   卧龙AI搜索
删除内容请联系邮箱 2879853325@qq.com
Code - 代码工具平台
© 2024 ~ 沪ICP备11025650号