Last active
December 5, 2019 18:47
-
-
Save federicofazzeri/bed215cb90ffe0f7777e9f2c1245ce15 to your computer and use it in GitHub Desktop.
Flutter reactive UI without class based widgets (...but don't do this)
This file contains hidden or 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'; | |
final Color darkBlue = Color.fromARGB(255, 18, 32, 47); | |
void main() { | |
MyAppModel myAppModel = MyAppModel(); | |
myAppModel.addListener(() => runApp(myApp(myAppModel))); | |
myAppModel.updateUI(1); | |
} | |
Widget myApp(myAppModel) { | |
return MaterialApp( | |
theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: darkBlue), | |
debugShowCheckedModeBanner: false, | |
home: Scaffold( | |
body: Center( | |
child: myWidget(myAppModel), | |
), | |
), | |
); | |
} | |
Widget myWidget(myAppModel) { | |
return RaisedButton( | |
onPressed: () { | |
myAppModel.updateUI(3); | |
}, | |
child: Text(myAppModel.n.toString(), style: TextStyle(fontSize: 20)), | |
); | |
} | |
class MyAppModel with ChangeNotifier { | |
int _n = 1; | |
updateUI(int newN) { | |
_n = newN; | |
notifyListeners(); | |
} | |
int get n => _n; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment