简体   繁体   English

Flutter/Android 更改手势导航图标的背景颜色

[英]Flutter/Android change background color of Gesture Navigation icon

I have an icon set using a transparent image, on the home screen the background is white but when in the Gesture Navigation view the icon above the app screen is blue.我有一个使用透明图像设置的图标,在主屏幕上背景是白色的,但是在手势导航视图中,应用程序屏幕上方的图标是蓝色的。 How do I change this background color?如何更改此背景颜色? (Using flutter) (使用颤振)

手势导航视图android

Thats the primary color you can change it like this那是原色,您可以像这样更改它

MaterialApp(
    debugShowCheckedModeBanner: false,
    theme: ThemeData(
      primaryColor: Colors.lightGreen,//here.change this one
    ),
  )

It isn't from Android.它不是来自 Android。 Flutter's MaterialApp already provides a set of attributes for us, including a Prymary Color, ColorScheme, etc. The reason is obvious, if every developer had to write every theme aspect in every widget coding would be awful. Flutter 的 MaterialApp 已经为我们提供了一组属性,包括 Prymary Color、ColorScheme 等。原因很明显,如果每个开发人员都必须在每个小部件编码中编写每个主题方面,那就太糟糕了。 So Material widgets look for a theme, which Material will provide as default, if we don't overide it.因此,Material 小部件会寻找一个主题,如果我们不覆盖它,Material 将默认提供该主题。

Know this, the solution is:知道这一点,解决办法是:

  1. Overide some componentes of the whole Theme in the MaterialApp wich is my recomendation.在 MaterialApp 中覆盖整个主题的一些组件,这是我的建议。 The code bellow is an example of this.下面的代码就是一个例子。
  2. Wrap a specifc widget/s with an Theme widget that overides the define Theme for explict specified atributes.用一个 Theme 小部件包装一个特定的小部件,该小部件覆盖为显式指定属性的定义主题。
  3. Pass a custom theme directly in the widgets you are using.直接在您正在使用的小部件中传递自定义主题。 Very common to see in Text widgets when people do Text( "some text", style: TextStyle()) (note the TextStyle ), but this logic is apllied to a bunch of other widgets too, including buttons.当人们执行Text( "some text", style: TextStyle()) (注意TextStyle )时,在 Text 小部件中很常见,但这种逻辑也适用于许多其他小部件,包括按钮。 Disavantage of this is that you have to manual change every widget, so no auto darkmode and painfull design changes for reasonable size apps.这样做的缺点是您必须手动更改每个小部件,因此对于合理大小的应用程序,没有自动暗模式和痛苦的设计更改。 I do not recomend as a go to solution for every widget.我不建议将 go 作为每个小部件的解决方案。

Example of what I meant by overiding the default Theme of your App:我的意思是覆盖应用程序的默认主题的示例:

MaterialApp(
        debugShowCheckedModeBanner: false,
        title: 'Association App for AMDKP Integrated Plataform',
        theme: ThemeData(
          colorScheme: ColorScheme(
            brightness: Brightness.light,
            primary: consts.golden1,
            onPrimary: consts.black41,
            secondary: Colors.green.shade500,
            onSecondary: Colors.green.shade300,
            background: consts.greyWhite,
            onBackground: consts.black41,
            surface: Colors.white,
            onSurface: Colors.black45,
            error: Colors.red.shade900,
            onError: Colors.red.shade900,
          ),
          primarySwatch: Colors.blue,
          primaryColor: consts.golden1,
          elevatedButtonTheme: ElevatedButtonThemeData(
              style: ElevatedButton.styleFrom(
            shadowColor: consts.black41,
            primary: Theme.of(context).colorScheme.onSurface.withAlpha(150),
            onPrimary: Theme.of(context).colorScheme.surface,
          )),
          textButtonTheme: TextButtonThemeData(
              style: TextButton.styleFrom(
            primary: Colors.white.withAlpha(230),
            backgroundColor: Colors.black87.withAlpha(170),
            textStyle: Theme.of(context).textTheme.bodyMedium,
            padding: const EdgeInsets.symmetric(horizontal: 10.0),
          )),
          inputDecorationTheme: const InputDecorationTheme(
            focusedBorder: UnderlineInputBorder(
                borderSide: BorderSide(color: consts.golden1, width: 2)),
          ),
        ),
        home: const HomePage(),

So definitely take a look at flutter themes, it will empower your flutter developer skills and you will benefit a lot by using it anyway: :) Cheers所以一定要看看 flutter 主题,它将增强您的 flutter 开发人员技能,无论如何使用它您都会受益匪浅::) 干杯

I am also having this issue.我也有这个问题。 As a work around, I keep the Material App ThemeData's primary color as white.作为解决方法,我将 Material App ThemeData 的原色保留为白色。 Then used the Theme widget to override my page theme to use my custom theme.然后使用主题小部件覆盖我的页面主题以使用我的自定义主题。

https://api.flutter.dev/flutter/material/ThemeData-class.html https://api.flutter.dev/flutter/material/ThemeData-class.html

 class MyApp extends StatelessWidget { final _navigatorKey = GlobalKey<NavigatorState>(); NavigatorState? get _navigator => _navigatorKey.currentState; MyApp({super.key}); // App Routing Route<dynamic> _generateRoute(RouteSettings settings) { Widget newPage = Container(); switch (settings.name) { case AppRoutes.welcome: newPage = const WelcomePage(); break; case AppRoutes.login: newPage = LoginPage(); break; } return FadeRoute( page: Theme( data: lightTheme, child: newPage, ), ); } // This widget is the root of your application. @override Widget build(BuildContext context) { return MultiBlocProvider( providers: [ BlocProvider<AppBloc>( create: (_) => AppBloc()..add(InitializeAppEvent())), BlocProvider<AuthBloc>(create: (_) => AuthBloc()) ], child: BlocListener<AuthBloc, AuthState>( listener: (_, state) { if (state is Authenticated) { // Go to Main Page _navigator?.pushReplacementNamed(AppRoutes.home); } else { // Go to Login Page _navigator?.pushReplacementNamed(AppRoutes.login); } }, child: MaterialApp( navigatorKey: _navigatorKey, title: 'Flutter Demo', theme: ThemeData(primaryColor: Colors.white), initialRoute: AppRoutes.welcome, onGenerateRoute: _generateRoute, ))); } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM