Ny Regency
Ny Regency

Reputation: 1722

Error: Don't import implementation files from another package

I am trying to make a custom stepper in Flutter. I copied the implementation of stepper.dart, save my file to my own lib folder and fixed the imports to remove the errors.

import 'package:flutter/src/material/button_theme.dart';
import 'package:flutter/src/material/colors.dart';
import 'package:flutter/src/material/debug.dart';
import 'package:flutter/src/material/flat_button.dart';
import 'package:flutter/src/material/icons.dart';
import 'package:flutter/src/material/ink_well.dart';
import 'package:flutter/src/material/material.dart';
import 'package:flutter/src/material/material_localizations.dart';
import 'package:flutter/src/material/theme.dart';
import 'package:flutter/src/material/typography.dart';

Errors are removed but dart says that "Don't import implementation files from another package."

May I know if it's safe to proceed? OR is there another way to implement custom widgets? Or should I transfer the location of my custom stepper file? Thanks.

Upvotes: 14

Views: 10790

Answers (2)

Soundbytes
Soundbytes

Reputation: 1249

Flutter Material Widgets are not meant to be subclassed. Here is what I do to create a customized version of a Widget:

(1) Find the source file of the original widget and copy it to the lib directory of your project.
(you already did that)

(2) remove all import statements from the copied file and insert the line
import 'package:flutter/material.dart'; instead

(3) Check the Dart Analysis for missing packages. To add those click on the unknown (red underlined) class name in the copied source file then hit ALT+Enter and select the context menu entry that offers to add the missing dependency.

(4) Now modify the Widget to your hearts delight.

(5) to use the modified Widget in your project import it like this:
import 'stepper.dart' as my;

(6) You can now access your modified stepper as
my.Stepper stepper = my.Stepper(...);

Upvotes: 15

Günter Zöchbauer
Günter Zöchbauer

Reputation: 657248

lib/src from other packages is considered to be private by convention. The analyzer explains that you shouldn't import such files.

Flutter exports everything it considers public under package:flutter/... anyway. Import these files instead.

See also https://www.dartlang.org/tools/pub/package-layout#implementation-files

Upvotes: 3

Related Questions