我無法在行后放置水平串列視圖。我想要這樣的東西..

這是我的代碼(我在單獨的檔案中有那個搜索欄小部件,它作業正常,我還沒有類別 itmes svgs,所以只使用文本作為孩子)這是我的代碼:
class CategoriesListView extends StatelessWidget {
CategoriesListView({Key? key}) : super(key: key);
final List<String> _categories = ['Fruits', 'Vegetables', 'Fishes', 'Dairy'];
Widget categoryWidget(BuildContext context, String title) {
return Expanded(
child: Container(
height: 50,
width: 50,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Theme.of(context).primaryColor,
border: Border.all(color: Theme.of(context).primaryColor)),
child: Text(title)),
);
}
@override
Widget build(BuildContext context) {
return Container(
color: Colors.black,
margin: margin,
height: 200,
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Expanded(
child: Text(
'Category',
style: TextStyle(
color: Colors.black,
fontSize: 15.0,
fontWeight: FontWeight.w600),
),
),
TextButton(
onPressed: null,
child: Text(
'See all',
style: TextStyle(color: Theme.of(context).primaryColor),
)),
],
),
ListView(
physics: NeverScrollableScrollPhysics(),
shrinkWrap: true,
scrollDirection: Axis.horizontal,
children:
_categories.map((e) => categoryWidget(context, e)).toList())
]),
);
}
}
但最終得到:
════════ Exception caught by rendering library ═════════════════════════════════
RenderBox was not laid out: _RenderColoredBox#f3a92
'package:flutter/src/rendering/box.dart':
package:flutter/…/rendering/box.dart:1
Failed assertion: line 1927 pos 12: 'hasSize'
The relevant error-causing widget was
Container
lib\screens\home_screen.dart:18
如果需要任何其他資訊,我隨時準備提供。
uj5u.com熱心網友回復:
這是您的作業代碼
class CategoriesListView extends StatelessWidget {
CategoriesListView({Key? key}) : super(key: key);
final List<String> _categories = ['Fruits', 'Vegetables', 'Fishes', 'Dairy'];
Widget categoryWidget(BuildContext context, String title) {
return Container(
height: 50,
width: 50,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Theme.of(context).primaryColor,
border: Border.all(color: Theme.of(context).primaryColor)),
child: Text(title),);
}
@override
Widget build(BuildContext context) {
return Container(
color: Colors.black,
margin: margin,
height: 200,
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Expanded(
child: Text(
'Category',
style: TextStyle(
color: Colors.black,
fontSize: 15.0,
fontWeight: FontWeight.w600),
),
),
TextButton(
onPressed: null,
child: Text(
'See all',
style: TextStyle(color: Theme.of(context).primaryColor),
)),
],
),
Expanded(
child: ListView(
physics: NeverScrollableScrollPhysics(),
shrinkWrap: true,
scrollDirection: Axis.horizontal,
children:
_categories.map((e) => categoryWidget(context, e)).toList()))
]),
);
}
}
uj5u.com熱心網友回復:
控制臺拋出 hasSize 錯誤,因為您的 ListView 未與 Container 包裝在一起。在 Flutter 中, ListView 沒有自己的高度和寬度。所以總是用 Container 包裹你的 ListView 和它的高度。
做一件事:
從容器中洗掉擴展小部件。
用容器包裝你的 ListView。
Container(
height: 200, // height should be as per your desire
child: ListView(
physics: NeverScrollableScrollPhysics(),
shrinkWrap: true,
scrollDirection: Axis.horizontal,
children: _categories.map((e)=>categoryWidget(context,e)).toList(),
),
),
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/395292.html
上一篇:顫動閃屏沒有顯示
