Skip to content

Commit

Permalink
feat: Refactor project structure and integrate Clean Architecture
Browse files Browse the repository at this point in the history
1. lib/app/:
   - Reviewed and optimized global configurations and routes.
   - Updated `app.dart` with improved root widget setup.

2. config/:
   - Added and refined application theme and settings.
   - Simplified configurations for better scalability.

3. navigation/:
   - Integrated `go_router` for route management.
   - Created `app_router.dart` and `routes.dart` for centralized navigation.

4. lib/core/:
   - Restructured common providers, helper classes, and constants.
   - Enhanced modularity and readability.

5. lib/features/:
   - Improved feature-based module structure by separating into:
     - **presentation/**: Widgets, Pages, and UI components.
     - **application/**: State Notifiers, Providers, and business logic.
     - **domain/**: Entities, Use Cases, and core business rules.
     - **infrastructure/**: Repository implementations, data sources, and external integrations.
   - Added new Entity and Use Case definitions in the domain layer.
   - Enhanced state management with StateNotifier in the application layer.

6. lib/main.dart:
   - Refactored entry point for better alignment with Clean Architecture.
   - Optimized Firebase initialization as a separate function.

Additionally:
- Fully integrated Clean Architecture principles.
- Removed unnecessary files and legacy code.
- Improved Riverpod integration and configured ProviderScope.

These changes aim to enhance project maintainability and simplify the addition of new features.
  • Loading branch information
ersinaksar committed Dec 3, 2024
1 parent c66b1f0 commit ea8b331
Show file tree
Hide file tree
Showing 11 changed files with 127 additions and 122 deletions.
16 changes: 16 additions & 0 deletions lib/app/app.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import 'package:flutter/material.dart';
import 'config/app_config.dart';
import 'navigation/app_router.dart';

class StructureApp extends StatelessWidget {
const StructureApp({Key? key}) : super(key: key);

@override
Widget build(BuildContext context) {
return MaterialApp.router(
title: AppConfig.appName,
theme: AppConfig.themeData,
routerConfig: appRouter,
);
}
}
10 changes: 10 additions & 0 deletions lib/app/config/app_config.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import 'package:flutter/material.dart';

class AppConfig {
static const String appName = 'Structure App';

static ThemeData themeData = ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
);
}
8 changes: 8 additions & 0 deletions lib/app/navigation/app_router.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';

import 'routes.dart';
final GoRouter appRouter = GoRouter(
initialLocation: '/',
routes: appRoutes,
);
13 changes: 13 additions & 0 deletions lib/app/navigation/routes.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';

import '../../features/counter/presentation/pages/counter_page.dart';

List<GoRoute> appRoutes = [
GoRoute(
path: '/',
name: 'counter',
builder: (BuildContext context, GoRouterState state) => const CounterPage(),
),
// The other routes will come here.
];
5 changes: 5 additions & 0 deletions lib/core/providers/firebase_provider.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import 'package:firebase_core/firebase_core.dart';

Future<void> initializeFirebase() async {
await Firebase.initializeApp();
}
18 changes: 18 additions & 0 deletions lib/features/counter/application/counter_notifier.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../domain/entities/counter.dart';

class CounterNotifier extends StateNotifier<Counter> {
CounterNotifier() : super(Counter(value: 0, incrementValue: 0));

void increment() {
state = state.copyWith(
value: state.value + 1,
incrementValue: 1,
);
print(state.value);
}
}

final counterProvider = StateNotifierProvider<CounterNotifier, Counter>(
(ref) => CounterNotifier(),
);
13 changes: 13 additions & 0 deletions lib/features/counter/domain/entities/counter.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Counter {
final int value;
final int incrementValue;

Counter({required this.value, required this.incrementValue});

Counter copyWith({int? value, int? incrementValue}) {
return Counter(
value: value ?? this.value,
incrementValue: incrementValue ?? this.incrementValue,
);
}
}
30 changes: 30 additions & 0 deletions lib/features/counter/presentation/pages/counter_page.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

import '../../application/counter_notifier.dart';

class CounterPage extends ConsumerWidget {
const CounterPage({Key? key}) : super(key: key);

@override
Widget build(BuildContext context, WidgetRef ref) {
final counter = ref.watch(counterProvider);

return Scaffold(
appBar: AppBar(
title: const Text('Structure App counter'),
),
body: Center(
child: Text(
'Counter: ${counter.value}',
style: Theme.of(context).textTheme.headlineMedium,
),
),
floatingActionButton: FloatingActionButton(
onPressed: () => ref.read(counterProvider.notifier).increment(),
tooltip: 'Increment',
child: const Icon(Icons.add),
),
);
}
}
129 changes: 8 additions & 121 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import 'package:flutter/material.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

import 'app/app.dart';

void main () async {
WidgetsFlutterBinding.ensureInitialized();
Expand All @@ -14,125 +17,9 @@ void main () async {
badge: true,
sound: true,
);
runApp(const MyApp());
}

class MyApp extends StatelessWidget {
const MyApp({super.key});

// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
// This is the theme of your application.
//
// TRY THIS: Try running your application with "flutter run". You'll see
// the application has a purple toolbar. Then, without quitting the app,
// try changing the seedColor in the colorScheme below to Colors.green
// and then invoke "hot reload" (save your changes or press the "hot
// reload" button in a Flutter-supported IDE, or press "r" if you used
// the command line to start the app).
//
// Notice that the counter didn't reset back to zero; the application
// state is not lost during the reload. To reset the state, use hot
// restart instead.
//
// This works for code too, not just values: Most code changes can be
// tested with just a hot reload.
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}

class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});

// This widget is the home page of your application. It is stateful, meaning
// that it has a State object (defined below) that contains fields that affect
// how it looks.

// This class is the configuration for the state. It holds the values (in this
// case the title) provided by the parent (in this case the App widget) and
// used by the build method of the State. Fields in a Widget subclass are
// always marked "final".

final String title;

@override
State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;

void _incrementCounter() {
setState(() {
// This call to setState tells the Flutter framework that something has
// changed in this State, which causes it to rerun the build method below
// so that the display can reflect the updated values. If we changed
// _counter without calling setState(), then the build method would not be
// called again, and so nothing would appear to happen.
_counter++;
});
}

@override
Widget build(BuildContext context) {
// This method is rerun every time setState is called, for instance as done
// by the _incrementCounter method above.
//
// The Flutter framework has been optimized to make rerunning build methods
// fast, so that you can just rebuild anything that needs updating rather
// than having to individually change instances of widgets.
return Scaffold(
appBar: AppBar(
// TRY THIS: Try changing the color here to a specific color (to
// Colors.amber, perhaps?) and trigger a hot reload to see the AppBar
// change color while the other colors stay the same.
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text(widget.title),
),
body: Center(
// Center is a layout widget. It takes a single child and positions it
// in the middle of the parent.
child: Column(
// Column is also a layout widget. It takes a list of children and
// arranges them vertically. By default, it sizes itself to fit its
// children horizontally, and tries to be as tall as its parent.
//
// Column has various properties to control how it sizes itself and
// how it positions its children. Here we use mainAxisAlignment to
// center the children vertically; the main axis here is the vertical
// axis because Columns are vertical (the cross axis would be
// horizontal).
//
// TRY THIS: Invoke "debug painting" (choose the "Toggle Debug Paint"
// action in the IDE, or press "p" in the console), to see the
// wireframe for each widget.
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headlineMedium,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
runApp(
const ProviderScope(
child: StructureApp(),
),
);
}
4 changes: 4 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,13 @@ dependencies:
flutter:
sdk: flutter
async: '>=2.11.0 <3.0.0'

firebase_core: '>=2.32.0 <4.0.0'
firebase_messaging: ^14.3.0

go_router: ^14.3.0
flutter_riverpod: ^2.6.1


# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
Expand Down
3 changes: 2 additions & 1 deletion test/widget_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
// tree, read text, and verify that the values of widget properties are correct.

import 'package:flutter/material.dart';
import 'package:flutter_high_level_project_structure/app/app.dart';
import 'package:flutter_test/flutter_test.dart';

import 'package:flutter_high_level_project_structure/main.dart';

void main() {
testWidgets('Counter increments smoke test', (WidgetTester tester) async {
// Build our app and trigger a frame.
await tester.pumpWidget(const MyApp());
await tester.pumpWidget(const StructureApp());

// Verify that our counter starts at 0.
expect(find.text('0'), findsOneWidget);
Expand Down

0 comments on commit ea8b331

Please sign in to comment.