mvp_flutter 0.0.1 mvp_flutter: ^0.0.1 copied to clipboard
A framework that easily implements the MVP(Model-View-Presenter) architecture in Flutter
import 'package:flutter/material.dart';
import 'package:mvp_flutter/mvp_flutter.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
home: const MyHomePage(title: 'Flutter Demo with mvp'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> implements BaseView {
late final _presenter = CounterPresenter(this);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'You have pushed the button this many times:',
),
Text(
'${_presenter.counter}',
style: Theme.of(context).textTheme.headlineMedium,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _presenter.incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}
class CounterModel {
const CounterModel(this.counter);
final int counter;
CounterModel increase() => CounterModel(counter + 1);
}
class CounterPresenter {
CounterPresenter(this._view);
CounterModel get _model => _delegate.model;
final BaseView _view;
final _delegate = PresenterDelegate<CounterModel>(const CounterModel(0));
// public property and method
int get counter => _model.counter;
void incrementCounter() => _delegate.refresh(_view, _model.increase());
}