【问题标题】:carousel slider "A RenderFlex overflowed by Infinity pixels on the bottom." flutter轮播滑块“底部被无限像素溢出的 RenderFlex。”扑
【发布时间】:2021-09-12 08:16:30
【问题描述】:

我正在为此构建一个主页,我在单独的页面上创建了一个隐藏的抽屉,在单独的页面上创建了一个主屏幕,并在主页上堆叠了“隐藏的抽屉”和“主屏幕”,还在主页上创建了可折叠的搜索按钮屏幕与菜单按钮在同一行,margin:spaceBetween .
我在主页第二行的单独页面上创建了轮播滑块,即在搜索和菜单按钮下方,并在主页上调用该轮播滑块但显示错误

“一个 RenderFlex 在底部被无限像素溢出”

当我尝试将它添加到顶部时,为什么它在底部。我已经尝试了所有可能的填充和间距,但没有任何效果,请帮助

DevTools 布局

主屏幕代码

class HomeScreen extends StatefulWidget {
  HomeScreen({Key? key}) : super(key: key);

  @override
  _HomeScreenState createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  bool _folded = true;

  @override
  Widget build(BuildContext context) {
    return AnimatedContainer(
      duration: Duration(milliseconds: 250),
      color: Colors.blueGrey.shade100,
      child: Column(
        children: [
          SizedBox(
            height: 40,
          ),
          Row(
            // mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              IconButton(
                icon: SvgPicture.asset(
                  "assets/icons/menu.svg",
                ),
                onPressed: () {},
              ),
              Padding(
                padding: const EdgeInsets.only(left: 110),
                child: Container(
                  width: _folded ? 52 : 250,
                  height: getProportionateScreenHeight(50),
                  decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(32),
                      color: Colors.white,
                      boxShadow: [
                        BoxShadow(
                          color: Colors.brown.shade300.withOpacity(0.3),
                          spreadRadius: 0,
                          blurRadius: 8,
                          offset: Offset(-4, 0),
                        ),
                        BoxShadow(
                          color: Colors.grey.withOpacity(0.3),
                          spreadRadius: 0,
                          blurRadius: 8,
                          offset: Offset(4, 0),
                        ),
                        BoxShadow(
                          color: Colors.brown.shade300.withOpacity(0.3),
                          spreadRadius: 0,
                          blurRadius: 8,
                          offset: Offset(-4, 0),
                        ),
                        BoxShadow(
                          color: Colors.grey.withOpacity(0.3),
                          spreadRadius: 0,
                          blurRadius: 8,
                          offset: Offset(4, 0),
                        ),
                      ]),
                  child: Row(
                    children: [
                      Expanded(
                        child: Container(
                          padding: EdgeInsets.only(left: 16),
                          child: !_folded
                              ? TextField(
                                  decoration: InputDecoration(
                                    hintText: 'Search Book, Author,Genre ',
                                    hintStyle: TextStyle(
                                      color: Colors.black54,
                                      fontFamily:
                                          GoogleFonts.oregano().fontFamily,
                                    ),
                                    border: InputBorder.none,
                                  ),
                                )
                              : null,
                        ),
                      ),
                      AnimatedContainer(
                        duration: Duration(milliseconds: 400),
                        child: Material(
                          type: MaterialType.transparency,
                          child: InkWell(
                              borderRadius: BorderRadius.only(
                                topLeft: Radius.circular(_folded ? 32 : 0),
                                topRight: Radius.circular(32),
                                bottomLeft: Radius.circular(_folded ? 32 : 0),
                                bottomRight: Radius.circular(32),
                              ),
                              child: Padding(
                                padding: const EdgeInsets.all(16.0),
                                child: SvgPicture.asset(
                                    _folded
                                        ? "assets/icons/search.svg"
                                        : "assets/icons/close1.svg",
                                    height: getProportionateScreenHeight(18),
                                    color: Colors.black54),
                              ),
                              onTap: () {
                                setState(() {
                                  _folded = !_folded;
                                });
                              }),
                        ),
                      )
                    ],
                  ),
                ),
              ),
            ],
          ),
          Row(
            children: [ImageSlider()],
          )
        ],
      ),
    );
  }
}

轮播滑块的代码

  Widget build(BuildContext context) {
    return Scaffold(
      body: ListView(
        children: [
          CarouselSlider(
            items: [
              //1st ImageSlider
              Container(
                padding: EdgeInsets.only(bottom: 20),
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(8.0),
                  image: DecorationImage(
                    image: AssetImage("assets/images/13.jpg"),
                    fit: BoxFit.cover,
                  ),
                ),
              ),
              //2nd Image of Slider
              Container(
                padding: EdgeInsets.only(bottom: 20),
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(8.0),
                  image: DecorationImage(
                    image: AssetImage("assets/images/15.jpg"),
                    fit: BoxFit.cover,
                  ),
                ),
              ),

              //3rd Image of Slider
              Container(
                margin: EdgeInsets.all(6.0),
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(8.0),
                  image: DecorationImage(
                    image: AssetImage("assets/images/3.jpg"),
                    fit: BoxFit.cover,
                  ),
                ),
              ),
            ],
            //Slider Container properties
            options: CarouselOptions(
              // height: getProportionateScreenHeight(50),
              height: 50,
              autoPlay: true,
              reverse: true,
              enlargeCenterPage: true,
              autoPlayInterval: Duration(seconds: 2),
            ),
          ),
        ],
      ),
    );
  }

【问题讨论】:

  • 从 CarouselSlider 父级移除 ListView
  • 用 Container 包裹 CarouselSlider 并设置固定高度。
  • 我应该返回一个容器而不是脚手架并删除列表视图将起作用以及如何设置固定高度
  • 还是不行
  • 您的carousel slider 代码sn-p 是指ImageSlider() 吗?

标签: android flutter dart flutter-layout


【解决方案1】:

对于使用Expanded(child: ImageSlider()), 的单子包装,它将占用额外的空格。

使用行时

       Expanded(
              child: Row(
                children: [
                  // will take full spaces but width- IconSize
                  Flexible(
                    child: ImageSlider(),
                  ),
                  Icon(Icons.ac_unit),
                ],
              ),
            ),

或者只是用SizedBox包装

    SizedBox(
              height: 200,
              child: Row(
                /// controll position
                children: [
                  SizedBox(
                    width: 100,
                    child: ImageSlider(),
                  ),
                  Icon(Icons.ac_unit),
                ],
              ),
            ),

测试小部件



class HomeScreen extends StatefulWidget {
  HomeScreen({Key? key}) : super(key: key);

  @override
  _HomeScreenState createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  bool _folded = true;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: AnimatedContainer(
        duration: Duration(milliseconds: 250),
        color: Colors.blueGrey.shade100,
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            SizedBox(
              height: 40,
            ),
            Row(
              // mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                IconButton(
                  icon: SvgPicture.asset(
                    "assets/icons/menu.svg",
                  ),
                  onPressed: () {},
                ),
                Padding(
                  padding: const EdgeInsets.only(left: 110),
                  child: Container(
                    width: _folded ? 52 : 250,
                    // height: getProportionateScreenHeight(50),
                    height: 50,
                    decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(32),
                        color: Colors.white,
                        boxShadow: [
                          BoxShadow(
                            color: Colors.brown.shade300.withOpacity(0.3),
                            spreadRadius: 0,
                            blurRadius: 8,
                            offset: Offset(-4, 0),
                          ),
                          BoxShadow(
                            color: Colors.grey.withOpacity(0.3),
                            spreadRadius: 0,
                            blurRadius: 8,
                            offset: Offset(4, 0),
                          ),
                          BoxShadow(
                            color: Colors.brown.shade300.withOpacity(0.3),
                            spreadRadius: 0,
                            blurRadius: 8,
                            offset: Offset(-4, 0),
                          ),
                          BoxShadow(
                            color: Colors.grey.withOpacity(0.3),
                            spreadRadius: 0,
                            blurRadius: 8,
                            offset: Offset(4, 0),
                          ),
                        ]),
                    child: Row(
                      children: [
                        Expanded(
                          child: Container(
                            padding: EdgeInsets.only(left: 16),
                            child: !_folded
                                ? TextField(
                                    decoration: InputDecoration(
                                      hintText: 'Search Book, Author,Genre ',
                                      hintStyle: TextStyle(
                                        color: Colors.black54,
                                        fontFamily:
                                            GoogleFonts.oregano().fontFamily,
                                      ),
                                      border: InputBorder.none,
                                    ),
                                  )
                                : null,
                          ),
                        ),
                        AnimatedContainer(
                          duration: Duration(milliseconds: 400),
                          child: Material(
                            type: MaterialType.transparency,
                            child: InkWell(
                                borderRadius: BorderRadius.only(
                                  topLeft: Radius.circular(_folded ? 32 : 0),
                                  topRight: Radius.circular(32),
                                  bottomLeft: Radius.circular(_folded ? 32 : 0),
                                  bottomRight: Radius.circular(32),
                                ),
                                child: Padding(
                                  padding: const EdgeInsets.all(16.0),
                                  child: SvgPicture.asset(
                                      _folded
                                          ? "assets/icons/search.svg"
                                          : "assets/icons/close1.svg",
                                      // height: getProportionateScreenHeight(18),
                                      height: 18,
                                      color: Colors.black54),
                                ),
                                onTap: () {
                                  setState(() {
                                    _folded = !_folded;
                                  });
                                }),
                          ),
                        )
                      ],
                    ),
                  ),
                ),
              ],
            ),

            //For Single, will take full spaces
            // Expanded(child: ImageSlider()),

            /// with  rows, could be use [Flexible]
            Expanded(
              child: Row(
                children: [
                  // will take full spaces but width- IconSize
                  Flexible(
                    child: ImageSlider(),
                  ),
                  Icon(Icons.ac_unit),
                ],
              ),
            ),

            /// or just wrap with SizedBOx
            SizedBox(
              height: 200,
              child: Row(
                /// controll position
                children: [
                  SizedBox(
                    width: 100,
                    child: ImageSlider(),
                  ),
                  Icon(Icons.ac_unit),
                ],
              ),
            ),

            Container(
              height: 200,
              child: Text("Extra Bottom Part "),
              color: Colors.blueGrey,
              width: double.infinity,
            ),
          ],
        ),
      ),
    );
  }
}

class ImageSlider extends StatefulWidget {
  ImageSlider({Key? key}) : super(key: key);

  @override
  _ImageSliderState createState() => _ImageSliderState();
}

class _ImageSliderState extends State<ImageSlider> {
  Widget build(BuildContext context) {
    return Scaffold(
      body: ListView(children: [
        CarouselSlider(
          items: [
            //1st ImageSlider
            Container(
              padding: EdgeInsets.only(bottom: 20),
              height: 100,
              decoration: BoxDecoration(
                color: Colors.deepPurple,
                borderRadius: BorderRadius.circular(8.0),
                // image: DecorationImage(
                //   image: AssetImage("assets/images/13.jpg"),
                //   fit: BoxFit.cover,
                // ),
              ),
            ),
            //2nd Image of Slider
            Container(
              padding: EdgeInsets.only(bottom: 20),
              height: 100,
              decoration: BoxDecoration(
                color: Colors.deepOrange,
                borderRadius: BorderRadius.circular(8.0),
                // image: DecorationImage(
                //   image: AssetImage("assets/images/15.jpg"),
                //   fit: BoxFit.cover,
                // ),
              ),
            ),

            //3rd Image of Slider
            Container(
              margin: EdgeInsets.all(6.0),
              decoration: BoxDecoration(
                color: Colors.pinkAccent,
                borderRadius: BorderRadius.circular(8.0),
                // image: DecorationImage(
                //   image: AssetImage("assets/images/3.jpg"),
                //   fit: BoxFit.cover,
                // ),
              ),
            ),
          ],
          //Slider Container properties
          options: CarouselOptions(
            // height: getProportionateScreenHeight(50),
            height: 50,
            autoPlay: true,
            reverse: true,
            enlargeCenterPage: true,
            autoPlayInterval: Duration(seconds: 2),
          ),
        ),
      ]),
    );
  }
}

【讨论】:

  • 为什么扩展方法需要宽度图标大小以及为什么你在那里使用 icon.ac 和相同大小的盒子你能解释一下它的用途是什么。和扩展方法的工作原理与您所说的图标大小完全相同,但大小框方法被限制在具有该图标的非常小的空间
  • 您的扩展方法效果很好,但是当我什至在动画容器中添加了 imageslider 时,为什么它仍然没有将其颜色作为 bg 并且上面还有很多空白区域无法删除
  • 我也添加了模拟器图片,请查看并回复
  • 对于展开,它将占用可用空间,灵活控制孩子应该如何弯曲。您尝试使用Column&lt;Flex&lt;v:2&gt;,&lt;Flex&lt;v:4&gt;&gt;&gt; 或仅使用灵活包装并在行的孩子上使用flex:x。您将了解它是如何工作的。用 Expanded 包裹 Row 我们告诉它尽可能多地占用空间,然后为内部孩子占用同样多的空间。由于我们的Expnaded 占用了其余的空间(为图标提供空间),我们看不到我们的 BGcolor。 .但是您可以选择SizedBox 作为高度,并为宽度提供 flex 或 row 的子项......(我认为)
  • 对于背景颜色,您需要在_ImageSliderState脚手架上提供backgroundColor: Colors.transparent,。因为脚手架的默认颜色是灰色[50](如果我没记错的话)。
【解决方案2】:

使用 SingleChildScrollView 包装列

  @override
  Widget build(BuildContext context) {
    return AnimatedContainer(
      height: MediaQuery.of(context).size.height,
      duration: Duration(milliseconds: 250),
      color: Colors.blueGrey.shade100,
      child: SingleChildScrollView ( child:  Column(
        children: [
          SizedBox(
            height: 40,
          ),
          Row(
            // mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              IconButton(
                icon: SvgPicture.asset(
                  "assets/icons/menu.svg",
                ),
                onPressed: () {},
              ),
              Padding(
                padding: const EdgeInsets.only(left: 110),
                child: Container(
                  width: _folded ? 52 : 250,
                  height: getProportionateScreenHeight(50),
                  decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(32),
                      color: Colors.white,
                      boxShadow: [
                        BoxShadow(
                          color: Colors.brown.shade300.withOpacity(0.3),
                          spreadRadius: 0,
                          blurRadius: 8,
                          offset: Offset(-4, 0),
                        ),
                        BoxShadow(
                          color: Colors.grey.withOpacity(0.3),
                          spreadRadius: 0,
                          blurRadius: 8,
                          offset: Offset(4, 0),
                        ),
                        BoxShadow(
                          color: Colors.brown.shade300.withOpacity(0.3),
                          spreadRadius: 0,
                          blurRadius: 8,
                          offset: Offset(-4, 0),
                        ),
                        BoxShadow(
                          color: Colors.grey.withOpacity(0.3),
                          spreadRadius: 0,
                          blurRadius: 8,
                          offset: Offset(4, 0),
                        ),
                      ]),
                  child: Row(
                    children: [
                      Expanded(
                        child: Container(
                          padding: EdgeInsets.only(left: 16),
                          child: !_folded
                              ? TextField(
                                  decoration: InputDecoration(
                                    hintText: 'Search Book, Author,Genre ',
                                    hintStyle: TextStyle(
                                      color: Colors.black54,
                                      fontFamily:
                                          GoogleFonts.oregano().fontFamily,
                                    ),
                                    border: InputBorder.none,
                                  ),
                                )
                              : null,
                        ),
                      ),
                      AnimatedContainer(
                        duration: Duration(milliseconds: 400),
                        child: Material(
                          type: MaterialType.transparency,
                          child: InkWell(
                              borderRadius: BorderRadius.only(
                                topLeft: Radius.circular(_folded ? 32 : 0),
                                topRight: Radius.circular(32),
                                bottomLeft: Radius.circular(_folded ? 32 : 0),
                                bottomRight: Radius.circular(32),
                              ),
                              child: Padding(
                                padding: const EdgeInsets.all(16.0),
                                child: SvgPicture.asset(
                                    _folded
                                        ? "assets/icons/search.svg"
                                        : "assets/icons/close1.svg",
                                    height: getProportionateScreenHeight(18),
                                    color: Colors.black54),
                              ),
                              onTap: () {
                                setState(() {
                                  _folded = !_folded;
                                });
                              }),
                        ),
                      )
                    ],
                  ),
                ),
              ),
            ],
          ),
          Row(
            children: [ImageSlider()],
          )
        ],
      ),),
    );
  } ````

【讨论】:

  • AnimatedContainer 的初始高度是多少
  • 需要全屏
猜你喜欢
  • 1970-01-01
  • 2021-03-13
  • 1970-01-01
  • 2020-10-08
  • 1970-01-01
  • 2020-09-30
  • 2020-12-29
  • 2020-11-22
  • 1970-01-01
相关资源
最近更新 更多