Sub topic 11
Organizing Your App into Widgets and Files
This chapter assumes you know basic Dart syntax, can run a Flutter app, and understand StatelessWidget and the build method. The goal is to keep MaterialApp, screens, and reusable widgets separate as the app grows.
Why split an app into files?
A small Flutter app can begin in main.dart. That is useful while testing an idea, but one large file becomes difficult to read. It also makes it harder to find a screen or reuse a widget.
A useful starting structure is:
lib/main.dart: starts the app and configuresMaterialApp.lib/screens/home_screen.dart: defines the home screen.lib/screens/settings_screen.dart: defines the settings screen.lib/widgets/inbox_tile.dart: defines a widget used by more than one screen.
A file is not a runtime boundary. Flutter still builds one widget tree from all these classes. Files only organize the source code for people.
Keep app setup separate from screen content
main.dart should usually answer questions such as:
- What is the app called?
- Which theme does it use?
- Which screen opens first?
- Which routes are available?
The screen file should answer questions such as:
- What does this screen display?
- Which buttons and layout elements does it contain?
- What happens when the user interacts with it?
For example, main.dart can contain only app-level configuration:
import 'package:flutter/material.dart';
import 'screens/home_screen.dart';
import 'screens/settings_screen.dart';
void main() {
runApp(const InboxApp());
}
class InboxApp extends StatelessWidget {
const InboxApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Inbox',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.indigo),
useMaterial3: true,
),
initialRoute: '/',
routes: {
'/': (context) => const HomeScreen(),
'/settings': (context) => const SettingsScreen(),
},
);
}
}The import paths are relative to the lib directory. Therefore, screens/home_screen.dart refers to lib/screens/home_screen.dart.
Build screens from smaller widgets
A screen can contain a Scaffold, but its body does not need to contain every detail directly. Move a meaningful part of the interface into its own widget when it has a clear purpose or might be reused.
Here is lib/screens/home_screen.dart:
import 'package:flutter/material.dart';
import '../widgets/inbox_tile.dart';
class HomeScreen extends StatelessWidget {
const HomeScreen({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Inbox'),
actions: [
IconButton(
tooltip: 'Settings',
icon: const Icon(Icons.settings),
onPressed: () => Navigator.pushNamed(context, '/settings'),
),
],
),
body: ListView(
padding: const EdgeInsets.all(16),
children: const [
InboxTile(
sender: 'Alex',
subject: 'Project update',
),
InboxTile(
sender: 'Sam',
subject: 'Lunch tomorrow?',
),
],
),
floatingActionButton: FloatingActionButton(
onPressed: () {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Compose tapped')),
);
},
child: const Icon(Icons.edit),
),
);
}
}The reusable widget belongs in lib/widgets/inbox_tile.dart:
import 'package:flutter/material.dart';
class InboxTile extends StatelessWidget {
final String sender;
final String subject;
const InboxTile({
super.key,
required this.sender,
required this.subject,
});
@override
Widget build(BuildContext context) {
return Card(
child: ListTile(
leading: const CircleAvatar(child: Icon(Icons.person)),
title: Text(sender),
subtitle: Text(subject),
onTap: () {},
),
);
}
}InboxTile receives data through constructor parameters. This keeps the widget reusable. The widget does not need to know which screen created it.
Choose good widget boundaries
Create a separate widget when:
- A section has a clear name, such as
InboxTileorProfileHeader. - The same interface appears in multiple places.
- A
buildmethod becomes difficult to scan. - A section has its own state or behavior.
Do not split every single Text or Padding into a file. Excessive splitting hides the screen's structure and creates unnecessary navigation between files.
Keep shared widgets general. A widget named InboxTile can expose sender and subject. A widget named AlexProjectUpdateTile is tied to one piece of data and is less useful.
Organize by responsibility
A practical project can later grow into directories such as:
screens: complete pages connected to routes.widgets: reusable visual components.models: classes representing application data.services: code that talks to files, networks, or databases.
These folders are conventions, not Flutter requirements. Choose names that help your team locate code quickly. Keep navigation and app-wide configuration near MaterialApp, while keeping screen-specific layout in the screen class.
Exercise
Where should a reusable message tile be defined, and how should a screen provide its text?
Show answer
Define it in a file such as lib/widgets/inbox_tile.dart. Give it constructor parameters such as sender and subject, then pass those values from the screen.