mirror of
https://github.com/smmarty/friflex_flutter_starter.git
synced 2025-12-22 09:30:45 +00:00
feat(app): Добавить пример со Scope (#5)
* feat(app): Добавить пример со Scope * fix scope * feat: добавить скоуп с внутренней зависимостью от репозитория (#6) Co-authored-by: Artem Barkalov <artembark@gmail.com> * feat: исправить обалсть видимости ProfileScope * feat: добавить фикс namespace плагинов --------- Co-authored-by: PetrovY <y.petrov@friflex.com> Co-authored-by: Artem Barkalov <artembark@gmail.com>
This commit is contained in:
77
lib/features/profile_scope/presentation/profile_scope.dart
Normal file
77
lib/features/profile_scope/presentation/profile_scope.dart
Normal file
@@ -0,0 +1,77 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:friflex_starter/app/app_context_ext.dart';
|
||||
import 'package:friflex_starter/features/profile_scope/domain/bloc/profile_scope_bloc.dart';
|
||||
|
||||
class ProfileInheritedScope extends InheritedWidget {
|
||||
const ProfileInheritedScope({
|
||||
required this.profileScopeBloc,
|
||||
required super.child,
|
||||
super.key,
|
||||
});
|
||||
|
||||
final ProfileScopeBloc profileScopeBloc;
|
||||
|
||||
@override
|
||||
bool updateShouldNotify(ProfileInheritedScope oldWidget) =>
|
||||
profileScopeBloc != oldWidget.profileScopeBloc;
|
||||
}
|
||||
|
||||
class ProfileScope extends StatefulWidget {
|
||||
const ProfileScope({
|
||||
required this.child,
|
||||
super.key,
|
||||
});
|
||||
|
||||
final Widget child;
|
||||
|
||||
static ProfileInheritedScope? maybeOf(
|
||||
BuildContext context, {
|
||||
bool listen = false,
|
||||
}) {
|
||||
return listen
|
||||
? context.dependOnInheritedWidgetOfExactType<ProfileInheritedScope>()
|
||||
: context.getInheritedWidgetOfExactType<ProfileInheritedScope>();
|
||||
}
|
||||
|
||||
static ProfileInheritedScope of(
|
||||
BuildContext context, {
|
||||
bool listen = false,
|
||||
}) {
|
||||
final result = maybeOf(context, listen: listen);
|
||||
|
||||
if (result == null) {
|
||||
throw StateError(
|
||||
'ProfileScope is not found above widget ${context.widget}',
|
||||
);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@override
|
||||
State<StatefulWidget> createState() => _ProfileScopeState();
|
||||
}
|
||||
|
||||
class _ProfileScopeState extends State<ProfileScope> {
|
||||
late final ProfileScopeBloc _profileScopeBloc;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
|
||||
_profileScopeBloc =
|
||||
ProfileScopeBloc(profileRepository: context.di.repositories.profileScopeRepository);
|
||||
_profileScopeBloc.add(const ProfileScopeFetchProfileEvent(id: '1'));
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_profileScopeBloc.close();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return ProfileInheritedScope(profileScopeBloc: _profileScopeBloc, child: widget.child);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:friflex_starter/features/profile_scope/domain/bloc/profile_scope_bloc.dart';
|
||||
import 'package:friflex_starter/features/profile_scope/presentation/profile_scope.dart';
|
||||
|
||||
// Класс экрана, где мы инициализируем ProfileScopeBloc
|
||||
class ProfileScopeScreen extends StatelessWidget {
|
||||
const ProfileScopeScreen({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return const ProfileScope(
|
||||
child: _ProfileScopeView(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _ProfileScopeView extends StatelessWidget {
|
||||
const _ProfileScopeView();
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(title: const Text('Profile Scope')),
|
||||
body: Center(
|
||||
child: BlocBuilder<ProfileScopeBloc, ProfileScopeState>(
|
||||
bloc: ProfileScope.of(context).profileScopeBloc,
|
||||
builder: (context, state) {
|
||||
return switch (state) {
|
||||
ProfileScopeSuccessState() => Text('Data: ${state.props.first}'),
|
||||
ProfileScopeErrorState() => Text('Error: ${state.message}'),
|
||||
_ => const CircularProgressIndicator(),
|
||||
};
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user