Dman_14

Creating a view & view controller

Jan 10th, 2021 (edited)
1,193
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import 'package:flutter/material.dart';
  2. import 'package:get_it/get_it.dart';
  3. import 'package:is_web_enabled/base_view.dart';
  4.  
  5. GetIt locator = GetIt.instance;
  6.  
  7. void setupLocator() {
  8.   locator.registerFactory(() => MyHomePageController()); // This line would be placed in locator.dart
  9. }
  10.  
  11. void main() {
  12.   setupLocator();
  13.   runApp(MyApp());
  14. }
  15.  
  16. class MyApp extends StatelessWidget {
  17.   @override
  18.   Widget build(BuildContext context) {
  19.     return MaterialApp(
  20.       title: 'Flutter Demo',
  21.       theme: ThemeData(
  22.         primarySwatch: Colors.blue,
  23.         visualDensity: VisualDensity.adaptivePlatformDensity,
  24.       ),
  25.       home: MyHomePage(title: 'Flutter Demo Home Page'),
  26.     );
  27.   }
  28. }
  29.  
  30. class MyHomePage extends StatelessWidget {
  31.   final String title;
  32.  
  33.   MyHomePage({this.title});
  34.  
  35.   @override
  36.   Widget build(BuildContext context) {
  37.     return BaseView<MyHomePageController>(
  38.       onControllerReady: (controller) {
  39.         controller.counter = 10;
  40.       },
  41.       builder: (context, controller, child) {
  42.         return Scaffold(
  43.           appBar: AppBar(
  44.             title: Text(title),
  45.           ),
  46.           body: Center(
  47.             child: Column(
  48.               mainAxisAlignment: MainAxisAlignment.center,
  49.               children: <Widget>[
  50.                 Text(
  51.                   'You have pushed the button this many times:',
  52.                 ),
  53.                 Text(
  54.                   '${controller.counter}',
  55.                   style: Theme.of(context).textTheme.headline4,
  56.                 ),
  57.               ],
  58.             ),
  59.           ),
  60.           floatingActionButton: FloatingActionButton(
  61.             onPressed: controller.onIncrementFabPressed,
  62.             tooltip: 'Increment',
  63.             child: Icon(Icons.add),
  64.           ),
  65.         );
  66.       },
  67.     );
  68.   }
  69. }
  70.  
  71. class MyHomePageController extends ChangeNotifier {
  72.   int _counter;
  73.   int get counter => _counter;
  74.   set counter(int value) {
  75.     if (_counter == value) return;
  76.     _counter = value;
  77.     notifyListeners();
  78.   }
  79.  
  80.   onIncrementFabPressed() {
  81.     counter++;
  82.   }
  83. }
  84.  
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×