Skip to content

Adding pages screens repository with state managment #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: architektur_clean_mit_bounding
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions lib/data/counter/counter.repository.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,46 @@
import 'package:bloc/bloc.dart';
import 'package:counter/data/counter/data_source/api/counter.api.dart';
import 'package:counter/data/counter/data_source/db/counter.db.dart';
import 'package:counter/domain/counter/model/counter.entity.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';

part 'counter.repository.g.dart';

@riverpod
class CounterRiverpodController extends _$CounterRiverpodController {
@override
Future<CounterEntity> build() {
return Future.value(CounterEntity(value: 0));
}

Future<void> increment() async {
CounterEntity currentValue = state.value!;
state = const AsyncLoading();
await Future.delayed(const Duration(seconds: 2));
currentValue = currentValue.copyWith(value: currentValue.value + 1);
if (currentValue.value > 5) {
state = AsyncError('Level top high', StackTrace.fromString(''));
} else {
state = AsyncData(currentValue);
}
}

Future<void> reset() async {
ref.invalidate(counterRiverpodControllerProvider);
}
}

class CounterRepositoryCubit extends Cubit<CounterEntity> {
CounterRepositoryCubit(super.initialState);

Future<void> increment() async {
await Future.delayed(const Duration(seconds: 3), () {
CounterEntity currentCounter = state;
currentCounter = currentCounter.copyWith(value: currentCounter.value + 1);
emit(currentCounter);
});
}
}

class CounterRepository {
final CounterApi counterApi;
Expand Down
27 changes: 27 additions & 0 deletions lib/data/counter/counter.repository.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import 'package:counter/data/counter/data_source/api/counter.api.dart';
import 'package:counter/data/counter/data_source/db/counter.db.dart';
import 'package:counter/presentation/app.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

void main() {
final counterRepository =
CounterRepository(counterApi: CounterApi(), counterDB: CounterDb());

runApp(MyApp(counterRepository: counterRepository));
runApp(ProviderScope(child: MyApp(counterRepository: counterRepository)));
}
7 changes: 3 additions & 4 deletions lib/presentation/app.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import 'package:counter/data/counter/counter.repository.dart';
import 'package:counter/presentation/counter/counter.bloc.screen.dart';
import 'package:counter/presentation/counter/counter.riverpod.screen.dart';
import 'package:counter/presentation/counter/counter.screen.dart';
import 'package:flutter/material.dart';

Expand All @@ -16,10 +18,7 @@ class MyApp extends StatelessWidget {
appBarTheme: AppBarTheme(color: Theme.of(context).primaryColor),
useMaterial3: true,
),
home: CounterPage(
title: 'Flutter Demo Home Page',
counterRepository: counterRepository,
),
home: CounterRiverpodScreen(),
);
}
}
63 changes: 63 additions & 0 deletions lib/presentation/counter/counter.bloc.screen.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import 'package:counter/data/counter/counter.repository.dart';
import 'package:counter/domain/counter/model/counter.entity.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

class CounterBLocPageScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return BlocProvider(
create: (_) => CounterRepositoryCubit(CounterEntity(value: 0)),
child: CounterBlocView(),
);
}
}

class CounterBlocView extends StatefulWidget {
@override
State<CounterBlocView> createState() => _CounterBlocViewState();
}

class _CounterBlocViewState extends State<CounterBlocView> {
bool isLoading = false;

@override
Widget build(BuildContext context) {
final counterRepository = context.watch<CounterRepositoryCubit>();
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text('Counter Bloc'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'You have pushed the button this many times:',
),
isLoading
? CircularProgressIndicator()
: Text(
'${counterRepository.state.value}',
style: Theme.of(context).textTheme.headlineMedium,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: () async {
isLoading = true;
setState(() {});
await counterRepository.increment();
isLoading = false;
setState(() {});

///close overlay
},
tooltip: 'Increment',
child: const Icon(Icons.add),
),
);
}
}
48 changes: 48 additions & 0 deletions lib/presentation/counter/counter.riverpod.screen.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import 'package:counter/data/counter/counter.repository.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

class CounterRiverpodScreen extends ConsumerWidget {
@override
Widget build(BuildContext context, WidgetRef ref) {
final counterController = ref.watch(counterRiverpodControllerProvider);
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: const Text('Riverpod Screen'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'You have pushed the button this many times:',
),
counterController.when(
data: (data) {
return Text(
'${data.value}',
style: Theme.of(context).textTheme.headlineMedium,
);
},
loading: () => const Center(
child: CircularProgressIndicator(),
),
error: (error, __) {
return Text(error.toString());
},
)
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
ref.read(counterRiverpodControllerProvider.notifier).increment();
},
tooltip: 'Increment',
child: const Icon(Icons.add),
),
);
}
}
Loading