dart - 如何在 ListView 内 flutter 中创建一行可滚动的文本框或小部件?

1. 请有人告诉我如何在 ListView 中创建一行可左右滚动的文本框。我可以看到我正在尝试在有限宽度的 ListView 内定义无限宽度。但是,无法找到任何解决方法。

如果简单地在customscrollview 中注释scrolldirection 属性(即,将scrolldirection 更改为垂直),下面提到的代码绝对可以正常工作。但是,我正在寻找水平滚动。 我试图解决这个问题,但没有运气。

有人可以帮忙,让我知道我哪里做错了。

  1. 另外,我们如何像在 Android 中那样在 Flutter 中创建或扩展布局? 我已经包含了我正在创建的布局的屏幕截图以供引用:

我已经发布了下面的代码来重现同样的错误;

问候, 鲯鳅

  import 'package:flutter/material.dart';
  import 'package:flutter/rendering.dart';
   import 'package:flutter/widgets.dart';
       void main(){
       runApp(new AddNewBlock());
       }

 class AddNewBlock extends StatefulWidget{

@override
State<StatefulWidget> createState() {
return new _AddNewBlockState();
    }
  }

   class _AddNewBlockState extends State<AddNewBlock>{


    @override
   Widget build(BuildContext context) {
   return new MaterialApp(
            title: 'Add New Block',
  debugShowCheckedModeBanner: false,

  home: new Scaffold(

    body: new ListView(
      shrinkWrap: true,

      children: <Widget>[
        new Container(
          margin: const EdgeInsets.only(
            left: 16.0,top: 24.0, bottom: 8.0,
          ),
          child: new Text(
            'Add New Block',
          style: new TextStyle(
            fontSize: 18.0,
            fontWeight: FontWeight.bold,
            color: Colors.teal[300],
    ),
          ),
        ),
        new Container(
          margin: const EdgeInsets.only(left: 16.0, top: 16.0, bottom: 8.0),
          child: new Text ('Block Name'),
        ),

        new Container(
          margin: const EdgeInsets.fromLTRB(16.0, 8.0, 0.0, 8.0),
          child: new Text ('Block A1',
          style: new TextStyle(
            fontSize: 16.0,
            fontWeight: FontWeight.bold
          ),),
        ),
        new Divider(color: Colors.grey,),
        new Container(
          margin: const EdgeInsets.only(left: 16.0, top: 8.0,bottom: 8.0),
          child: new Text('NO. OF FLOORS',
          style: new TextStyle(
            fontSize: 12.0,
          ),
          ),
        ),



       new Container(
                    child: new Row(
                      children: <Widget>[
                        new Flexible(
                          child: new CustomScrollView(
                            shrinkWrap: true,
                            scrollDirection: Axis.horizontal,
                            slivers: <Widget>[
                          new SliverPadding(
                          padding: const EdgeInsets.all(20.0),
                          sliver: new SliverList(
                            delegate: new SliverChildListDelegate(
                              <Widget>[
                                const Text('this'),
                                const Text('is'),
                                const Text('scroll'),
                                const Text('view'),
                                const Text('inside'),
                                const Text('list'),
                                const Text('view'),
                                const Text('in'),
                                const Text('horizontal'),
                                const Text('direction')
                              ],
                            ),
                          ),
                        ),
                      ],
                          ),
                        ),
                      ],
                    ),
                  ),





      ],

    ),
  ),


  );
   }
}

最佳答案

我认为这很简单,只要你在水平 ListView 上放置一个固定高度的容器。但也许我不明白你的问题。如果这不起作用,请发布您的代码和您收到的错误消息。

import 'package:flutter/material.dart';
import 'package:flutter/gestures.dart';
import 'dart:collection';

void main() {
  runApp(new MaterialApp(home: new DemoApp()));
}

class DemoApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(title: new Text('Nested ListView Example')),
      body: new Center(
        child: new ListView(
          children: <Widget>[
            new Container(
              height: 80.0,
              child: new ListView(
                scrollDirection: Axis.horizontal,
                children: new List.generate(10, (int index) {
                  return new Card(
                    color: Colors.blue[index * 100],
                    child: new Container(
                      width: 50.0,
                      height: 50.0,
                      child: new Text("$index"),
                    ),
                  );
                }),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

https://stackoverflow.com/questions/46222788/

相关文章:

dart - 如何在 Flutter 中调度后台任务?

dart - Flutter:是否有可能创建 App Widgets (Android) 和 Tod

flutter - 单击屏幕上的文本字段/任何位置后,如何在 flutter 上隐藏软输入键盘?

flutter - 如何在 Dart 中对 Textfield onChange 进行去抖动?

flutter - Scoped Model、BLoC 模式、StreamBuilder 和 Inh

dart - 将带参数的函数传递给 VoidCallback

Flutter - 如何更改 TextField 提示颜色?

dart - 从 Flutter 记录大字符串

flutter - 如何为 flutter 添加自定义颜色?

mobile - 是否可以在 Linux 虚拟机上使用 Flutter 开发 iOS 应用程序?