Skip to content

Instantly share code, notes, and snippets.

@Andrious
Last active November 8, 2018 06:51
Show Gist options
  • Save Andrious/ffb953a1e7d4978acaedbd7fd24647f7 to your computer and use it in GitHub Desktop.
Save Andrious/ffb953a1e7d4978acaedbd7fd24647f7 to your computer and use it in GitHub Desktop.
Dart Files for the MVCCounterApp. The Counter App using the mvc_pattern plugin.
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 Con extends ControllerMVC {
factory Con(){
if(_this == null) _this = Con._();
return _this;
}
static Con _this;
Con._();
/// Allow for easy access to 'the Controller' throughout the application.
static Con get con => _this;
@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();
}
}
class Model{
static int get counter => _counter;
static int _counter = 0;
static int incrementCounter(){
return ++_counter;
}
}
import 'package:flutter/material.dart';
/// Notice this Dart file is a little separated from the rest of the app.
/// There is 'no hint' that a MVC design pattern will be utilized in this app.
import 'MyHomePage.dart';
void main() => runApp(MVCCounterApp());
class MVCCounterApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or press Run > Flutter Hot Reload in IntelliJ). Notice that the
// counter didn't reset back to zero; the application is not restarted.
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
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 StateMVC {
_MyHomePageState():super(Controller()){
_con = Controller.con;
}
Controller _con;
@override
Widget build(BuildContext context) {
// This method is rerun every time setState is called.
//
// The Flutter framework has been optimized to make rerunning build methods
// fast, so that you can just rebuild anything that needs updating rather
// than having to individually change instances of widgets.
return Scaffold(
appBar: AppBar(
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text(MyHomePage.title),
),
body: Center(
// Center is a layout widget. It takes a single child and positions it
// in the middle of the parent.
child: Column(
// Column is also layout widget. It takes a list of children and
// arranges them vertically. By default, it sizes itself to fit its
// children horizontally, and tries to be as tall as its parent.
//
// Invoke "debug paint" (press "p" in the console where you ran
// "flutter run", or select "Toggle Debug Paint" from the Flutter tool
// window in IntelliJ) to see the wireframe for each widget.
//
// Column has various properties to control how it sizes itself and
// how it positions its children. Here we use mainAxisAlignment to
// center the children vertically; the main axis here is the vertical
// axis because Columns are vertical (the cross axis would be
// horizontal).
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),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment