【问题标题】:how can I use media queries when I want to override the app bar height in flutter?当我想在颤动中覆盖应用栏高度时,如何使用媒体查询?
【发布时间】:2020-08-15 09:04:31
【问题描述】:
class MyAppBar extends StatelessWidget implements PreferredSizeWidget {
 

  @override
  Widget build(BuildContext context) {
    final double appBarHeight = MediaQuery.of(context).size.height * .3;

    return AppBar(
      brightness: Brightness.light,

      automaticallyImplyLeading: false, // hides leading widget

      title: Padding(
        padding: const EdgeInsets.only(top: 15.0),
        child: TyperAnimatedTextKit(
            speed: Duration(milliseconds: 200),
            isRepeatingAnimation: false,
            text: [
              "SANDRA",
            ],
            textStyle: GoogleFonts.permanentMarker(
                fontSize: 30.0, color: Colors.black),
            textAlign: TextAlign.start,
            alignment: AlignmentDirectional.topStart // or Alignment.topLeft

            ),
      ),
      centerTitle: true,
      backgroundColor: Colors.white,
    );
  }

  @override
  Size get preferredSize => Size.fromHeight(88);
}

这是我的代码,我想使用媒体查询使应用程序在不同的屏幕上做出响应,在这种情况下如何使用媒体查询而不是代码末尾的 88?有什么建议吗?

【问题讨论】:

    标签: flutter dart mobile


    【解决方案1】:

    您可以使用PreferredSize,而不是实现PreferredSizeWidget

    Widget build(BuildContext context) {
      return Scaffold(
        appBar: PreferredSize(
          preferredSize: Size.fromHeight(MediaQuery.of(context)...),
          child: AppBar(...),
        ),
      );
    }
    

    【讨论】:

    • 我想将应用栏放在一个单独的文件中,这就是我实现首选尺寸小部件的原因。是否可以找到解决方案并将应用栏放在单独的文件中?
    • 不,但您可以制作自定义脚手架。
    猜你喜欢
    • 2016-02-10
    • 2021-11-09
    • 1970-01-01
    • 2014-09-27
    • 2017-07-19
    • 2020-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多