dart - Why expanded view is not being shown
问题描述
I am trying out flutter, here I am having a simple card view, where there is an add AddButton , when the button is pressed new card is being added.
Now, i wanted to have it scrollable so added ListView and an expanded Widget in the Column. Here is the code...
import 'package:flutter/material.dart';
import './products.dart';
import './product_control.dart';
class ProductManger extends StatefulWidget {
final String startingProduct;
ProductManger({this.startingProduct = "Sweet Tester"});
@override
State<StatefulWidget> createState() {
return _ProductManagerState();
}
}
class _ProductManagerState extends State<ProductManger> {
List<String> _products = ['Food Tester'];
@override
void initState() {
_products.add(widget.startingProduct);
super.initState();
}
void _addProducts(String product) {
setState(() {
_products.add(product);
});
}
@override
Widget build(BuildContext context) {
return Column(children: [
Container(margin: EdgeInsets.all(10.0), child: ProductControl(_addProducts)),
Expanded(child: Products(_products))
]);
}
}
If I change that Expanded to Container with height, it works as expected, but now, nothing is being displayed except a button.
I am currently following a tutorial, exact same code is written, however, version of flutter is 0.3.2 and i am using 1.5
Or there could be some issue with the emulator?
Hope this helps.
Here is the listView code
import 'package:flutter/material.dart';
class Products extends StatelessWidget {
final List<String> products;
Products([this.products = const []]);
@override
Widget build(BuildContext context) {
return ListView(
children: products
.map((element) => Card(
child: Column(
children: <Widget>[
Image.asset('assets/food.jpg'),
Text(element)
],
),
))
.toList());
}
}
This is what is visible when i use Container with height 300.0 insted to expand
解决方案
请尝试改变
@override
Widget build(BuildContext context) {
return Column(children: [
Container(margin: EdgeInsets.all(10.0), child: ProductControl(_addProducts)),
Expanded(child: Products(_products))
]);
}
至
@override
Widget build(BuildContext context) {
return SingleChildScrollView(
child: Column: children: [
Container(
margin: EdgeInsets.all(10.0),
child: ProductControl(_addProducts)),
Expanded(child: Products(_products))
]);
}
如果它不起作用,请查看SingleChildeScrollView
推荐阅读
- python - 加快大型数组的 numpy 索引
- react-native - 我如何解决这个问题,在 expo framwork 中用 expo 架构做出反应
- android - 致命信号 11 (SIGSEGV) 改造
- php - php preg_match 排除 html 标签/属性中的文本以找到正确的位置来剪切字符串
- python - Python - 跨越多个脚本的 tqdm 嵌套循环
- junit - 在 Junit 测试用例中 @Autowired 和 @Qualifier 显示无法加载应用程序上下文和非法状态异常
- excel - 根据特定标准对单元格进行分组
- logging - Dask:SSHCluster 工作人员在哪里登录?(KilledWorker 例外)
- c - C如何使用指针管理堆栈?
- c++ - 简化从文件读取的功能