Created
April 11, 2022 05:50
-
-
Save aadomin/c086c3edf3b5ab7ff0842434594d0754 to your computer and use it in GitHub Desktop.
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'; | |
| import 'package:provider/provider.dart'; | |
| void main() { | |
| runApp(MyApp()); | |
| } | |
| class MyApp extends StatelessWidget { | |
| @override | |
| Widget build(BuildContext context) { | |
| return MaterialApp( | |
| theme: ThemeData( | |
| primarySwatch: Colors.blue, | |
| inputDecorationTheme: | |
| const InputDecorationTheme(border: OutlineInputBorder()), | |
| ), | |
| home: createScreenExampleScreen(), | |
| ); | |
| } | |
| } | |
| ////////////////////////////////////////////////////////////////////////////// | |
| ////////////////////////////////////////////////////////////////////////////// | |
| ////////////////////////////////////////////////////////////////////////////// | |
| Widget createScreenExampleScreen() { | |
| return ChangeNotifierProvider( | |
| create: (context) => ExampleScreenVM(context: context), | |
| child: Builder( | |
| builder: (context) => ExampleScreen( | |
| viewModel: context.watch<ExampleScreenVM>(), | |
| ), | |
| ), | |
| ); | |
| } | |
| class ExampleScreen extends StatefulWidget { | |
| const ExampleScreen({ | |
| required this.viewModel, | |
| Key? key, | |
| }) : super(key: key); | |
| final ExampleScreenVM viewModel; | |
| @override | |
| State<ExampleScreen> createState() => _ExampleScreenState(); | |
| } | |
| class _ExampleScreenState extends State<ExampleScreen> { | |
| late final ___viewModel = widget.viewModel; | |
| @override | |
| void initState() { | |
| super.initState(); | |
| ___viewModel.initVM(); | |
| } | |
| @override | |
| void dispose() { | |
| ___viewModel.disposeVM(); | |
| super.dispose(); | |
| } | |
| @override | |
| Widget build(BuildContext context) { | |
| return Scaffold( | |
| body: Text(___viewModel.i.toString()), | |
| floatingActionButton: FloatingActionButton( | |
| onPressed: () => ___viewModel.inc(), | |
| ), | |
| ); | |
| } | |
| } | |
| class ExampleScreenVM with ChangeNotifier { | |
| ExampleScreenVM({required this.context}); | |
| BuildContext context; | |
| int i = 0; | |
| void inc() { | |
| i++; | |
| notifyListeners(); | |
| } | |
| void initVM() {} | |
| void disposeVM() {} | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment