🧱 Inheritance & Encapsulation in Reusable Flutter Architecture
🔹 Inheritance
Inheritance allows Flutter widgets and classes to reuse, extend, and specialize functionality from base classes — keeping the UI and business logic modular and maintainable.
✅ Example — Reusable Base Widget
class BaseScreen extends StatelessWidget {
final String title;
const BaseScreen({required this.title, super.key});@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text(title)),
body: const Center(child: Text("Content area")),
);
}
}// Extended Screens
class DashboardScreen extends BaseScreen {
const DashboardScreen() : super(title: "Dashboard");
}class ReportsScreen extends BaseScreen {
const ReportsScreen() : super(title: "Reports");
}
Benefit:
Common layout (AppBar, Scaffold) reused across multiple screens — reduces code duplication, ensuring UI consistency.
🔹 Encapsulation
Encapsulation hides internal logic and exposes only necessary methods or properties.
This helps prevent state mutation from external widgets and keeps code more predictable.
Get Punith S Uppar’s stories in your inbox
Join Medium for free to get updates from this writer.
✅ Example — Encapsulated Provider
class LoginProvider extends ChangeNotifier {
bool _isLoading = false; // private
bool get isLoading => _isLoading; // public getterFuture<void> login(String user, String pass) async {
_isLoading = true;
notifyListeners(); // internal logic
await Future.delayed(const Duration(seconds: 2));
_isLoading = false;
notifyListeners();
}
}
Benefit:
Only the provider controls _isLoading.
The UI listens via getters — promoting clean, testable state management.
🧩 Real-world Reuse Example
In the system:
Concept Implementation Impact Base Form Widgets CustomTextField, CustomButton inherited from StatelessWidget Consistent behavior and validation across modules Encapsulated Providers InvoiceProvider, DashboardProvider Protected data flow with reactive UI updates Shared Layouts BaseScreen and ResponsiveCard classes Reduced layout duplication across dashboard, reports, and login
🧠 Summary
Principle Purpose Example Inheritance Code reuse through base classes Common UI layouts, shared widgets Encapsulation Hide internal logic, expose clean API Providers, private variables with getters
In Flutter architecture, inheritance ensures structural reusability, while encapsulation guarantees data integrity — together enabling clean, modular, and scalable codebases for production-ready apps.








