flutterdart

How to use different assets for flutter flavor implementation?


I have created a flutter app on which I am want to set up 3 flavors. Each one of them is having different font, different set of images and different set of string file. How can I use particular font, images and String file for that particular flavor? Thanks in advance!


Solution

  • you can create app config file.

    import 'package:flutter/material.dart';
    
    enum Flavor { FLAV1, FLAV2 }
    
    class AppConfig extends InheritedWidget {
      final String appTitle;
      final String buildFlavor;
      final Flavor flavor;
      final Widget child;
      final ThemeData themeData;
      final String s3BucketURL;
      final String stripeKey;
    
      AppConfig(
          {@required this.appTitle,
          @required this.buildFlavor,
          @required this.themeData,
          @required this.flavor,
          @required this.s3BucketURL,
          @required this.stripeKey,
          @required this.child});
    
      @override
      bool updateShouldNotify(covariant InheritedWidget oldWidget) => false;
    
      static AppConfig of(BuildContext context) {
        return context.dependOnInheritedWidgetOfExactType();
      }
    }
    

    create folders assets/ assets/1/ assets/2/

    add to pubspec.yaml

    and you can use the indexs of flavors to get assets when accessing those.

    SvgPicture.asset(
                        'assets/${AppConfig.of(context).flavor.inex}/splash_logo.svg',
                        colorBlendMode: BlendMode.srcIn,
                        color: Theme.of(context).primaryColor,
                      )
    

    You can add different app config files when init. create different main files like main_flav1.dart, main_flav2.dev.

    void main() async {
      WidgetsFlutterBinding.ensureInitialized();
      var configureApp = AppConfig(
        appTitle: 'App staging',
        buildFlavor: 'Staging',
        flavor: Flavor.FLAV1,
        themeData: PreOrderTheme.flav1Theme,
        stripeKey: 'pk_s',
        s3BucketURL: 'https://sssss.net',
        child: PreOrderApp(repository: Repository(baseURL:"https://webapi.net/api")),
      );
      return runApp(configureApp);
    }
    

    please refer https://medium.com/@animeshjain/build-flavors-in-flutter-android-and-ios-with-different-firebase-projects-per-flavor-27c5c5dac10b.