首页 > 解决方案 > 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 visibleenter image description here when i use Container with height 300.0 insted to expand

And when i use expand, this is what being shown enter image description here

标签: dartflutter

解决方案


请尝试改变

 @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


推荐阅读