Skip to content

Instantly share code, notes, and snippets.

@evaisse
Last active September 30, 2025 11:56
Show Gist options
  • Select an option

  • Save evaisse/e97d31c9f15daa6a53178d597617bcc9 to your computer and use it in GitHub Desktop.

Select an option

Save evaisse/e97d31c9f15daa6a53178d597617bcc9 to your computer and use it in GitHub Desktop.
simple way to make a screen controller/view using provider and without fullstate widgets
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
class MyBudgetModel {
final Set<String> activeCategories;
MyBudgetModel(this.activeCategories);
}
class MyBudgetController extends ChangeNotifier {
final viewModel = MyBudgetModel({"foo", "bar"});
MyBudgetController();
void toggleCategory(String cat) {
final a = viewModel.activeCategories;
a.contains(cat) ? a.remove(cat) : a.add(cat);
notifyListeners();
}
}
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: ChangeNotifierProvider<MyBudgetController>(
create: (context) => MyBudgetController(),
builder: (context, _) {
final ctrl = context.watch<MyBudgetController>();
return Scaffold(
body: Center(child: MyBudgetWidget(ctrl.viewModel)),
floatingActionButton: FloatingActionButton(
onPressed: () =>
context.read<MyBudgetController?>()?.toggleCategory('bar'),
child: const Icon(Icons.beach_access),
),
);
},
),
);
}
}
class MyBudgetWidget extends StatelessWidget {
final MyBudgetModel model;
final MyBudgetController? controller;
MyBudgetWidget(this.model, {this.controller});
@override
Widget build(BuildContext context) {
return Container(child: Text(model.activeCategories.join(' - ')));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment