-
Notifications
You must be signed in to change notification settings - Fork 10
/
base.view.dart
40 lines (34 loc) · 1.03 KB
/
base.view.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../viewmodels/base.viewmodel.dart';
import '../../locator.dart';
/// [BaseView]
/// Base class to manage a both a view and it related viewmodel
/// powered by provider package
class BaseView<T extends BaseViewModel> extends StatefulWidget {
final Widget Function(BuildContext context, T model, Widget? child)? builder;
final Function(T)? onModelReady;
const BaseView({
Key? key,
required this.builder,
this.onModelReady,
}) : super(key: key);
@override
_BaseViewState<T> createState() => _BaseViewState<T>();
}
class _BaseViewState<T extends BaseViewModel> extends State<BaseView<T>> {
T model = locator<T>();
@override
void initState() {
if (widget.onModelReady != null) {
widget.onModelReady!(model);
}
super.initState();
}
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider<T>(
create: (context) => model,
child: Consumer<T>(builder: widget.builder!));
}
}