how to use different pubspec.yaml in different environmnet with flutter - flutter

is it possible to use different pubspec.yaml in different environmnet? for example, in develop environment I use the dependencies from local folder, and config the local folder in the pubspec.yaml like this:
dependencies:
info:
path: ../common/info
in production environment I use the dependencies like this:
dependencies:
info: 1.0.0
is it possible to do like that? I do not want to change the pubspec.yaml every time. I have read the solution from here https://github.com/flutter/flutter/issues/45769 but I am not sure it is the proper way.

Related

Not a valid Dart package name - Flutter error

I want my app to be available for the web.
I run this command: flutter create .
I get this error: "Fredi" is not a valid Dart package name.
I know that there shouldn't be any capital letters. However, I do not know how to change the dart package name.
I have already used these packages successfully, but they seem to be changing other things, not the Dart Package Name.
change_app_package_name: ^1.1.0
rename: ^2.0.1
The only thing in my project with the name 'Fredi' is the root folder (original project name). However, I don't know how to change it (file explorer won't let me) and it will probably break things.
What step can I take?
Config Files
app/build.gradle -> applicationId "com.tomasward.fredi"
android manifest -> package="com.tomasward.fredi"
android manifest -> android:label="Fredi"> [I understand this is the name displayed to the user, I've tried changing it but it doesn't fix the error.]
You can run flutter create --project-name "your_valid_package_name" InvalidPackageNameDirectory to create a Flutter project in a directory with a non-valid package name.
In an already existing project, you can change the package name by modifying the name property in the pubspec.yaml at the root of the project (see here for more details on the pubspec).
You can probably rename your package name with the help of some dependency that you can find on pub.dev by typing "rename" in the search bar.
All those dependencies will help you rename your package to a lowercase only package name, then you will be able to use the flutter create . command to create the web folder for your app.

Own common libraries in own dart/flutter projects in VSCode?

I'm working on one project for some time now on flutter. Part of the source code has been designed so that it can be used again as is in other projects.
I'm working with Visual Studio Code.
Now I'm creating a second project. I'd like to organize folders this way:
Parent folder
Project1 folder
Project2 folder
my_library
Is it possible to add the library folder to the projects, as it is not inside their respective folders?
In pubspec.yaml of project 1, refer to the library as:
dependencies:
my_library:
path: ../my_library
The way to solve this isn't straightforward for beginners. So I summed up the proposed solutions here (I provide the names out of fairness, and follow related potential discussions below each one).
From Richard Heap:
In pubspec.yaml of project 1, refer to the library as:
dependencies:
my_library:
path: ../my_library
From me:
In ../my_library, add a specific pubspec.yaml. Something like:
name: my_libraries
description: my own common libraries
publish_to: 'none' # Remove this line if you wish to publish to pub.dev
version: 1.0.0+1
environment:
sdk: ">=2.17.1 <3.0.0"
dependencies:
flutter:
sdk: flutter
From me:
Drag and drop (maybe there's a menu) your my_libraries folder from a file explorer to the VSCODE Explorer panel, and choose "add folder to workspace". Of course, all dependencies to external libraries used in my_libraries must be specified in its own pub_spec.yaml file.
Remarks:
I added source files in my_libraries/lib/. Don't know if the lib sub-directory is mandatory. Didn't take time to test without, and I like better to keep the same structure in my_libraries than in projects.
in project (and my_library) source files, to import my_library source files, just do as:
import 'package/my_library/xxx.dart'
where xxx.dart is the file to import.

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

how to add modified plugin to my app? how to give path?

I modified the plugin dropdown_formfield and gave the name dropdown_formfield-hijacked.dart. Now I cant give a path to my dependency. it is located at the same level pubspec.yaml in my app.
dependencies:
dropdown-formfield:
path: 'what is the path'?
Typically, you might have a folder called projects where you keep your source.
You might have two projects:
/projects/some_project
/projects/dropdown_formfield
In the former, you refer to the latter as:
dropdown_formfield
path: ../dropdown_formfield