Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'package:flutter/material.dart';
- import 'package:get_it/get_it.dart';
- import 'package:is_web_enabled/base_view.dart';
- GetIt locator = GetIt.instance;
- void setupLocator() {
- locator.registerFactory(() => MyHomePageController()); // This line would be placed in locator.dart
- }
- void main() {
- setupLocator();
- runApp(MyApp());
- }
- class MyApp extends StatelessWidget {
- @override
- Widget build(BuildContext context) {
- return MaterialApp(
- title: 'Flutter Demo',
- theme: ThemeData(
- primarySwatch: Colors.blue,
- visualDensity: VisualDensity.adaptivePlatformDensity,
- ),
- home: MyHomePage(title: 'Flutter Demo Home Page'),
- );
- }
- }
- class MyHomePage extends StatelessWidget {
- final String title;
- MyHomePage({this.title});
- @override
- Widget build(BuildContext context) {
- return BaseView<MyHomePageController>(
- onControllerReady: (controller) {
- controller.counter = 10;
- },
- builder: (context, controller, child) {
- return Scaffold(
- appBar: AppBar(
- title: Text(title),
- ),
- body: Center(
- child: Column(
- mainAxisAlignment: MainAxisAlignment.center,
- children: <Widget>[
- Text(
- 'You have pushed the button this many times:',
- ),
- Text(
- '${controller.counter}',
- style: Theme.of(context).textTheme.headline4,
- ),
- ],
- ),
- ),
- floatingActionButton: FloatingActionButton(
- onPressed: controller.onIncrementFabPressed,
- tooltip: 'Increment',
- child: Icon(Icons.add),
- ),
- );
- },
- );
- }
- }
- class MyHomePageController extends ChangeNotifier {
- int _counter;
- int get counter => _counter;
- set counter(int value) {
- if (_counter == value) return;
- _counter = value;
- notifyListeners();
- }
- onIncrementFabPressed() {
- counter++;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement