我正在尝试使listBuilder正常工作。我正在使用FutureBuilder,因为我正在填充数据“ BoxConstraints强制无限宽度”。
我尝试添加Containers,Expanded和sizebox。我在展示位置上做错了什么,或者我在整个错误的方向上做错了。请帮忙。
class SubredditList extends StatelessWidget {
@override
Widget build(BuildContext context) {
return FutureBuilder(
future: Provider.of<RedditAPIService>(context, listen: false)
.getSubreddits(),
builder: (context, snapshot) {
if (snapshot.hasData) {
List data = snapshot.data;
return ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: data.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(data[index]['name']),
// subtitle: Image(image: NetworkImage(data[index]['url'])),
);
// Text(data[1]['image'])
});
} else if (snapshot.hasError) {
return Card(child: Text("${snapshot.error}"));
}
return CircularProgressIndicator();
});
}
}
父部件:
class ViewSubredditAdmin extends StatelessWidget {
static const String id = 'viewSubredditAdmin';
List newsub = List();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: GlobalAppBar(
appbarTitle: 'Subreddit - Admin',
),
body: SafeArea(
child: Column(
children: <Widget>[
SubredditList(),
Container(
child: ButtonBar(
alignment: MainAxisAlignment.center,
children: <Widget>[
RaisedButton(
child: Text("Get New Submissions"),
onPressed: () {
print(
Provider.of<RedditAPIService>(context, listen: false)
.getNewSubmissions(subredditName: 'Python')
.runtimeType);
},
color: Colors.red,
textColor: Colors.yellow,
padding: EdgeInsets.fromLTRB(10, 10, 10, 10),
splashColor: Colors.grey,
),
],
),
),
Container(
child: Center(
child: ViewSubmissionsAdmin(),
),
),
],
),
),
);
}
}
答案 0 :(得分:0)
您可以在下面复制粘贴运行完整代码
我使用以下演示代码来模拟这种情况
您可以将ListTile
用Container
包装并提供width
代码段
return Container(
width: 100,
child: ListTile(title: Text(numbers[index].toString())));
工作演示
完整代码
import 'package:flutter/material.dart';
void main() => runApp(MaterialApp(
home: MainScreen(),
));
class MainScreen extends StatelessWidget {
final List<int> numbers = [1, 2, 3, 5, 8, 13, 21, 34, 55];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Horizontal ListView'),
),
body: Container(
padding: EdgeInsets.symmetric(horizontal: 16.0, vertical: 24.0),
height: MediaQuery.of(context).size.height * 0.35,
child: ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: numbers.length,
itemBuilder: (context, index) {
return Container(
width: 100,
child: ListTile(title: Text(numbers[index].toString())));
})));
}
}