Is it possible to import a single file from material library to access its private fields? - flutter

I want to access a file from material.dart library ('src/material/search.dart';) so that I can access private fields (_somethingsomething) in it to create a widget.
I read a bit about part/part of and so on, but it seems that I am not using it properly.
Is something like this possible?

Steps: Copying a file from Flutter into your own app.
Create a file search_copy.dart, and copy the content of material's search.dart.
Remove the internal imports used in the file.
Instead add the import import 'package:flutter/material.dart' to search_copy.dart
Modify the new file according to your needs, like exposing the private field.
Import the file with a prefix import 'search_copy.dart' as search; so you don't get import conflicts with material itself.
When you want to access it do so like search.showSearch(context: context, delegate: delegate)
Note: The search.dart file is part of flutter itself which is BSD-licensed. (https://github.com/flutter/flutter/blob/master/packages/flutter/lib/src/material/search.dart)
License:
https://github.com/flutter/flutter/blob/master/packages/flutter/LICENSE

The answer is NO. And probably there is a reason why these fields are private since package owners (in this case, framework maintainers) do not want the developer to use the variables directly or change them in any way.
If you would like to adjust the code for the component in the material library, you could:
a) Create an issue under flutter repository and explain why you need to access the fields, maybe even create a POC by forking the repository and implementing your changes. The Flutter team may approve this and your changes will be a part of the framework - that's the beauty of open-source! However, there is no guarantee that it will happen.
b) Copy-paste the search.dart code to your project and adjust whatever is needed. This is a faster solution to your problem, however, now you should maintain this code by yourself, you would need to keep this component in sync with any Flutter update.

Related

I want to export my whole code base like a package or something like a module that I can import

I created an app for a small group of people. I created a lot of widgets/pages and models. Now another group asked if I could make them the app too. I could create a new project and copy/paste al stuff in there, but more groups want this app, and its a lot of work.
My idea was to create something like a package or export the whole code base to another destination, so that I only need to edit one code base and it changes on all app instances. For the new group I only need to change the API URL and some images which can be changed in pub spec.yaml and main.dart. Do you have a working solution for this?
Please read those articles about creating new package:
Flutter Website: link
Tutorial: link
Also, Pay attention to what is the supported platforms in you package and test it with each platform. You can also publish it to Pub.Dev if you want. but must be on GitHub first.

Call a Dart const from a String version of its name

I am using Flutter and the FontAwesome library and I need to create icons based on their name. So, I need to get the following:
FaIcon(FontAwesomeIcons.lightWalking);
...but from its name as a String.
Something like this:
FaIcon(FontAwesomeIcons["lightWalking"]); // <== this doesn't work in Dart
I can then build a function to return icons based on the name that I get out of a database.
I don't think this is a dart related question on first sight but rather a FontAwesomeIcons question unless you want to use reflection in dart. You need to access the Icons here which are simply not accessible the way you tried it.
See the following issue:
https://github.com/fluttercommunity/font_awesome_flutter/issues/102
Quote:
Hi, we don't support icon maps officially, but you can use this
generator by calling it in the updater tool. You will need a local
installation of font awesome for this, please follow the instructions
for pro icons and ignore steps that mention icons.json or .ttf files.
If you need further assistance feel free to ask.
That means the way you are trying to access your icons is not supported directly.
However you could use the generator tools to create such a map, iterate it and find a suitable icon. See the FontAwesome example for that (they generated a map and iterated it).
https://github.com/fluttercommunity/font_awesome_flutter/tree/master/example/lib
What might be a little more convenient might be a third party tool which already has such a map and allows you to search it.
https://pub.dev/packages/icons_helper/example
You can then do the following:
getIconUsingPrefix(name: "PREFIX.ICON_NAME")
You can achieve this only using reflection. There is a library called reflectable for Flutter. It will generate some code for you and then you will be able to access the class members of FontAwesomeIcons by their name as String.

Conditional package import for desktop in Flutter

I know that the availability of dart.library.html can be used as the condition for web, but what about desktop?
Although I suppose you can import a particular package for desktop and not for mobile if you create and use a new package in which different packages are specified for different platforms in pubspec.yaml as explained in the document,
it seems a little redundant to make such a package for that purpose.
Is it possible just by using a statement of the import 'foo.dart' if (...) 'bar.dart' style, and if possible, what library is put in its if (...) part?
You cannot use conditional imports to get different behavior between mobile and desktop; see this comment from the Dart team.

One code base that supports Flutter for Web and Flutter iOS/Android?

Looks like Flutter for Web and Flutter for Mobile have to exist as separate projects due to the imports.
Example:
import 'package:flutter_web/material.dart
vs
import 'package:flutter/material.dart';
Is there anyway to build one flutter project with one code base that works for both web and mobile (ios/android)? If not, is this coming?
If so, can you provide an example app?
Would like to just make one code base for the web and mobile and not have to maintain separate projects/code repos.
The OP's question is a bit old and is no longer applicable at the time of posting(7/21/2020). Flutter now has consolidated web into the main flutter package, which prevents us from running into issues with imports like this. flutter_web is no longer a separate package.
However, you may have been able to accomplish this even at the time you posted your question with conditional imports. This answer provides an excellent method of doing this. The following are the essentials of that post:
The core idea is as follows.
Create an abstract class to define the methods you will need to use in general.
Create implementations specific to web and android dependencies which extends this abstract class.
Create a stub which exposes a method to return the instance of this abstract implementation. This is only to keep the dart analysis tool happy.
In the abstract class import this stub file along with the conditional imports specific for mobile and web. Then in its factory constructor return the instance of the specific implementation. This will be handled automatically by conditional import if written correctly.
This method allows for you to do these imports based on platform and applies to all packages that may not support every possible flutter platform(e.g. dart:html, dart:js, dart:js_util, dart:io).

How does importing work in swift?

If i have a function, struct, enum or class in another file/nested folder
how do i import it to use in another file?
I'm a javascript dev and i've decided to pick up swift, so i'm still trying to wrap my head around this.
NOTE: I'm not building ios/macOS apps. I'm trying to build linux cli software.
If this is the same target - you don't import anything, you should be able to use it without problems.
If you have separate targets (e.g. a Framework for a library) you should do import ModuleName to get access to the code.
By default things are internal which means you can access them in the whole module. You can't if they're private or fileprivate
write "var main = Main()" any where you like to use it, then use main.getArgs
// by writing that you define instance of the struct