如何将小部件带到 ListViewBuilder 的下一行?扑
How to bring widgets to next line in ListViewBuilder? flutter
过去几个月我一直在使用 flutter,现在我遇到了一个问题,我得到了一个标签名称列表,我必须设计一个 ui 来向用户显示标签,如下图所示.
,
当我使用 ListViewBuilder 和 Row 来显示标签时,它呈现了 stragint 行中的所有小部件,我收到 renderfelx 溢出错误,如下图所示。.
这是我的代码的样子-
Container(
height: 40,
child: ListView.builder(
shrinkWrap: true,
itemCount: brandmodels[index].tags.length,
itemBuilder: (context, index0) {
return Row(
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: CircleAvatar(
radius: 05,
backgroundColor: Color(0xffC4C4C4)),
),
Padding(
padding:
const EdgeInsets.only(right: 8.0),
child: Expanded(
child: Text(
brandmodels[index].tags[index0],
),
),
),
],
);
},
),
)
那么当达到屏幕的最大宽度时,如何将小部件带到下一行?
我刚刚将 ListView 更改为 Wrap 小部件。
(为了检查是否存在溢出问题,我将 Container 的宽度设置为 300。
并给出检查容器区域的背景颜色。)
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: _buildBody(),
floatingActionButton: FloatingActionButton(
onPressed: () {},
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
Widget _buildBody() {
List data = ['biscuits', 'cakes', 'drinks', 'snacks', 'food'];
return Container(
color: Colors.yellow[100],
height: 60,
width: 300,
child: Wrap(
direction: Axis.horizontal,
children: data.map((item) {
return Row(
mainAxisSize: MainAxisSize.min,
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: CircleAvatar(
radius: 05, backgroundColor: Color(0xffC4C4C4)),
),
Padding(
padding: const EdgeInsets.only(right: 8.0),
child: Expanded(
child: Text(
item,
),
),
),
],
);
}).toList(),
),
);
}
}
过去几个月我一直在使用 flutter,现在我遇到了一个问题,我得到了一个标签名称列表,我必须设计一个 ui 来向用户显示标签,如下图所示.
当我使用 ListViewBuilder 和 Row 来显示标签时,它呈现了 stragint 行中的所有小部件,我收到 renderfelx 溢出错误,如下图所示。
这是我的代码的样子-
Container(
height: 40,
child: ListView.builder(
shrinkWrap: true,
itemCount: brandmodels[index].tags.length,
itemBuilder: (context, index0) {
return Row(
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: CircleAvatar(
radius: 05,
backgroundColor: Color(0xffC4C4C4)),
),
Padding(
padding:
const EdgeInsets.only(right: 8.0),
child: Expanded(
child: Text(
brandmodels[index].tags[index0],
),
),
),
],
);
},
),
)
那么当达到屏幕的最大宽度时,如何将小部件带到下一行?
我刚刚将 ListView 更改为 Wrap 小部件。
(为了检查是否存在溢出问题,我将 Container 的宽度设置为 300。
并给出检查容器区域的背景颜色。)
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: _buildBody(),
floatingActionButton: FloatingActionButton(
onPressed: () {},
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
Widget _buildBody() {
List data = ['biscuits', 'cakes', 'drinks', 'snacks', 'food'];
return Container(
color: Colors.yellow[100],
height: 60,
width: 300,
child: Wrap(
direction: Axis.horizontal,
children: data.map((item) {
return Row(
mainAxisSize: MainAxisSize.min,
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: CircleAvatar(
radius: 05, backgroundColor: Color(0xffC4C4C4)),
),
Padding(
padding: const EdgeInsets.only(right: 8.0),
child: Expanded(
child: Text(
item,
),
),
),
],
);
}).toList(),
),
);
}
}