Flutter 如何在展开的颤振列表中指定图元的宽度?
下面的代码表示一个项目列表,其中列表应为全宽,但列表中的元素可能不全宽。颤振似乎忽略了我对Flutter 如何在展开的颤振列表中指定图元的宽度?,flutter,flutter-listview,Flutter,Flutter Listview,下面的代码表示一个项目列表,其中列表应为全宽,但列表中的元素可能不全宽。颤振似乎忽略了我对SizedBox的宽度限制,并迫使它完全展开 class ExampleBadListWidth extends StatelessWidget { List<String> things = [ "1: This is a really really really really really really really really really really long thi
SizedBox
的宽度限制,并迫使它完全展开
class ExampleBadListWidth extends StatelessWidget {
List<String> things = [
"1: This is a really really really really really really really really really really long thing",
"2: This is a really really really really really really really really really really long thing",
"3: This is a really really really really really really really really really really long thing"
];
ExampleBadListWidth();
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(children: [
Expanded(
child: ListView.builder(
itemCount: things.length,
itemBuilder: _thingBuilder,
))
]));
}
Widget _thingBuilder(context, index) {
return SizedBox(width: 100, child: Text(things[index]));
}
}
类示例BadListWidth扩展无状态小部件{
列出事情=[
“1:这真是一件很长的事情”,
“2:这真是一件很长的事情”,
“3:这真是一件很长的事”
];
示例BadListWidth();
@凌驾
小部件构建(构建上下文){
返回脚手架(
正文:列(子项:[
扩大(
子项:ListView.builder(
itemCount:things.length,
itemBuilder:_thingBuilder,
))
]));
}
小部件_thingBuilder(上下文、索引){
return-SizedBox(宽度:100,子项:Text(things[index]));
}
}
您可以复制粘贴运行下面的完整代码您可以使用
Center
或Align
代码片段
Widget _thingBuilder(context, index) {
return Center(
child: SizedBox(
width: 100,
child: Text(things[index])),
);
}
或
工作演示
完整代码
import 'package:flutter/material.dart';
class ExampleBadListWidth extends StatelessWidget {
List<String> things = [
"1: This is a really really really really really really really really really really long thing",
"2: This is a really really really really really really really really really really long thing",
"3: This is a really really really really really really really really really really long thing"
];
ExampleBadListWidth();
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(children: [
Expanded(
child: ListView.builder(
itemCount: things.length,
itemBuilder: _thingBuilder,
))
]));
}
Widget _thingBuilder(context, index) {
return Align(
alignment: Alignment.centerLeft,
child: SizedBox(
width: 100,
child: Text(things[index])),
);
}
}
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: ExampleBadListWidth(),
);
}
}
导入“包装:颤振/材料.省道”;
类ExampleBadListWidth扩展了无状态小部件{
列出事情=[
“1:这真是一件很长的事情”,
“2:这真是一件很长的事情”,
“3:这真是一件很长的事”
];
示例BadListWidth();
@凌驾
小部件构建(构建上下文){
返回脚手架(
正文:列(子项:[
扩大(
子项:ListView.builder(
itemCount:things.length,
itemBuilder:_thingBuilder,
))
]));
}
小部件_thingBuilder(上下文、索引){
返回对齐(
对齐:alignment.centerLeft,
孩子:大小盒子(
宽度:100,
子:文本(事物[索引]),
);
}
}
void main(){
runApp(MyApp());
}
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“颤振演示”,
主题:主题数据(
主样本:颜色。蓝色,
视觉密度:视觉密度。自适应平台密度,
),
主页:示例BadListWidth(),
);
}
}
import 'package:flutter/material.dart';
class ExampleBadListWidth extends StatelessWidget {
List<String> things = [
"1: This is a really really really really really really really really really really long thing",
"2: This is a really really really really really really really really really really long thing",
"3: This is a really really really really really really really really really really long thing"
];
ExampleBadListWidth();
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(children: [
Expanded(
child: ListView.builder(
itemCount: things.length,
itemBuilder: _thingBuilder,
))
]));
}
Widget _thingBuilder(context, index) {
return Align(
alignment: Alignment.centerLeft,
child: SizedBox(
width: 100,
child: Text(things[index])),
);
}
}
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: ExampleBadListWidth(),
);
}
}