Facebook
From Eduardo Florence, 1 Week ago, written in Dart.
Embed
Download Paste or View Raw
Hits: 24
  1. import 'package:flutter/material.dart';
  2. import 'package:get/get.dart';
  3.  
  4. void main() {
  5.   Get.lazyPut(() => Page2Controller(), fenix: true);
  6.   runApp(GetMaterialApp(home: HomePage()));
  7. }
  8.  
  9. class HomePage extends StatelessWidget {
  10.   @override
  11.   Widget build(BuildContext context) {
  12.     return Scaffold(
  13.       appBar: AppBar(title: Text('HOME')),
  14.       body: Center(
  15.         child: RaisedButton(
  16.           child: Text('Go to page 2'),
  17.           onPressed: () => Get.to(() => Page2()),
  18.         ),
  19.       ),
  20.     );
  21.   }
  22. }
  23.  
  24. class Page2 extends StatelessWidget {
  25.   final controller = Get.find<Page2Controller>();
  26.   @override
  27.   Widget build(BuildContext context) {
  28.     return Scaffold(
  29.       appBar: AppBar(title: Text('Page2')),
  30.       body: Center(
  31.         child: Obx(() => Text(controller.number.toString())),
  32.       ),
  33.       floatingActionButton: FloatingActionButton(
  34.         onPressed: () => controller.increment(),
  35.         child: Icon(Icons.add),
  36.       ),
  37.     );
  38.   }
  39. }
  40.  
  41. class Page2Controller extends GetxController {
  42.   RxInt number = RxInt(0);
  43.  
  44.   increment() => number++;
  45. }
  46.