Skip to content

Instantly share code, notes, and snippets.

@federicofazzeri
Last active December 5, 2019 18:47
Show Gist options
  • Save federicofazzeri/bed215cb90ffe0f7777e9f2c1245ce15 to your computer and use it in GitHub Desktop.
Save federicofazzeri/bed215cb90ffe0f7777e9f2c1245ce15 to your computer and use it in GitHub Desktop.
Flutter reactive UI without class based widgets (...but don't do this)
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