Skip to content

Instantly share code, notes, and snippets.

@RipplesCode
Created April 8, 2021 15:15
Show Gist options
  • Save RipplesCode/3c6def44fa249fb6cdc8343937613696 to your computer and use it in GitHub Desktop.
Save RipplesCode/3c6def44fa249fb6cdc8343937613696 to your computer and use it in GitHub Desktop.
Seprating UI and business logic GetXController(Introducing GextController)
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 {
// Create the instance of Controller
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: [
Obx(() => Text(
"Name is ${myController.student.name}",
style: TextStyle(fontSize: 25),
)),
SizedBox(
height: 16,
),
RaisedButton(
child: Text("Upper"),
onPressed: () {
//If individual variables are observable
myController.convertToUpperCase();
// If entire class is observable
// myController.convertUpperCase();
},
),
],
),
),
),
);
}
}
import 'package:flutter_getx/student.dart';
import 'package:get/get.dart';
class MyController extends GetxController
{
var student =Student();
void convertToUpperCase()
{
student.name.value=student.name.value.toUpperCase();
}
// var student=Student(name: "Tom",age: 25).obs;
//
// void convertUpperCase()
// {
// student.update((student) {
// student.name=student.name.toString().toUpperCase();
// });
// }
}
import 'package:get/get.dart';
class Student {
/* Classes can be made observable by making individuals variables Rx
or by making the entire class observable.
*/
// Individuals variables Rx
var name = "Tom".obs;
var age = 25.obs;
// To make the entire class observable
// var name;
// var age;
// Student({this.name, this.age});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment