Created
April 8, 2021 15:16
-
-
Save RipplesCode/eb5d651fca01fdfea24fd713fac6fc74 to your computer and use it in GitHub Desktop.
Simple State manager GetBuilder
This file contains hidden or 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:flutter_getx/my_controller.dart'; | |
import 'package:flutter_getx/next_screen.dart'; | |
import 'package:flutter_getx/student.dart'; | |
import 'package:flutter_getx/unknown_route.dart'; | |
import 'package:get/get.dart'; | |
import 'home.dart'; | |
void main() => runApp(MyApp()); | |
class MyApp extends StatelessWidget { | |
// If init property is not used in GetX<Type of Controller> then | |
//create the instance of controller as follows. | |
//MyController myController = Get.put(MyController()); | |
@override | |
Widget build(BuildContext context) { | |
// TODO: implement build | |
return GetMaterialApp( | |
title: "State Management", | |
home: Scaffold( | |
appBar: AppBar(title: Text("State Management")), | |
body: Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
crossAxisAlignment: CrossAxisAlignment.center, | |
children: [ | |
GetBuilder<MyController>( | |
init: MyController(), | |
builder: (controller) { | |
return Text( | |
"The value is ${controller.count}", | |
style: TextStyle(fontSize: 25), | |
); | |
}, | |
), | |
SizedBox( | |
height: 16, | |
), | |
RaisedButton( | |
child: Text("Increment"), | |
onPressed: () { | |
//myController.increment(); | |
// If instance of controller not created at top | |
// Get.find<MyController>().increment(); | |
}, | |
), | |
], | |
), | |
), | |
), | |
); | |
} | |
} |
This file contains hidden or 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_getx/student.dart'; | |
import 'package:get/get.dart'; | |
class MyController extends GetxController | |
{ | |
var count=0; | |
void increment() | |
{ | |
count++; | |
update(); // Will update the count variable on UI which uses it | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment