How to organize dart files inside packages in flutter

Viewed 2585

I'm new at programming using flutter and I'd like to develop a small project containing a few screens: Login, Home, Settings, User, PurchaseHistory, etc.

I need to organize the code inside packages so that it can readable easily.

If I develop an Android App, I'd create some packages: model, activity, fragment, util, etc. If I create a LoginActivty, I'd put it inside activity package. If I create a User model, I'd put it inside model package. And so on.

So If I develop a flutter project, where am I supposed to put all of the files I create so far?

For now I've created only model package.

2 Answers

First of all, in Flutter we don't deal with activities or fragments directly, that is a naming convention from Android itself.

There are many options to architect your app and organize your folders. I wouldn't say that there is a holy grail solution. So you have to try some of them and see the best fit for you.

At the end of this article, I show an option to a folder structure when working with flavors, like this:

Flavoring

But there are plenty of others, so I recommend you to see how some of the GitHub projects are organized, a good way to start is having a look at the projects from this repository. Especially the 'Open Source Apps' section.

I'm currently working on a project that follows an approach like you described. I don't know if it's the better structure, but it works really nice for me.

\lib
  \-model
  \-api
  \-bloc
  \-widgets (commom components)
  \-exceptions
  \-config (config classes/files like routes, theme, specific settings for each environment - dev, production, test)
  \-views
    \-login
    \-home
    \-user_profile
    \-...
  \-main.dart

----- EDITED -----

After work for almost a year with Flutter, I've tested some different structures, and there's one in particular that's really nice and provides an amazing organization...

Take a look at slidy, the following image represents the kind of organization it provides. More details in the package description.

project organization example

Related