Home > Mobile >  Is it possible to import a single file from material library to access its private fields?
Is it possible to import a single file from material library to access its private fields?

Time:03-15

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?

CodePudding user response:

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.

CodePudding user response:

Steps: Copying a file from Flutter into your own app.

  1. Create a file search_copy.dart, and copy the content of material's search.dart.
  2. Remove the internal imports used in the file.
  3. Instead add the import import 'package:flutter/material.dart' to search_copy.dart
  4. Modify the new file according to your needs, like exposing the private field.
  5. Import the file with a prefix import 'search_copy.dart' as search; so you don't get import conflicts with material itself.
  6. 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

  • Related