gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 12:29:52 25 4
gpt4 key购买 nike

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

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

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

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

1[enter image description here

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

问候,鲯鳅

  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 上放置一个固定高度的容器。但也许我不明白你的问题。如果这不起作用,请发布您的代码和您收到的错误消息。

screenshot

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"),
),
);
}),
),
),
],
),
),
);
}
}

关于dart - 如何在 ListView 内 flutter 中创建一行可滚动的文本框或小部件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46222788/

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