【问题标题】:Flutter align a custom bottom at bottom center of screen?Flutter在屏幕底部中心对齐自定义底部?
【发布时间】:2021-01-11 19:20:12
【问题描述】:

我的自定义按钮未在屏幕底部中心对齐,现在我的自定义按钮位于容器底部的中心。它只是下面代码的一部分。关于如何修复以使我的自定义按钮出现在屏幕底部中心的任何想法?

  class _MainMenuState extends State<MainMenu> {

       @override
  Widget build(BuildContext context) {
    final width = MediaQuery.of(context).size.width;
    return Scaffold(
      appBar: AppBar(
        title: Header(text: 'Home', style: Fonts.header1),
        centerTitle: true,
      ),
     backgroundColor: ColorStyle.black,
      body: Container(
          alignment: Alignment.topCenter,
          child: SingleChildScrollView(
            child: Column(
              children: <Widget>[
                Util.paddingTop,
                TextFieldNormal(),
              Expanded(
              child: Align(
                alignment: Alignment.bottomCenter,
                child: ButtonNormal(
                  text: 'Go to next screen',
                  width: width * 0.8,
                  height: 50,
                  color: ColorStyle.blueDark,
                  onPressed: () => pushNewScreen(
                    context,
                    screen: BottomNavBarWidget(
                      menuScreenContext: context,
                    ),
                  ),
                ),
              ),
            ),
          ],
        ),
      )),
);

}

}

【问题讨论】:

    标签: flutter button alignment


    【解决方案1】:

    只需将BottonNormal 包裹在Expanded 小部件中即可。

     Expanded(
               child: Align(
                 alignment: Alignment.bottomCenter,
                  child: ButtonNormal(
                   // ... 
                  ),
                ),
             ),
    

    为什么它不起作用:

    这是因为列占据了整个屏幕,但小部件垂直渲染,只占用自己的空间。

    解决方案:

    使用Expanded 小部件包裹底部小部件,这将占用Column 底部的剩余空间,因此后缀底部对齐可以工作

    【讨论】:

    • 我已经更新了代码,但是由于我收到错误消息,这不起作用:“RenderBox 未布置:_RenderSingleChildViewport#565f2 relayoutBoundary=up8 NEEDS-PAINT 'package:flutter/src/rendering/box .dart':断言失败:第 1702 行 pos 12:'hasSize' 相关的导致错误的小部件是 SingleChildScrollView"。
    • 如果我删除 SingleChildScrollView 就可以了。但我希望能够在较小的设备上使用 Scrollview。我该如何解决?
    • 你可以将singleChildScrollView作为父级
    • @DavidRuland:如果可行,你能接受答案吗?
    猜你喜欢
    • 1970-01-01
    • 2017-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-24
    相关资源
    最近更新 更多