简体   繁体   English

flutter - 如何从父小部件获取原色?

[英]flutter - how get primary color from parent widget?

I have this code in my flutter app:我的 flutter 应用程序中有此代码:

return ParentThemeWidget{
  child: MyChildWidget(
     backgroundColor: Theme.of(context).primaryColor,
     ....
  )
}

this is my ParentThemeWidget:这是我的 ParentThemeWidget:

@override
Widget build(BuildContext context) {
  ThemeData themeData = ThemeData(primaryColor: MyColor);
  return Theme(data: themeData, child: child);
}

but this code:但是这段代码:

Theme.of(context).primaryColor

return not MyColor, this return primaryColor for app level.返回不是 MyColor,这将返回应用级别的 primaryColor。 How fix it?怎么修? how do I get the MyColor in MyChildWidget?如何在 MyChildWidget 中获取 MyColor?

Themes should be defined in main as root of your project, here is an approach that can help you with it.主题应在 main 中定义为项目的根目录,这是一种可以帮助您的方法。 When building MaterialApp use:构建 MaterialApp 时使用:

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);
  static final _themes = Themes();

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
          // ... your configuration

          darkTheme: ThemeData.dark().copyWith(),

          theme: ThemeData(
              primarySwatch: Colors.indigo,
              primaryColor: _themes.primaryColor,
              appBarTheme: _themes.getAppBarTheme(),
              floatingActionButtonTheme: _themes.getFabTheme(),
              outlinedButtonTheme: _themes.outlinedButtonTheme(),
              elevatedButtonTheme: _themes.elevatedButtonTheme(),
              listTileTheme: _themes.listTileTheme(),
              inputDecorationTheme: _themes.inputDecorationTheme()
          ),
     );
  }
}

As you can see, there is a class that I defined called Themes(), this themes are shared across all widgets in the application.如您所见,我定义了一个名为 Themes() 的 class,该主题在应用程序中的所有小部件之间共享。

import 'package:flutter/material.dart';

class Themes {
  final primaryColor = const Color.fromRGBO(48, 46, 122, 1);
  final secondaryColor = Colors.indigo;
  final filledColor = Colors.white;

  getAppBarTheme() {
    return const AppBarTheme().copyWith(backgroundColor: primaryColor);
  }

  getFabTheme() {
    return const FloatingActionButtonThemeData()
        .copyWith(backgroundColor: secondaryColor);
  }

  outlinedButtonTheme() {
    return OutlinedButtonThemeData(
        style: OutlinedButton.styleFrom(
            primary: secondaryColor,
            side: BorderSide(width: 1, color: secondaryColor),
            backgroundColor: const Color.fromRGBO(236, 236, 254, 1),
            padding: const EdgeInsets.symmetric(horizontal: 15, vertical: 15),
            shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(5))));
  }

  elevatedButtonTheme() {
    return ElevatedButtonThemeData(
        style: ElevatedButton.styleFrom(
            primary: secondaryColor, // Button color
            padding: const EdgeInsets.symmetric(horizontal: 15, vertical: 15),
            tapTargetSize: MaterialTapTargetSize.shrinkWrap,
            minimumSize: Size.zero,
            onPrimary: Colors.white, // Text color
            elevation: 0,
            shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(5))));
  }

  listTileTheme() {
    return const ListTileThemeData().copyWith(horizontalTitleGap: 0);
  }

  inputDecorationTheme() {
    return const InputDecorationTheme().copyWith(
        filled: true,
        fillColor: filledColor,
        border: const OutlineInputBorder(),
        enabledBorder: const OutlineInputBorder(),
        disabledBorder: const OutlineInputBorder(),
        errorBorder: const OutlineInputBorder(),
        focusedBorder: const OutlineInputBorder(),
        focusedErrorBorder: const OutlineInputBorder());
  }

}

On the other hand, make sure to use.copyWith() function everytime you want to override a style or theme, to avoid creating new ones.另一方面,确保每次要覆盖样式或主题时都使用.copyWith() function,以避免创建新的。

I solved my issue, I need to use widget Builder:我解决了我的问题,我需要使用小部件生成器:

 return ParentThemeWidget{
   child: Builder(builder: (context) {
      return  MyChildWidget(
       backgroundColor: Theme.of(context).primaryColor,
      ....
  )});
 }

More detail here更多细节在这里

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

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