草庐IT

dart - Flutter 拖放 ListView hasSize 不是真的

coder 2023-07-22 原文

有人可以启动一个快速的 flutter 项目并将 main.dart 替换为以下内容,看看我做错了什么吗?我正在尝试在 ListView 中进行拖放操作。

我什至不确定这是正确的方法,所以如果不正确,请告诉我。

我现在得到的错误是:

另一个异常被抛出:'package:flutter/src/rendering/box.dart': Failed assertion: line 1446 pos 12: 'hasSize': is not true.

import 'package:flutter/material.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final title = 'Basic List';

    var tile1 = new Material(child:
       new ListTile(
          leading: new Icon(Icons.photo),
          title: new Text('Row 1'),
          trailing: new Icon(Icons.reorder),

    ));


    var tile2 = new Material(
        child:
          new ListTile(
            leading: new Icon(Icons.photo),
            title: new Text('Row 2'),
            trailing: new Icon(Icons.reorder),
    ));


    return new MaterialApp(
      title: title,
      home: new Scaffold(
        appBar: new AppBar(
          title: new Text(title),
        ),
        body:
        new GestureDetector(
          onVerticalDragStart: startDrag,
          onVerticalDragEnd: endDrag,
          child: new ListView(
            shrinkWrap: true,
            children: [
                  new Flex (

                    children: <Widget>[
                      new Flexible(
                        child: new Draggable(child: tile1, feedback: 
tile1),
                      ),
                      new Flexible(
                        child: new Draggable(child: tile2, feedback: 
tile2),
                      ),
                    ],
                    direction: Axis.vertical,
                    mainAxisAlignment: MainAxisAlignment.start,
                    mainAxisSize: MainAxisSize.min,
                  ),
            ],
          ),
        ),
      ),
    );
  }

  void startDrag(DragStartDetails event) {}

  void endDrag(DragEndDetails event) {}
}

谢谢

最佳答案

在@Darky 的帮助下解决了 hasSize 问题,这是完成的可排序 ListView 示例:

https://github.com/marchampson/FluterSortableListView

import 'package:flutter/material.dart';

void main() => runApp(new MyApp());

class MyApp extends StatefulWidget {
  @override
  MyAppState createState() => new MyAppState();
}

class MyAppState extends State<MyApp> {

  List<String> rows = new List<String>()
    ..add('Row 1')
    ..add('Row 2')
    ..add('Row 3')
    ..add('Row 4');

  void _handleAccept(int data, int index) {
    setState(() {
      String imageToMove = rows[data];
      rows.removeAt(data);
      rows.insert(index, imageToMove);
    });
  }

  @override
  Widget build(BuildContext context) {

    final title = 'Sortable ListView';

    return new MaterialApp(
      title: title,
      home: new Scaffold(
        appBar: new AppBar(
          title: new Text(title),
        ),
        body:
        new LayoutBuilder(builder: (context, constraint) {
          return new ListView.builder(
            itemCount: rows.length,
            addRepaintBoundaries: true,
            itemBuilder: (context, index) {
              return new LongPressDraggable(
                key: new ObjectKey(index),
                data: index,
                child: new DragTarget<int>(
                  onAccept: (int data) {
                    _handleAccept(data, index);
                  },
                  builder: (BuildContext context, List<int> data, List<dynamic> rejects) {
                    return new Card(
                        child: new Column(
                          children: <Widget>[
                            new ListTile(
                                leading: new Icon(Icons.photo),
                                title: new Text(rows[index])
                            ),
                          ],
                        )
                    );
                  },
                  onLeave: (int data) {
                    // Debug
                    print('$data is Leaving row $index');
                  },
                  onWillAccept: (int data) {
                    // Debug
                    print('$index will accept row $data');

                    return true;
                  },
                ),
                onDragStarted: () {
                  Scaffold.of(context).showSnackBar(new SnackBar (
                    content: new Text("Drag the row onto another row to change places"),
                  ));

                },
                onDragCompleted: () {
                  print("Finished");
                },
                feedback: new SizedBox(
                    width: constraint.maxWidth,
                     child: new Card (
                      child: new Column(
                        children: <Widget>[
                          new ListTile(
                            leading: new Icon(Icons.photo),
                            title: new Text(rows[index]),
                            trailing: new Icon(Icons.reorder),
                          ),
                        ],
                      ),
                      elevation: 18.0,
                    )
                ),
                childWhenDragging: new Container(),
              );
            },
          );
        }),
      ),
    );
  }
}

关于dart - Flutter 拖放 ListView hasSize 不是真的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49369147/

有关dart - Flutter 拖放 ListView hasSize 不是真的的更多相关文章

  1. 【Java 面试合集】HashMap中为什么引入红黑树,而不是AVL树呢 - 2

    HashMap中为什么引入红黑树,而不是AVL树呢1.概述开始学习这个知识点之前我们需要知道,在JDK1.8以及之前,针对HashMap有什么不同。JDK1.7的时候,HashMap的底层实现是数组+链表JDK1.8的时候,HashMap的底层实现是数组+链表+红黑树我们要思考一个问题,为什么要从链表转为红黑树呢。首先先让我们了解下链表有什么不好???2.链表上述的截图其实就是链表的结构,我们来看下链表的增删改查的时间复杂度增:因为链表不是线性结构,所以每次添加的时候,只需要移动一个节点,所以可以理解为复杂度是N(1)删:算法时间复杂度跟增保持一致查:既然是非线性结构,所以查询某一个节点的时候

  2. ruby-on-rails - 只有当不是 nil 时才执行映射? - 2

    如果names为nil,则以下中断。我怎样才能让这个map只有在它不是nil时才执行?self.topics=names.split(",").mapdo|n|Topic.where(name:n.strip).first_or_create!end 最佳答案 其他几个选项:选项1(在其上执行map时检查split的结果):names_list=names.try(:split,",")self.topics=names_list.mapdo|n|Topic.where(name:n.strip).first_or_create!e

  3. ruby-on-rails - 我真的需要在 Rails 中使用 csv gem 吗? - 2

    我的问题很简单:我是否必须在使用RubyonRails的类上require'csv'?如果我打开一个railsconsole并尝试使用CSVgem它可以工作,但我必须在文件中这样做吗? 最佳答案 CSVlibrary是ruby​​标准库的一部分;它不是gem(即第三方库)。与所有标准库(与核心库不同)一样,csv不会由ruby​​解释器自动加载。所以是的,在您的应用程序中某处您确实需要要求它:irb(main):001:0>CSVNameError:uninitializedconstantCSVfrom(irb):1from/Us

  4. ruby-on-rails - Rails 格式验证——字母数字,但不是纯数字 - 2

    什么是测试格式验证的最佳方法让我们说一个用户名,使用字母数字的正则表达式,但不是纯数字?我一直在我的模型中使用以下验证validates:username,:format=>{:with=>/^[a-z0-9]+[-a-z0-9]*[a-z0-9]+$/i}数字用户名(例如“342”)通过了验证,这是我不想要的。 最佳答案 您想“向前看”一封信:/\A(?=.*[a-z])[a-z\d]+\Z/i 关于ruby-on-rails-Rails格式验证——字母数字,但不是纯数字,我们在Sta

  5. ruby - 强制浏览器下载文件而不是打开文件 - 2

    我要下载http://foobar.com/song.mp3作为song.mp3,而不是让Chrome在其native中打开它浏览器中的播放器。我怎样才能做到这一点? 最佳答案 您只需要确保发送这些header:Content-Disposition:attachment;filename=song.mp3;Content-Type:application/octet-streamContent-Transfer-Encoding:binarysend_file方法为您完成:get'/:file'do|file|file=File.

  6. ruby - 更改 $LOAD_PATH 时,为什么使用 unshift 而不是 push? - 2

    我发现ruby加载路径是一个数组,很多项目都是这样使用的:$:.unshift(File.expand_path("../../lib",__FILE__))可以将本地文件添加到ruby路径数组的前面,方便我们require或者load。所以,我希望知道为什么我们不使用push将文件添加到数组的末尾? 最佳答案 假设您有一个“date.rb”文件(为什么不呢)并且您想要加载这个文件,而不是标准库日期。如果您使用追加,当您调用require'date'时您的文件将永远不会被加载,因为它位于数组的末尾并且标准日期会在之前找到。因此,如果

  7. ruby - 如何排序不是简单的哈希(哈希的哈希) - 2

    我有一个这样的哈希{55=>{:value=>61,:rating=>-147},89=>{:value=>72,:rating=>-175},78=>{:value=>64,:rating=>-155},84=>{:value=>90,:rating=>-220},95=>{:value=>39,:rating=>-92},46=>{:value=>97,:rating=>-237},52=>{:value=>73,:rating=>-177},64=>{:value=>69,:rating=>-167},86=>{:value=>68,:rating=>-165},53=>{:va

  8. ruby - Unicorn 使用 `reload` 而不是 `restart`? - 2

    我在这里对我的部署策略有点困惑,在什么情况下部署时我想向unicorn发送reload信号?例如在我的例子中它会是这样的:sudokill-sUSR2`cat/home/deploy/apps/my_app/current/tmp/pids/unicorn.pid`我一直在通过杀死那个pid来部署我的应用程序,然后通过类似的东西再次启动unicorn:bundleexecunicorn-cconfig/unicorn/production.rb-Eproduction-D我只是想知道为什么要使用重新加载?我可以通过这样做获得部署的任何性能吗? 最佳答案

  9. ruby - 使用 Ruby FileUtils 而不是 Bash 命令的好处? - 2

    使用FileUtils方法有什么好处http://ruby-doc.org/core/classes/FileUtils.html比等效的Bash命令? 最佳答案 除此之外,您不必担心确保您的目标平台安装了您正在使用的特定工具这一事实,以及正确引用shell异常的问题(如果您的目标是特别有问题的)Windows和Unix-alikes——尽管有Cygwin、GNUWin32等),如果你使用Ruby的FileUtils,你有一个Ruby函数调用的中等大小的开销,而如果你使用外部实用程序,你有相当大的开销来启动一个外部进程的每一次“调用

  10. ruby-on-rails -/usr/local/lib/libz.1.dylib,文件是为 i386 构建的,它不是被链接的体系结构 (x86_64) - 2

    在我的mac上安装几个东西时遇到这个问题,我认为这个问题来自将我的豹子升级到雪豹。我认为这个问题也与macports有关。/usr/local/lib/libz.1.dylib,filewasbuiltfori386whichisnotthearchitecturebeinglinked(x86_64)有什么想法吗?更新更具体地说,这发生在安装nokogirigem时日志看起来像:xslt_stylesheet.c:127:warning:passingargument1of‘Nokogiri_wrap_xml_document’withdifferentwidthduetoproto

随机推荐