Last active
November 1, 2018 18:52
-
-
Save Andrious/983f685b1cddf4090a087c332e6962b5 to your computer and use it in GitHub Desktop.
The App has the View. First Approach to implement the MVC Library Package, mvc_pattern.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:mvc_pattern/mvc_pattern.dart'; | |
/// Notice 'the rest of the app' has no idea this Dart file even exists. | |
import 'Model.dart'; | |
class Controller extends ControllerMVC { | |
Controller() { | |
con = this; | |
} | |
static Controller con; | |
@override | |
initState() { | |
/// Demonstrating how the 'initState()' is easily implemented. | |
_counter = Model.counter; | |
} | |
int get displayThis => _counter; | |
int _counter; | |
void whatever() { | |
/// The Controller knows how to 'talk to' the Model. It knows the name, but Model does the work. | |
_counter = Model.incrementCounter(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'MyHomePage.dart'; | |
/// Dependency Injection of 'the Controller' in the design pattern. | |
import 'Controller.dart'; | |
void main() => runApp(MVCCounterApp()); | |
class MVCCounterApp extends AppMVC { | |
MVCCounterApp():super(con: Controller()); | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: MyHomePage(), | |
); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Model { | |
static int get counter => _counter; | |
static int _counter = 0; | |
static int incrementCounter() { | |
return ++_counter; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
//import 'package:mvc_pattern/mvc_pattern.dart'; | |
/// Dependency Injection of 'the Controller' in the design pattern. | |
import 'Controller.dart'; | |
class MyHomePage extends StatefulWidget { | |
MyHomePage({Key key}) : super(key: key); | |
// Fields in a Widget subclass are always marked "final". | |
static final String title = 'Flutter Demo Home Page'; | |
@override | |
_MyHomePageState createState() => new _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> { | |
Controller _con = Controller.con; | |
@override | |
@protected | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text(MyHomePage.title), | |
), | |
body: Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
Text( | |
MyHomePage.title, | |
), | |
Text( | |
'${_con.displayThis}', | |
style: Theme.of(context).textTheme.display1, | |
), | |
], | |
), | |
), | |
floatingActionButton: FloatingActionButton( | |
onPressed: () { | |
setState(_con.whatever); | |
}, | |
tooltip: 'Increment', | |
child: Icon(Icons.add), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment