https://github.com/karbunkul/flutter_contract_prop
DI for widgets, override contract
https://github.com/karbunkul/flutter_contract_prop
Last synced: 4 months ago
JSON representation
DI for widgets, override contract
- Host: GitHub
- URL: https://github.com/karbunkul/flutter_contract_prop
- Owner: karbunkul
- License: mit
- Created: 2022-05-22T17:39:00.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2022-05-22T20:24:06.000Z (about 4 years ago)
- Last Synced: 2025-10-23T00:43:50.076Z (8 months ago)
- Language: Dart
- Size: 170 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# contract_prop
Dependency Injection (DI) for widget properties, provide override contract from inherited scope.
## Getting started
Add package to your project ``` flutter pub add any_to_widget```
Implements ContractInterface for your contract. For example
```dart
class LabelContract with ContractInterface {
final String name;
LabelContract({this.name = 'foo bar'});
ContractProp get label => ContractProp(contract: (_) => name);
}
```
Create widget, wrap your widget ContractPropBuilder
```dart
class ContractText extends StatelessWidget {
final String? text;
const ContractText({Key? key, this.text}) : super(key: key);
@override
Widget build(BuildContext context) {
return ContractPropBuilder(
contract: LabelContract(),
builder: (_, contract) => Text(
text ?? contract.label.value(context) ?? '',
),
);
}
}
```
Override contracts
```dart
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
builder: (_, child) {
return ContractScope(
contracts: [
LabelContract(name: 'Hello world'),
],
child: child!,
);
},
home: const Scaffold(body: Center(child: DemoPage())),
);
}
}
```
## Additional information
See tests for more example