Created
May 7, 2025 18:58
-
-
Save slightfoot/a910d472a123e315d9f7af08018ec5b7 to your computer and use it in GitHub Desktop.
MVPVMS pattern - by Simon Lightfoot :: #HumpdayQandA on 7th May 2025 :: https://www.youtube.com/watch?v=CqmhwUnesvQ
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
// MIT License | |
// | |
// Copyright (c) 2025 Simon Lightfoot | |
// | |
// Permission is hereby granted, free of charge, to any person obtaining a copy | |
// of this software and associated documentation files (the "Software"), to deal | |
// in the Software without restriction, including without limitation the rights | |
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
// copies of the Software, and to permit persons to whom the Software is | |
// furnished to do so, subject to the following conditions: | |
// | |
// The above copyright notice and this permission notice shall be included in all | |
// copies or substantial portions of the Software. | |
// | |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
// SOFTWARE. | |
// | |
import 'package:flutter/material.dart'; | |
void main() { | |
runApp(App()); | |
} | |
class App extends StatefulWidget { | |
const App({super.key}); | |
static HomeRepository homeRepoOf(BuildContext context) { | |
return context.findRootAncestorStateOfType<_AppState>()!.homeRepository; | |
} | |
@override | |
State<App> createState() => _AppState(); | |
} | |
class _AppState extends State<App> { | |
late HomeRepository homeRepository; | |
@override | |
void initState() { | |
super.initState(); | |
homeRepository = HomeRepository(); | |
} | |
ThemeData createTheme(Color seedColor) { | |
final data = ThemeData.from( | |
colorScheme: ColorScheme.fromSeed(seedColor: seedColor), | |
useMaterial3: false, | |
); | |
return data.copyWith( | |
bottomNavigationBarTheme: data.bottomNavigationBarTheme.copyWith( | |
backgroundColor: seedColor, | |
unselectedItemColor: Colors.white, | |
selectedItemColor: Colors.blue.shade200, | |
type: BottomNavigationBarType.shifting, | |
), | |
); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
debugShowCheckedModeBanner: false, | |
theme: createTheme(Colors.deepPurple.shade600), | |
home: Home(), | |
); | |
} | |
} | |
class HomeRepository { | |
const HomeRepository(); | |
Future<List<String>> loadHomeData() async { | |
// Example loading delay | |
await Future.delayed(const Duration(seconds: 3)); | |
return [ | |
for (int index = 0; index < 1000; index++) // | |
'Home Item #$index', | |
]; | |
} | |
} | |
class HomePresenter extends ChangeNotifier { | |
HomePresenter(); | |
var _viewModel = HomeViewModel.initial; | |
HomeViewModel get viewModel => _viewModel; | |
void load(HomeRepository homeRepo) async { | |
final data = await homeRepo.loadHomeData(); | |
_viewModel = _viewModel.copyWith( | |
title: 'Home', | |
isLoading: false, | |
items: data, | |
); | |
notifyListeners(); | |
} | |
void adminLogin() { | |
_viewModel = _viewModel.copyWith(isAdmin: true); | |
notifyListeners(); | |
} | |
void updateSortOrder(HomeSortOrder order) { | |
final compare = switch (order) { | |
HomeSortOrder.ascending => (String a, String b) => a.compareTo(b), | |
HomeSortOrder.descending => (String a, String b) => b.compareTo(a), | |
}; | |
_viewModel = _viewModel.copyWith( | |
items: List.of(_viewModel.items)..sort(compare), | |
); | |
notifyListeners(); | |
} | |
void updateNavigation(int index) { | |
_viewModel = _viewModel.copyWith(navIndex: index); | |
notifyListeners(); | |
} | |
} | |
abstract class ViewModel { | |
const ViewModel(); | |
} | |
class HomeViewModel extends ViewModel { | |
const HomeViewModel({ | |
required this.title, | |
required this.isAdmin, | |
required this.navIndex, | |
required this.isLoading, | |
required this.items, | |
}); | |
static const initial = HomeViewModel( | |
title: 'Loading', | |
isAdmin: false, | |
navIndex: 0, | |
isLoading: true, | |
items: [], | |
); | |
final String title; | |
final bool isAdmin; | |
final int navIndex; | |
final bool isLoading; | |
final List<String> items; | |
HomeViewModel copyWith({ | |
String? title, | |
bool? isAdmin, | |
int? navIndex, | |
bool? isLoading, | |
List<String>? items, | |
}) { | |
return HomeViewModel( | |
title: title ?? this.title, | |
isAdmin: isAdmin ?? this.isAdmin, | |
navIndex: navIndex ?? this.navIndex, | |
isLoading: isLoading ?? this.isLoading, | |
items: items ?? this.items, | |
); | |
} | |
} | |
class Home extends StatefulWidget { | |
const Home({super.key}); | |
@override | |
State<Home> createState() => _HomeState(); | |
} | |
class _HomeState extends State<Home> { | |
final presenter = HomePresenter(); | |
@override | |
void initState() { | |
super.initState(); | |
presenter.load(App.homeRepoOf(context)); | |
} | |
@override | |
void dispose() { | |
presenter.dispose(); | |
super.dispose(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return ListenableBuilder( | |
listenable: presenter, | |
builder: (BuildContext context, Widget? child) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text(presenter.viewModel.title), | |
actions: [ | |
HomeOverflowMenu( | |
onLoginPressed: presenter.adminLogin, | |
onChanged: presenter.updateSortOrder, | |
), | |
], | |
), | |
body: presenter.viewModel.isLoading | |
? Center( | |
child: CircularProgressIndicator.adaptive(), | |
) | |
: ListView.builder( | |
itemCount: presenter.viewModel.items.length, | |
itemBuilder: (BuildContext context, int index) { | |
final item = presenter.viewModel.items[index]; | |
return ListTile(title: Text(item)); | |
}, | |
), | |
bottomNavigationBar: BottomNavigationBar( | |
type: BottomNavigationBarType.fixed, | |
currentIndex: presenter.viewModel.navIndex, | |
onTap: presenter.updateNavigation, | |
items: [ | |
BottomNavigationBarItem( | |
icon: Icon(Icons.home), | |
label: 'Home', | |
), | |
BottomNavigationBarItem( | |
icon: Icon(Icons.feed), | |
label: 'Feed', | |
), | |
BottomNavigationBarItem( | |
icon: Icon(Icons.person), | |
label: 'Profile', | |
), | |
if (presenter.viewModel.isAdmin) // | |
BottomNavigationBarItem( | |
icon: Icon(Icons.lock_person), | |
label: 'Admin', | |
), | |
], | |
), | |
); | |
}, | |
); | |
} | |
} | |
enum HomeSortOrder { | |
ascending, | |
descending, | |
} | |
class HomeOverflowMenu extends StatelessWidget { | |
const HomeOverflowMenu({ | |
super.key, | |
required this.onLoginPressed, | |
required this.onChanged, | |
}); | |
final VoidCallback onLoginPressed; | |
final ValueChanged<HomeSortOrder> onChanged; | |
@override | |
Widget build(BuildContext context) { | |
return Row( | |
children: [ | |
IconButton( | |
onPressed: onLoginPressed, | |
icon: Icon(Icons.login), | |
), | |
MenuAnchor( | |
menuChildren: [ | |
MenuItemButton( | |
onPressed: () => onChanged(HomeSortOrder.ascending), | |
leadingIcon: Icon(Icons.sort_by_alpha), | |
child: Text('Ascending'), | |
), | |
MenuItemButton( | |
onPressed: () => onChanged(HomeSortOrder.descending), | |
leadingIcon: Icon(Icons.sort_by_alpha), | |
child: Text('Descending'), | |
), | |
], | |
builder: (BuildContext context, MenuController controller, Widget? child) { | |
return IconButton( | |
onPressed: () => controller.open(), | |
icon: Icon(Icons.sort), | |
); | |
}, | |
), | |
], | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment