Skip to content

Instantly share code, notes, and snippets.

@rodrigoespinozadev
Forked from RipplesCode/count_controller.dart
Created December 22, 2022 18:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rodrigoespinozadev/017915359ff930fb77aad57a5b742b9d to your computer and use it in GitHub Desktop.
Save rodrigoespinozadev/017915359ff930fb77aad57a5b742b9d to your computer and use it in GitHub Desktop.
GetView and GetWidget
import 'package:get/get.dart';
class CountController extends GetxController
{
var count=0.obs;
void increment()
{
count++;
}
}
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