You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
diplomatic-quarter/lib/pages/base/base_view.dart

66 lines
1.8 KiB
Dart

4 years ago
import 'package:diplomaticquarterapp/config/shared_pref_kay.dart';
import 'package:diplomaticquarterapp/core/service/AuthenticatedUserObject.dart';
import 'package:diplomaticquarterapp/core/viewModels/base_view_model.dart';
4 years ago
import 'package:diplomaticquarterapp/core/viewModels/project_view_model.dart';
import 'package:diplomaticquarterapp/uitl/app_shared_preferences.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../../locator.dart';
4 years ago
AppSharedPreferences sharedPref = AppSharedPreferences();
class BaseView<T extends BaseViewModel> extends StatefulWidget {
final Widget Function(BuildContext context, T model, Widget child) builder;
final Function(T) onModelReady;
4 years ago
final bool allowAny;
BaseView({
this.builder,
this.onModelReady,
4 years ago
this.allowAny = false,
});
@override
_BaseViewState<T> createState() => _BaseViewState<T>();
}
class _BaseViewState<T extends BaseViewModel> extends State<BaseView<T>> {
T model = locator<T>();
4 years ago
AuthenticatedUserObject authenticatedUserObject =
locator<AuthenticatedUserObject>();
bool isLogin = false;
@override
void initState() {
if (widget.allowAny && widget.onModelReady != null) {
4 years ago
widget.onModelReady(model);
} else if (widget.onModelReady != null &&
Provider.of<ProjectViewModel>(context, listen: false).isLogin) {
widget.onModelReady(model);
}
super.initState();
}
4 years ago
getUser() async {
var userData = await sharedPref.getObject(USER_PROFILE);
this.isLogin = userData != null;
}
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider<T>(
create: (BuildContext context) => model,
child: Consumer<T>(builder: widget.builder),
);
}
@override
void dispose() {
4 years ago
if (model != null) {
model = null;
}
super.dispose();
}
}