我在栏中有图片和文字。 我希望文本容器扩展到列宽,以文本为中心创建黑色边框。
这就是我现在得到的。
代码。
List<Container> getMediaItem(List<Media> mediaItems) {
List<Container> mediaContainers = [];
for (Media media in mediaItems) {
mediaContainers.add(
Container(
padding: EdgeInsets.only(left: 8, right: 8, bottom: 8),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
media.image,
Container(
color: Colors.black,
padding: EdgeInsets.only(top: 8, bottom: 8),
child: Text(media.title),
)
],
),
),
);
}
return mediaContainers;
}
如果您知道如何解决,请分享。
更新: 完整的代码(我试图制作一个嵌套的滚动视图以显示诸如Netflix之类的媒体项目的库):
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Live Tree',
theme: ThemeData(
brightness: Brightness.dark,
),
home: Scaffold(
appBar: AppBar(
title: Row(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.max,
children: <Widget>[
Text("LiveTree"),
Icon(Icons.check_circle_outline),
],
),
),
body: HomePage(),
),
);
}
}
class HomePage extends StatefulWidget {
HomePage({Key key}) : super(key: key);
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
@override
Widget build(BuildContext context) {
List<Column> getMediaItem(List<Media> mediaItems) {
List<Column> mediaContainers = [];
for (Media media in mediaItems) {
mediaContainers.add(
Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
media.image,
Container(
color: Colors.black,
padding: EdgeInsets.only(top: 8, bottom: 8),
child: Text(media.title),
)
],
),
);
}
return mediaContainers;
}
List<Widget> getCategoryRows(List<CategoryModel> categoryModels) {
List<Widget> categoryRows = [];
for (CategoryModel category in categoryModels) {
final mediaItemsForCategory = getMediaItem(category.media);
categoryRows.add(
ListView(scrollDirection: Axis.horizontal,
children: mediaItemsForCategory,
),
);
}
return categoryRows;
}
Widget gallerySection = Column(
mainAxisSize: MainAxisSize.min,
children: getCategoryRows(mockCategoryDataSet),
);
return Scaffold(
body: ListView(
children: <Widget>[
gallerySection,
],
),
);
}
答案 0 :(得分:4)
Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
media.image,
Container(
color: Colors.black,
padding: EdgeInsets.only(top: 8, bottom: 8),
child: Text(media.title),
),
],
);
或
Column(
children: [
media.image,
Container(
color: Colors.black,
padding: EdgeInsets.only(top: 8, bottom: 8),
child: Center(
child: Text(media.title),
),
),
],
);