Flutter-ListView 溢出

Posted

技术标签:

【中文标题】Flutter-ListView 溢出【英文标题】:Flutter-ListView overflow 【发布时间】:2019-01-30 13:11:21 【问题描述】:

Flutter Overflow widget picture

我想设计一个小部件的 ListView,每个小部件都有一个存储信息的容器。我尝试使用 ListView 但容器没有出现在屏幕上,所以我切换到 Row,它们出现但不能超过容器的最大宽度。你能建议如何管理图片中的 ListView 吗? 这是我的代码:

class NewsPage extends StatelessWidget 

@override
Widget build(BuildContext context) 

Widget buildButtonScore= Container(

  height: 100.0,
  width: 100.0,
  decoration: BoxDecoration(borderRadius: BorderRadius.circular(20.0),color: Colors.white),
  child: FlatButton(
    padding: EdgeInsets.only(top: 20.0),
    child: Column( // Replace with a Row for horizontal icon + text
      children: <Widget>[
        IconButton(
            icon: Icon(Icons.add_circle_outline,size: 30.0,color: Colors.blue),
            onPressed: () 
              Navigator.push(
                  context,
                  MaterialPageRoute(
                      builder: (context) => PointPage()
                  )
              );
            
        ),
        Text("Đặt hàng",style: TextStyle(color: Colors.black),)
      ],
    ),
  ),
);
Widget buildButtonOrder= Container(
  height: 100.0,
  width: 100.0,
  decoration: BoxDecoration(borderRadius: BorderRadius.circular(20.0),color: Colors.white),
  child: FlatButton(
    padding: EdgeInsets.only(top: 20.0),
    child: Column( // Replace with a Row for horizontal icon + text
      children: <Widget>[
        IconButton(
            icon: Icon(Icons.shopping_cart,size: 30.0,color: Colors.blue),
            onPressed: () 
              Navigator.push(
                  context,
                  MaterialPageRoute(
                      builder: (context) => PointPage()
                  )
              );
            
        ),
        Text("Đặt hàng",style: TextStyle(color: Colors.black),)
      ],
    ),
  ),
);
Widget buildButtonPurse = Container(
  height: 100.0,
  width: 100.0,
  decoration: BoxDecoration(borderRadius: BorderRadius.circular(20.0),color: Colors.white),
  child: FlatButton(
    padding: EdgeInsets.only(top: 20.0),
    child: Column( // Replace with a Row for horizontal icon + text
      children: <Widget>[
        IconButton(
            icon: Icon(Icons.monetization_on,size: 30.0,color: Colors.blue),
            onPressed: () 
              Navigator.push(
                  context,
                  MaterialPageRoute(
                      builder: (context) => PointPage()
                  )
              );
            
        ),
        Text("Ví tiền",style: TextStyle(color: Colors.black),)
      ],
    ),
  ),
);
//Button Column
Widget buttonRow = Container(
  padding: const EdgeInsets.only(bottom: 10.0),
  margin: EdgeInsets.symmetric(vertical: 10.0),
  child: Row(
    mainAxisAlignment: MainAxisAlignment.spaceEvenly,
    children: [
      buildButtonScore,
      buildButtonOrder,
      buildButtonPurse,
    ],
  ),
);
//OFFERROW
Widget offer1 = Container(
  height: 150.0,
  width: 180.0,
  decoration: BoxDecoration(borderRadius: BorderRadius.circular(20.0),color: Colors.white30),
  child: FlatButton(

    padding: EdgeInsets.only(top: 0.0),

    child: Column( 

      children: <Widget>[

        new Image.asset('images/Q.png',height: 90.0,fit: BoxFit.fitWidth,width: 180.0,),

        Text("Các cô gái đẹp đang chờ bạn đó!!!",style: TextStyle(color: Colors.black),)
      ],
    ),
  ),
);
Widget offer2 = Container(
  height: 150.0,
  width: 180.0,
  decoration: BoxDecoration(borderRadius: BorderRadius.circular(20.0),color: Colors.white30),
  child: FlatButton(

    padding: EdgeInsets.only(top: 0.0),

    child: Column( // Replace with a Row for horizontal icon + text

      children: <Widget>[
        new Image.asset('images/Nina.gif',height: 90.0,fit:BoxFit.fitWidth,width: 180.0,),

        Text("Hãy nhanh tay sở hữu vé xem phim Goddess Kiss!",style: TextStyle(color: Colors.black),)
      ],
    ),
  ),
);
Widget offerRow = Row(
  //shrinkWrap: true,
  //scrollDirection: Axis.horizontal,
  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
  children: <Widget>[
    Row(
      children: [
        offer1,
        VerticalDivider(),
        offer2,
      ],
    ),
  ],
);

Widget newsRow = Container(
  padding: EdgeInsets.only(top: 10.0),
  child: Container(
    color: Colors.transparent,
    margin: EdgeInsets.symmetric(vertical: 20.0),
    height: 125.0,
    child: ListView(
      scrollDirection: Axis.vertical,
      children: <Widget>[
        new NewsBody(),
      ],
    ),
  ),
);
// TODO: implement build
return Container(
  child: ListView(
    scrollDirection: Axis.vertical,
    children: <Widget>[
      buttonRow,

      new Text('Dành cho bạn',style: TextStyle( fontSize: 20.0, fontWeight: FontWeight.w400),),
      offerRow,

      new Text('Tin tức',style: TextStyle( fontSize: 20.0, fontWeight: FontWeight.w400),),
      newsRow,
    ],
  ),
);

我得到错误:

Pic1

Pic2

Pic3

Pic4

Pic5

【问题讨论】:

如果在水平模式下使用 listview 会发生什么?你得到什么错误? 你能放一些代码吗? @DineshBalasubramanian 我添加了代码。 @diegoveloper 我更新了错误。 【参考方案1】:

在 SingleChildScrollView 中添加您的代码,使用 scrollDirection: Axis.horizo​​ntal,或替换为以下代码

Widget offerRow = new SingleChildScrollView(scrollDirection: Axis.horizontal,
child: Row(
  //shrinkWrap: true,
  //scrollDirection: Axis.horizontal,
  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
  children: <Widget>[
    Row(
      children: [
        offer1,
        VerticalDivider(),
        offer2,
      ],
    ),
  ],
),);

【讨论】:

以上是关于Flutter-ListView 溢出的主要内容,如果未能解决你的问题,请参考以下文章

Flutter - ListView.builder:初始滚动位置

Flutter -Listview内的复选框

Flutter - ListView 可以包含静态小部件和流吗

Flutter - 另一个 Listview 中的 Listview.builder

UIButton 未添加到 UIView

关于理解布局单位