osam 4.2.1
osam: ^4.2.1 copied to clipboard
State management library inspired redux, bloc and SOLID prenciples. It is null safety and very stable solution to avoid red screens and bad code.
example/lib/main.dart
import 'package:example/presenter.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:osam/osam.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
final store = Store(AppState());
runApp(
StoreProvider(
store: store,
child: PresenterProvider(
key: const ValueKey('main'),
child: MaterialApp(home: MyHomePage()),
presenter: ExamplePresenter(),
),
),
);
}
class MyHomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
final presenter = context.presenter<ExamplePresenter>();
return OsamNavigator<ExamplePresenter>(
key: const ValueKey('mainN'),
initialRoute: MainRoutes.first,
rootNavigator: true,
screens: {
MainRoutes.first: Scaffold(
appBar: AppBar(),
body: Container(
color: Colors.pink,
child: Center(
child: RaisedButton(
child: const Text('to 2'),
onPressed: () {
presenter.push(MainRoutes.second);
},
),
),
),
),
MainRoutes.second: MultiPresenterProvider(
key: const ValueKey('multi'),
providers: [
PresenterProvider(
presenter: TabExamplePresenter(),
key: const ValueKey('tab_p'),
),
PresenterProvider(presenter: ExamplePresenter1(), key: const ValueKey('ex1_p')),
PresenterProvider(presenter: ExamplePresenter2(), key: const ValueKey('ex2_p')),
],
child: TabPage(),
)
},
);
}
}
class TabPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
final presenterTab = context.presenter<TabExamplePresenter>();
final presenter1 = context.presenter<ExamplePresenter1>();
final presenter2 = context.presenter<ExamplePresenter2>();
return OsamTabNavigator<TabExamplePresenter>(
key: const ValueKey('mainTabN'),
tabDecoration: const BoxDecoration(color: Colors.pink),
tabs: {
TabRoutes.first: RaisedButton(
key: const ValueKey('tb1'),
color: Colors.green,
child: const Text('1'),
onPressed: () => presenterTab.changeTab(TabRoutes.first),
),
TabRoutes.second: RaisedButton(
key: const ValueKey('tb2'),
color: Colors.green,
child: const Text('2'),
onPressed: () => presenterTab.changeTab(TabRoutes.second),
),
},
navigators: {
TabRoutes.first: OsamNavigator<ExamplePresenter1>(
key: const ValueKey('sub_n_1'),
initialRoute: MainRoutes.first,
screens: {
MainRoutes.first: Scaffold(
appBar: AppBar(title: const Text('first')),
body: Center(
child: RaisedButton(
child: const Text('to 2'),
onPressed: () {
presenter1.push(MainRoutes.second);
},
),
),
backgroundColor: Colors.cyanAccent,
),
MainRoutes.second: Scaffold(
appBar: AppBar(
title: const Text('second'),
),
body: Center(
child: RaisedButton(
child: const Text('back'),
onPressed: () {
presenter1.pop();
},
),
),
backgroundColor: Colors.cyan,
),
},
),
TabRoutes.second: OsamNavigator<ExamplePresenter2>(
key: const ValueKey('sub_n_2'),
initialRoute: MainRoutes.first,
screens: {
MainRoutes.first: Scaffold(
backgroundColor: Colors.red,
appBar: AppBar(title: const Text('first')),
body: Center(
child: RaisedButton(
child: const Text('to 2'),
onPressed: () {
presenter2.push(MainRoutes.second);
},
),
),
),
MainRoutes.second: Scaffold(
backgroundColor: Colors.redAccent,
appBar: AppBar(title: const Text('second')),
body: Center(
child: RaisedButton(
child: const Text('back'),
onPressed: () {
presenter2.pop();
},
),
),
),
},
),
},
);
}
}
class MainRoutes {
static const first = 'first';
static const second = 'second';
}
class TabRoutes {
static const first = 'first';
static const second = 'second';
}
// ignore: must_be_immutable
class AppState extends BaseState<AppState> {
var mainNavigation = NavigationState(routes: [MainRoutes.first]);
var tabNavigation = TabNavigationState(currentTab: TabRoutes.first);
var mainNavigation1 = NavigationState(routes: [MainRoutes.first]);
var mainNavigation2 = NavigationState(routes: [MainRoutes.first]);
@override
List<Object> get props => [mainNavigation, tabNavigation, mainNavigation1, mainNavigation2];
}