Forked from RipplesCode/count_controller.dart
Created
December 22, 2022 18:04
Star
You must be signed in to star a gist
GetView and GetWidget
This file contains 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:get/get.dart'; | |
class CountController extends GetxController | |
{ | |
var count=0.obs; | |
void increment() | |
{ | |
count++; | |
} | |
} |
This file contains 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:get/get.dart'; | |
import 'package:getx_getview/count_controller.dart'; | |
void main() { | |
runApp(MyApp()); | |
} | |
//GetView | |
// ========== | |
// If we have single controller as a dependency then we can use GetView | |
//instead of Statelesswidget and avoid writing Get.find | |
//GetWidget | |
// ========== | |
//It is similar to GetView with one difference it gives the same instance of | |
//Get.find everytime. It becomes very useful when used in combination with | |
//Get.create | |
class MyApp extends GetView<CountController> { | |
@override | |
Widget build(BuildContext context) { | |
Get.put(CountController()); | |
//Get.create(() => CountController()); | |
return GetMaterialApp( | |
title: 'GetView', | |
home: Scaffold( | |
appBar: AppBar( | |
title: Text('GetView'), | |
), | |
body: Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
crossAxisAlignment: CrossAxisAlignment.center, | |
children: [ | |
Obx( | |
() => Text( | |
'The value is ${controller.count}', | |
style: TextStyle(fontSize: 25), | |
), | |
), | |
SizedBox( | |
height: 8, | |
), | |
RaisedButton( | |
child: Text('Increment'), | |
onPressed: () { | |
print(controller.hashCode); | |
controller.increment(); | |
}, | |
), | |
], | |
), | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment