【问题标题】:Globally remove all drop shadows in Flutter app?全局删除 Flutter 应用中的所有阴影?
【发布时间】:2021-11-30 11:50:18
【问题描述】:

有没有办法全局删除 Flutter 应用中的所有阴影? 我想在一个地方执行此操作,而不是为所有 MaterialButtons、ElevatedButtons 等设置 elevation: 0。 我想设置主题,或者以其他方式设置主题,但在全局范围内进行。

我在 ThemeData 中寻找属性,但找不到所需的属性,例如对于 MaterialButtons。

【问题讨论】:

    标签: flutter flutter-theme


    【解决方案1】:

    你在正确的轨道上,ThemeData 有一个 ElevatedButtons 的属性,我做了一个小例子,说明你需要根据 MaterialStateProperty 移除阴影:

    
    void main() {
      runApp(MyApp());
    }
    
    class MyApp extends StatelessWidget {
      //this is the function that deletes shadows
      double getElevation(Set<MaterialState> states) {
        return 0;
      }
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          theme: ThemeData.light().copyWith(
              //this is the property that affects elevated buttons
              elevatedButtonTheme: ElevatedButtonThemeData(
                  style: ButtonStyle(
                      elevation: MaterialStateProperty.resolveWith(getElevation)))),
          debugShowCheckedModeBanner: false,
          home: Scaffold(
            body: Center(
              child: MyWidget(),
            ),
          ),
        );
      }
    }
    
    class MyWidget extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        //this button doesn't have a shadow
        return ElevatedButton(
          child: const Text("HEY!"),
          onPressed: () {},
        );
      }
    }
    

    【讨论】:

    • 谢谢,我刚刚检查过了,它适用于ElevatedButton。但是,它不适用于MaterialButton
    • 是的,MaterialButton 已经过时了,因为它使用的是函数而不是属性,所以我建议迁移并使用 ElevatedButton 而不是 MaterialButton。文档声明您应该尽快迁移api.flutter.dev/flutter/material/ButtonTheme-class.html
    【解决方案2】:

    解决方案非常简单,

    ThemeData(
       shadowColor: Colors.transparent,
    );
    

    完成任务

    【讨论】:

      猜你喜欢
      • 2018-08-07
      • 2010-11-07
      • 2019-10-15
      • 2018-04-28
      • 1970-01-01
      • 1970-01-01
      • 2015-11-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多