SingleChildScrollView中的全高容器

时间:2019-01-27 14:22:41

标签: flutter

我有一个SingleChildScrollView,里面有一个Container。我要实现的是Container's的高度应为整个视口的高度。

我的代码:

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'MyApp',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primarySwatch: Colors.blue,
        fontFamily: 'OpenSans',
      ),
      home: SingleChildScrollView(
        child: Container(
          color: Colors.white,
          height: double.infinity,
        ),
      ),
    );
  }
}

如果我运行我的代码,则会出现异常:

I/flutter ( 6293): ══╡ EXCEPTION CAUGHT BY RENDERING LIBRARY ╞═════════════════════════════════════════════════════════
I/flutter ( 6293): The following assertion was thrown during performLayout():
I/flutter ( 6293): BoxConstraints forces an infinite height.
I/flutter ( 6293): These invalid constraints were provided to RenderDecoratedBox's layout() function by the following
I/flutter ( 6293): function, which probably computed the invalid constraints in question:
I/flutter ( 6293):   RenderConstrainedBox.performLayout (package:flutter/src/rendering/proxy_box.dart:258:13)
I/flutter ( 6293): The offending constraints were:
I/flutter ( 6293):   BoxConstraints(w=411.4, h=Infinity)

3 个答案:

答案 0 :(得分:6)

我遇到了同样的问题,但是答案对我不起作用,所以我没有将SingleChildScrollView()包装在容器中并给它高度:double.infinity

Container(
    height: double.infinity,
    color: Colors.green,
    child: SingleChildScrollView()
 )

答案 1 :(得分:1)

也许

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final mq = MediaQueryData.fromWindow(window);
    return MaterialApp(
      title: 'MyApp',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primarySwatch: Colors.blue,
        fontFamily: 'OpenSans',
      ),
      home: SingleChildScrollView(
        child: ConstrainedBox(
          constraints: BoxConstraints.tightFor(
            height: mq.size.height,
          ),
          child: Container(
            height: double.infinity,
            decoration: BoxDecoration(
                color: Colors.red,
                border: Border.all(color: Colors.blue, width: 8)),
          ),
        ),
      ),
    );
  }
}

答案 2 :(得分:0)

您可以尝试SliverFillViewport或PageView