【问题标题】:Align widget center with AppBar bottom edge将小部件中心与 AppBar 底部边缘对齐
【发布时间】:2020-08-10 22:08:06
【问题描述】:

我需要将小部件的中心与应用栏底部边缘对齐。 因此它将垂直一半位于应用栏上,一半位于页面正文上。

现在我已将小部件添加到 AppBar 底部:但它不会与其水平中心线对齐。

目前看起来是这样的:

虽然我希望 SelectEnvironment 按钮的 center 以及水平白线将“正好”位于 appBar 的底部边缘

appBar的代码是这样的:

  class CustomAppBar extends AppBar {

     final Widget appBarActionButton;

     CustomAppBar({Widget title = AppUtils.EMPTY_TEXT_VIEW, this.appBarActionButton}): super(
        title: title,
        backgroundColor: Colors.blueGrey,
        elevation: 0,
        bottom: PreferredSize(
            child: Stack( //The stack holds the horizontal line and the button aligned cente
              alignment: Alignment.center,
              children: <Widget>[
                Container( //This is the horizontal line
                  color: Colors.GeneralDividerGray,
                  height: 1.0,
                ),
                Align(
                  child: Container(
                      child: appBarActionButton, //This is the button widget
                      ),
               )
          ],
        ),
        preferredSize: Size.fromHeight(4.0)),
      );
 }

如果有更好的方法可以通过将其移出应用栏来实现这一点,我可以,只要它会产生相同的效果。

【问题讨论】:

  • 你试过centerTitle: true

标签: flutter flutter-layout


【解决方案1】:

我认为你应该像这样使用Stack 和Column

import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';

typedef void OnWidgetSizeChange(Size size);

class MeasureSize extends StatefulWidget {
  final Widget child;
  final OnWidgetSizeChange onChange;

  const MeasureSize({
    Key key,
    @required this.onChange,
    @required this.child,
  }) : super(key: key);

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

class _MeasureSizeState extends State<MeasureSize> {
  @override
  Widget build(BuildContext context) {
    SchedulerBinding.instance.addPostFrameCallback(postFrameCallback);
    return Container(
      key: widgetKey,
      child: widget.child,
    );
  }

  var widgetKey = GlobalKey();
  var oldSize;

  void postFrameCallback(_) {
    var context = widgetKey.currentContext;
    if (context == null) return;

    var newSize = context.size;
    if (oldSize == newSize) return;

    oldSize = newSize;
    widget.onChange(newSize);
  }
}

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  Size s;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Stack(
      children: [
        Column(
          children: [
            MeasureSize(
              onChange: (size) {
                setState(() {
                  s = size;
                });
              },
              child: AppBar(
                title: Text('title'),
              ),
            ),
            SizedBox(
                width: MediaQuery.of(context).size.width,
                height: MediaQuery.of(context).size.height - (s?.height ?? 0.0),
                child: Center(child: Text('body')))
          ],
        ),
        Positioned(
          top: (s?.height ?? 0.0) - 16.0,
          child: Container(
            width: MediaQuery.of(context).size.width,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Container(
                    height: 32,
                    color: Colors.red[400],
                    padding: EdgeInsets.all(6),
                    child: Center(child: Text('Select Environment'))),
              ],
            ),
          ),
        )
      ],
    ));
  }
}

【讨论】:

  • 正如您在我的代码中看到的,我的 AppBar 是自定义的并且有一个底部的 PreferredSized 小部件,这导致 Column 计算它的高度时出现问题。当我将它与标准 AppBar 一起使用时,效果很好!
  • 这很酷,为了避免无限高,为什么不将列放入容器中,然后给容器无限高,然后给该列一个最小轴尺寸....这将使列限制其高度适合孩子的身高。
  • 抱歉,这样做并没有解决问题。任何带有底部小部件( PrefferedSizeWidget )的 CustomAppBar 都不会在列中呈现。
  • 如果你把一个widget放在appbar的底部,appbar的高度会变大,如果你把bottom appbar的颜色设置为透明,那么bottom appbar的颜色就会和appbar的颜色一样
【解决方案2】:

最好的方法是通过如下小部件使用 Slivers:

  ScrollController scrollController = new ScrollController();
  return Stack(
    children: [
      NestedScrollView(
        controller: scrollController,
        headerSliverBuilder: (context, value){
          return [
//            list of widgets in here
          ];
        },
        body: Container(
          // here, your normal body goes
        ),
      ),
      Positioned(
        top: 50.0,
        left: 100.0,
        child: Container(
          // your centered widget here
        ),
      )
    ]
  );
}

您必须使用 SliverAppBar,而不是使用普通的 appBar

【讨论】:

  • 谢谢,当我需要在 Sliver 中使用它时会试一试,这是我的下一个任务
  • 所有最好的朋友
猜你喜欢
  • 2021-09-29
  • 1970-01-01
  • 2020-03-04
  • 2019-04-09
  • 1970-01-01
  • 2022-01-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多