【问题标题】:Globally change appbar shape in flutter在颤动中全局更改应用栏形状
【发布时间】:2020-08-27 20:19:12
【问题描述】:

我可以在 Flutter 中更改应用栏形状

    return Scaffold(
      appBar: AppBar(
        elevation: 0.0,
        shape: ContinuousRectangleBorder(
          borderRadius: const BorderRadius.only(
            bottomLeft: Radius.circular(80.0),
          ),
        ),
        title: Text('AppBar'),
      ),
    );

我不希望每次创建 Scaffold 时都将我的 AppBar 传递给它,我想在全局范围内进行(可能在主题中我们可以)

【问题讨论】:

    标签: flutter


    【解决方案1】:

    您可以创建一个单独的小部件,它会为您返回自定义应用栏,如下所示。

    class CustomAppBar extends PreferredSize {
      @override
      Size get preferredSize {
        return new Size.fromHeight(56.0);
      }
    
      @override
      Widget build(BuildContext context) {
        return AppBar(
          elevation: 0.0,
          shape: ContinuousRectangleBorder(
            borderRadius: const BorderRadius.only(
              bottomLeft: Radius.circular(80.0),
            ),
          ),
          title: Text('AppBar'),
        );
      }
    }
    

    现在您可以将此小部件分配给 appbar。

    【讨论】:

    • 太麻烦了,每次传给脚手架,脚手架太多了,我在找几行解决方案
    • 我不认为它很麻烦,因为当你使用 appbar 时,你必须使用这个小部件。这是什么问题?
    • 是的,你是对的,但我认为可能有更好的方法来做到这一点,而无需创建新的小部件
    【解决方案2】:

    您只需要使用您在其上创建 AppBarGlobalUtilclass 并在整个类中使用,如下所示

    GlobalUtil.dart

    import 'package:flutter/material.dart';
    
    import 'colors.dart';
    
    
    class GlobalUtil {
    
      static Widget showAppBar(BuildContext context, String msg, String path) {
        return AppBar(
          centerTitle: true,
          title: Text(
            msg,
            style: TextStyle(
                color: ColorConst.TEXT_DARK_GREY_COLOR,  /// USE THE COLOR AS PER YOUR REQUIREMENT
                fontFamily: 'Medium', /// USE THE FONT FAMILY AS PER YOUR REQUIREMENT
                fontSize: Dimens.TEXT_EXTRA_EXTRA_LARGE),////// USE THE DIMENSION OF TEXT AS PER YOUR REQUIREMENT
          ),
            actions: [
              new IconButton(
                icon: new Image.asset("YOUR_ICON_PATH",height: 20.0,width: 20.0,),
                onPressed: () => {
    
    
                },
              )
            ],
          leading: Padding(
              padding: EdgeInsets.all(8),
              child: new IconButton(
                // icon: new Icon(iconData, color: Colors.black),
                icon: new Image.asset(path),
                onPressed: () => Navigator.of(context).pop(),
              )),
          backgroundColor: Colors.PINK,
          elevation: 0.0,
        );
    
      }
    
    }
    

    在任何类中,您都可以按如下方式使用它

     GlobalUtil.showAppBar(context, "YOUR HEADING TEXT",
                "YOUR_ICON_FOR_BACK"),
    

    【讨论】:

    • 您的解决方案与viren的解决方案相同,您只需使用静态方法,就像主题一样,我想用几行代码轻松更改我的应用栏,假设我问“如何更改我的按钮全局着色?”我搜索了“您可以使用 ThemeData”,但您回答“创建自定义小部件或返回它的静态方法”
    • @WebMaster 有什么问题......你只需在你想要的所有屏幕上使用相同的小部件,效果相同
    猜你喜欢
    • 1970-01-01
    • 2020-10-27
    • 2021-01-11
    • 2020-07-24
    • 1970-01-01
    • 1970-01-01
    • 2020-03-28
    • 2019-10-03
    • 1970-01-01
    相关资源
    最近更新 更多