mirror of
https://github.com/pese-git/cherrypick.git
synced 2026-01-24 05:25:19 +00:00
implement example
This commit is contained in:
41
examples/client_app/lib/main.dart
Normal file
41
examples/client_app/lib/main.dart
Normal file
@@ -0,0 +1,41 @@
|
||||
import 'package:cherrypick/cherrypick.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:cherrypick_flutter/cherrypick_flutter.dart';
|
||||
import 'my_home_page.dart';
|
||||
import 'use_case.dart';
|
||||
|
||||
void main() {
|
||||
// Здесь происходит инициализация рутового скоупа и привязка зависимостей
|
||||
CherryPick.openRootScope().installModules([
|
||||
// Создаем модуль, который будет предоставлять UseCase
|
||||
ModuleWithUseCase(),
|
||||
]);
|
||||
|
||||
runApp(
|
||||
const CherryPickProvider(
|
||||
child: MyApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
class MyApp extends StatelessWidget {
|
||||
const MyApp({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
title: 'Example App',
|
||||
theme: ThemeData(primarySwatch: Colors.blue),
|
||||
home: MyHomePage(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Модуль для настройки зависимостей
|
||||
class ModuleWithUseCase extends Module {
|
||||
@override
|
||||
void builder(Scope currentScope) {
|
||||
// Привязка UseCase как singleton
|
||||
bind<UseCase>().toInstance(UseCase());
|
||||
}
|
||||
}
|
||||
21
examples/client_app/lib/my_home_page.dart
Normal file
21
examples/client_app/lib/my_home_page.dart
Normal file
@@ -0,0 +1,21 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:cherrypick_flutter/cherrypick_flutter.dart';
|
||||
import 'use_case.dart';
|
||||
|
||||
class MyHomePage extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
// Разрешение зависимости UseCase из рутового скоупа
|
||||
final UseCase useCase =
|
||||
CherryPickProvider.of(context).openRootScope().resolve<UseCase>();
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text('Example App'),
|
||||
),
|
||||
body: Center(
|
||||
child: Text(useCase.fetchData()),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
3
examples/client_app/lib/use_case.dart
Normal file
3
examples/client_app/lib/use_case.dart
Normal file
@@ -0,0 +1,3 @@
|
||||
class UseCase {
|
||||
String fetchData() => "Data fetched by UseCase";
|
||||
}
|
||||
Reference in New Issue
Block a user