【问题标题】:Resize leading widget in flutter AppBar调整颤振 AppBar 中的前导小部件的大小
【发布时间】:2019-02-04 13:29:39
【问题描述】:

我正在制作一个高度高于典型 AppBar 的自定义 AppBar。我也想调整前导小部件/图标的大小,并利用automaticallyImplyLeading 默认行为(因此菜单图标和后退图标会自动实现)。

这是我认为我会实施的解决方案:

class AppAppBar extends PreferredSize{
  AppAppBar(String title) : super(
    preferredSize: Size.fromHeight(56.0),
    child: AppBar(
      centerTitle: true,
      title: Text(title, style: textStyle)
    )) {
    (child as AppBar).leading = 
        SizedBox(width: 30.0, height: 30.0, child: (child as AppBar).leading);
  }

  static const textStyle = TextStyle(fontSize: 32.0);
}

但这当然行不通,因为(child as AppBar).leading 是最终的。

因此,在下面的 AppBar 中(为了便于说明,文本大小显着变大),我想将自动添加的汉堡图标设置得更大。

你怎么看?是否有解决方案或者我应该放弃自动图标并自己添加它们?

编辑:添加了一张图片来说明我的意思

【问题讨论】:

  • 你能放一张你想要的图片吗?

标签: flutter


【解决方案1】:

你不能,因为它是一个预定义的小部件。

您可以使用 Row 小部件来解决它:

Scaffold(
key:_scaffoldKey,
drawer: Drawer(),
appBar: AppBar(
      automaticallyImplyLeading: false
      title: Row(
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: <Widget>[
          SizedBox(
              height: 20, // Your Height
              width: 20, // Your width
              child: IconButton( // Your drawer Icon 
                      onPressed: () => _scaffoldKey.currentState.openDrawer()),
                      icon: Icon(Icons.arrow_back, color: Colors.white),
          ),)
          // Your widgets here
        ],
      ),
    ),
)

您需要 Key 才能使用 _scaffoldKey.currentState.openDrawer() 打开抽屉。

automaticallyImplyLeading: false 将阻止默认抽屉图标。

【讨论】:

    【解决方案2】:

    一个简单的例子来展示拉菲乔纳斯的答案

    AppBar(
        automaticallyImplyLeading: false,
        title: Row(
          children: [
            Expanded(
              child: Text('One'),
            ),
            Center(
              child: Text('Two'),
            ),
            Expanded(
              child: Align(
                alignment: Alignment.centerRight,
                child: Text('Three'),
              ),
            ),
          ],
        ),
      ),
    

    【讨论】:

      【解决方案3】:

      您想要的是 Flutter 中的自定义应用栏。大多数人尝试在AppBartitle 参数中提供自己的小部件。但是让我告诉你如何正确地做到这一点。

      @override
      Widget build(BuildContext context) => Scaffold(
          appBar: _appBar(),
          body: _body(),
      );
      
      //Custom AppBar
      _appBar() => PreferredSize(
      
          //kToolBarHeight: Default size used by all AppBar widgets in Flutter.
          //MediaQuery...: viewPadding.top is StatusBar area. viewPadding.bottom is iPhone bottom bar.
          
          preferredSize: PreferredSize.fromHeight(kToolBarHeight + MediaQuery.of(context).viewPadding.top),
          child: Container(
              child: Row(
                //This will spread Row content evenly across the screen.
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  //Leading Widget
                  Icon(Icons.home),
      
                  //Title
                  Text("Hello World!"),
      
                  //Trailing Widget / Actions
                  Icon(Icons.home),
               ],
            ),
          ),
      );
      
      Widget _body() => Container(
          color: Colors.blue,
      );
      

      【讨论】:

        【解决方案4】:

        Flutter 中 AppBar 的前导 Widget 宽度可以使用 leadingWidth 属性调整大小。

        例如:

        AppBar(
          title: const Text('Title'),
          leadingWidth: 50,
          leading: Container()
        )
        

        【讨论】:

          【解决方案5】:

          要使用自定义的appBar前导按钮,代码如下。

           appBar: AppBar(
              title: Text('hello'),
              // automaticallyImplyLeading: false,
              elevation: 0,
              leadingWidth: 58,
          
              actions: [
                ProfileBar(),
              ],
              leading: Container(
                width: 14,
                //color: Colors.yellow,
                child: Row( // <--- Using row to avoid force resizing of leading 
                  children: [
                    Padding( // <--- Padding to make it look more nicer
                      padding: const EdgeInsets.only(left: 24.0),
                      child: GestureDetector(
                        onTap: () {
                          Navigator.of(context).pop();
                        },
                        child: SvgPicture.asset( // <-- Using SvgPicture package
                          'assets/svg/icons/backbtn.svg',
                          width: 24,
                          height: 24,
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ),
          

          【讨论】:

            【解决方案6】:

            您可以设置高度或宽度的边距。

            AppBar(
                leading: Container(
                  margin: EdgeInsets.symmetric(vertical: 15),
                  child: IconButton(
                    icon: Icon(CupertinoIcons.chevron_left),
                    onPressed: () {},
                  ),
                ),
            

            【讨论】:

              猜你喜欢
              • 2019-03-07
              • 2021-08-07
              • 2021-08-11
              • 2020-08-31
              • 1970-01-01
              • 1970-01-01
              • 2021-01-26
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多