【问题标题】:Overflow warning in AnimatedContainer adjusting heightAnimatedContainer 调整高度时溢出警告
【发布时间】:2019-10-11 09:24:25
【问题描述】:

Example

使用 CupertinoPicker,我希望它使用 AnimatedContainer 以动画方式显示在文本输入下方的视图中,但是当它变得可见时,我会收到溢出警告。从我所见,您无法调整 CupertinoPicker 的大小,只有它是父级。

有没有更好的解决方案?

Column(
    children: <Widget>[
        buildTextField(
            field: 'limit',
            label: 'How Many Guests?',
            controller: TextEditingController(
                text: eventModel.event['limit']),
            onTap: () {
                showPicker.value = !showPicker.value;
            },
        ),
            AnimatedContainer(
            height: showPicker.value ? 150 : 0,
            duration: Duration(milliseconds: 150),
            child: showPicker.value
                ? CupertinoPicker(
                    backgroundColor: Colors.transparent,
                    itemExtent: 40,
                    children: List<Widget>.generate(
                        98,
                        (index) => Center(
                            child: Text(
                                '${index + 2}',
                                style: TextStyle(
                                    color: Colors.black,
                                    fontSize: 16),
                            ),
                            ),
                    ),
                    onSelectedItemChanged: (item) {
                        print((item + 2).toString());
                    },
                    )
                : null,
        ),
    ]
)

例外:

flutter: ══╡ EXCEPTION CAUGHT BY RENDERING LIBRARY ╞═════════════════════════════════════════════════════════
flutter: The following message was thrown during layout:
flutter: A RenderFlex overflowed by 22 pixels on the bottom.
flutter:
flutter: The overflowing RenderFlex has an orientation of Axis.vertical.
flutter: The edge of the RenderFlex that is overflowing has been marked in the rendering with a yellow and
flutter: black striped pattern. This is usually caused by the contents being too big for the RenderFlex.
flutter: Consider applying a flex factor (e.g. using an Expanded widget) to force the children of the
flutter: RenderFlex to fit within the available space instead of being sized to their natural size.
flutter: This is considered an error condition because it indicates that there is content that cannot be
flutter: seen. If the content is legitimately bigger than the available space, consider clipping it with a
flutter: ClipRect widget before putting it in the flex, or using a scrollable container rather than a Flex,
flutter: like a ListView.
flutter: The specific RenderFlex in question is:
flutter:   RenderFlex#73b9d relayoutBoundary=up16 OVERFLOWING
flutter:   creator: Column ← Stack ← CupertinoPicker ← ConstrainedBox ← Container ← AnimatedContainer ←
flutter:   Column ← Observer ← _FormScope ← WillPopScope ← Form ← Padding ← ⋯
flutter:   parentData: not positioned; offset=Offset(0.0, 0.0) (can use size)
flutter:   constraints: BoxConstraints(0.0<=w<=346.9, 0.0<=h<=18.2)
flutter:   size: Size(346.9, 18.2)
flutter:   direction: vertical
flutter:   mainAxisAlignment: start
flutter:   mainAxisSize: max
flutter:   crossAxisAlignment: center
flutter:   verticalDirection: down
flutter: ◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤
flutter: Another exception was thrown: A RenderFlex overflowed by 22 pixels on the bottom.

【问题讨论】:

    标签: flutter flutter-layout flutter-animation


    【解决方案1】:

    AnimatedContainerCupertinoPicker 存在问题,因为它为子级 itemExtent: 40 使用了固定高度。

    尝试使用SizeTransition 来达到同样的效果。这是一个示例:

        class _MySampleWidgetState extends State<MySampleWidget>
            with SingleTickerProviderStateMixin {
          bool showPicker = false;
          AnimationController _controller;
    
          @override
          void initState() {
            _controller = AnimationController(
              vsync: this,
              duration: Duration(milliseconds: 200),
            );
            super.initState();
          }
    
          @override
          void dispose() {
            _controller.dispose();
            super.dispose();
          }
    
          @override
          Widget build(BuildContext context) {
            return Center(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  RaisedButton(
                    child: Text("text"),
                    onPressed: () {
                      showPicker = !showPicker;
                      if (showPicker) {
                        _controller.forward();
                      } else {
                        _controller.reverse();
                      }
                    },
                  ),
                  SizeTransition(
                    sizeFactor: _controller,
                    child: Container(
                      height: 150,
                      child: CupertinoPicker(
                        backgroundColor: Colors.transparent,
                        itemExtent: 40,
                        children: List<Widget>.generate(
                            98,
                            (index) => Center(
                                  child: Text(
                                    '${index + 2}',
                                    style: TextStyle(color: Colors.black, fontSize: 16),
                                  ),
                                )),
                        onSelectedItemChanged: (item) {
                          print((item + 2).toString());
                        },
                      ),
                    ),
                  ),
                ],
              ),
            );
          }
        }
    

    【讨论】:

    • 哇,它做到了!谢谢!我喜欢遇到这些问题,因为有很多小部件可以用来做同样的事情。但并非所有方式都是最优的。谢谢!
    【解决方案2】:

    您可以使用 SingleChildScrollView 包装您的其他小部件,如下所示:

    AnimatedContainer( 
    duration:Duration(milliseconds:500),
        child: SingleChildScrollView(child:contents),
    ),
    

    【讨论】:

      猜你喜欢
      • 2020-12-16
      • 2014-04-14
      • 2016-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-07
      • 2016-10-06
      • 1970-01-01
      相关资源
      最近更新 更多