How to bundle asset for mobile apps only and not for Web - flutter

I'm trying to build a Flutter app with support for custom fonts. The quirk is, I ideally only want to bundle the fonts in mobile apps and not on Web without making significant changes to code at build time.
Is there any method?
I added Font Files into assets and pubspec, also used the package google_fonts.

Here's how you can do it:
Create two folders in your project directory: fonts and fonts_web.
Add your font files to the fonts folder.
Update your pubspec.yaml file to include the font files in the assets section:
yaml
flutter:
assets:
- fonts/
Then you need to Install google_fonts package to make it easy and smooth to use custom fonts in your app:
yaml
dependencies:
google_fonts: ^2.1.0
Then nn your code, import google_fonts package, and use GoogleFonts widget to specify custom font:
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
dart
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'My App',
theme: ThemeData(
textTheme: GoogleFonts.openSansTextTheme(),
),
home: MyHomePage(),
);
}
}
For including the fonts only for mobile apps, you need use conditional imports based on platform. Then Replace the assets section in pubspec.yaml file with the code shown below :
yaml
flutter:
assets:
- ${{false ? "ignore" : "fonts/"}}
- ${{true ? "fonts_web/" : "ignore/"}}
Create the fonts_web folder, add an empty ignore file in it. It will prevent your fonts from being included in the web builds.

Related

Internationalization in a monorepo

I have an app (just frontend) whose code is structured in a monorepo. There are separate packages for each custom widget. I would also like to have one package with all the translations. In this way, if another package need a translation I just need to import that package. So I created the translation package and in its main file I just wrote (here I use intl):
export 'package:flutter_gen/gen_l10n/app_localizations.dart';
Now, if I import that package in another package and I import the file with:
import 'package:l10n/l10n.dart';
It tells me that that import is not used and gives me error when I use the translation with AppLocalizations.of(context)!.foo
If you use VScode (don't know for other editors) you have to manualy import the generated translation package:
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
It seems that it's not possible to export generated file by design (check out this github issue). The best solution that I've found to fix my problem is the following:
Create a translation package inside your project, like in packages/translations and add that package to the pubspec.yaml of your your main project. Like this:
dependencies:
flutter:
sdk: flutter
translations:
path: packages/translations
In that package create a l10n.yaml file like this:
arb-dir: lib
template-arb-file: app_en.arb
output-localization-file: app_localizations.dart
synthetic-package: false
If you are working in a git repo, add packages/translations/lib/app_localizations*.dart in your .gitignore
Now if you run flutter packages get && flutter gen-l10n in that translations packages all the translations will be automatically generated (assuming they are in packages/translations/lib/app_xx.arb)
Now you can import the translation file from you main package with import 'package:translations/app_localizations.dart';
The only drawback is that the translations are not automatically generated when you type flutter pub get (see this issue). In order to regenerate them you have to type flutter gen-l10n inside the translations package every time. The solution could be improved by using a tool for managing Dart monorepo projects like melos.
An example implementation of what I described (including melos) could be found in this github repo

Combining multiple different flutter projects

So I have received a project which requires e-commerce, food ordering and car hailing services like Grab to be inside one app. I have delivered all these 3 apps in different projects before and for this new project I'm wondering whether it is possible to combine all my previous source code into one project, running on different dependencies.
You can create packages for all 3 modules (food, e-commerce, car-hailing services)
To create a Flutter package, use the --template=package flag with flutter create:
flutter create --template=package food
Folder Structure
lib
packages
- food
- e_commerce
- car_hailing_service
Import this in the main module's pubspec.yaml file
dependencies:
flutter:
sdk: flutter
# My Custom Packages
food:
path: packages/food
e_commerce:
path: packages/e_commerce
car_hailing_service:
path: packages/car_hailing_service
If you have any common functionalities for all 3 packages then create a separate package for that also and import that common package for all 3 packages.
Refer to this document for creating flutter packages. I hope this helps you.

How to add localization support inside Flutter packages

I am creating a Flutter package that has some text inside it. I want the consumer application of my package to pass locale to it, based on that locale my package should decide whether to show this text in 'Arabic' or 'English' (This means my package will have resource file containing strings inside it for these locales). How can I achieve this?
The only thing I was able to achieve was that my consumer application has the resource file and both my consumer application and package have to register the same localization plugin as dependency. I do not want my consumer app to worry about localization and instead my package should handle showing of translated strings based on locale. Is there anything wrong with my approach?
I have implemented the same by following what is done in another package - Catcher. Here is the file - https://github.com/jhomlala/catcher/blob/master/lib/model/localization_options.dart
In order to generate localization files inside a package:
Create localization files in .arb format
add l10n.yaml file inside the root folder of the package with these settings:
# Where to find translation files
arb-dir: lib/l10n
# Which translation is the default/template
template-arb-file: app_en.arb
# What to call generated dart files
output-localization-file: app_localizations.dart
make sure your main localization file and the 'template-arb-file' names are the same
update 'dependencies' and 'flutter' in pubspec.yaml file of the package as below:
dependencies:
flutter_localizations:
sdk: flutter
intl: ^0.17.0
flutter:
uses-material-design: true
generate: true
run 'flutter pub get' inside the package root location
Wrap your main UI widget with a MaterialApp
Update MaterialApp as below
MaterialApp(
localizationsDelegates: AppLocalizations.localizationsDelegates,
supportedLocales: AppLocalizations.supportedLocales,
),
Call the localization key inside a widget:
Text(AppLocalizations.of(context)!.helloWorld)
Note: Basically do almost same steps in official documentation inside your package root folder with a Custom MaterialApp
https://docs.flutter.dev/development/accessibility-and-localization/internationalization

flutter package can not find classes in package

I am trying to create a flutter package.
Inside Lib(package) folder, I have MY-PACKAGE.dart file. I also have a folder Called src which contains some codes I will import for use in MY-PACKAGE.dart.
In my pubspec.yaml in Example folder I have added the package like below and run packages get.
onboardly:
path: ../
To use my package I do this
// THERE IS NOT ANY PROBLEM WITH THIS IMPORT STATEMENT
import 'package:onboardly/onboardly.dart';
OnBoardly( // THIS CLASS WORKS FINE
screens: [
OnBoardlyScreenItem( // THIS CLASS CAN NOT BE FOUND EVEN THO IT EXISTS IN THE src FOLDER
image: Image.asset("assets/loadicon.png"),
description: Text("Hello There"),
),
],
),
The problem am facing is the fact that OnBoardlyScreenItem() which is in the src of the package can not be found.
I have ran
flutter packages get
flutter pub get
restarted my IDE
run flutter clean
Adding this for anyone later,
I had to export those classes or files that I wanted to be available to the Package to the "Entry file" since the folders in my case are regarded as private. Export all files or classes you'll want to be available to the user.
// Exporting all codes to be avaible to package
export 'package:onboardly/src/IntroScreen/OnBoardlyScreenItem.dart';

Flutter: How do I change main.dart(entry point) to some other page in Flutter?

I started working on the "home page" of the application in main.dart.
Then I created "Login.dart" page, and now I want my application to start with "Login.dart" page.
I am newbie in Flutter.
The -t parameter does that which is supported by various commands (run, build, ...)
flutter run -t lib/my_other_main.dart
In Android Studio, You can change your Dart entry point from Edit configurations.
Go to: Run -> Edit Configurations -> Dart entrypoint
If you want to build your release app with the main entrypoint in a file other than lib/config/main_production.dart, you have to do so like this:
flutter build apk -t lib/config/main_production.dart
Follow these pictures:
be sure you write the right path:
hint: ("lib/start.dart") just example.
image(5) to test your new entrypoint:
flutter run -t lib/main_admin.dart the t stands for target, the default of which is lib/main.dart. If you don't specify the -t you will go to lib/main.dart
you can also pass in --flavor to the flutter run command if you would like to load a different flavour for your same entry point - ie production, dev & test
In your main.dart you can specify the first screen that your app opens to:
runApp(new MaterialApp(
debugShowCheckedModeBanner: false,
theme: //theme
title: "Title",
home: new Login(), //Here you can specify the screen the app starts on.
routes: <String, WidgetBuilder>{
//routes
},
));
I'm not sure if this is any better than Günter's answer, but mine will mean you don't have to always specify the file name when building or running.
We have a separate file for this. Please follow below steps:
1.Go to test> widget.test.dart
2.Change import package:flutter_async/main.dart
to package:flutter_async/your_file_name.dart
and in the class which you have defined you can use your MyApp (which runapp function will take as a input) class or you can rename Myapp class also to some other class from widget.testfile as a first screen for your app.