I'm starting to program now and I'm having trouble changing the value of an observable variable, I was wondering if you could help me as it has no syntax errors but still only takes the value assigned to the initializer
Variable call location
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:get/get_state_manager/src/rx_flutter/rx_obx_widget.dart';
import 'package:project001_app_waiter/config/custom_colors.dart';
import 'package:project001_app_waiter/pages/base/controller/main_base_controller.dart';
import 'package:project001_app_waiter/pages/kitchen/view/kitchen_screen.dart';
import 'package:project001_app_waiter/pages/requests/controller/requests_controller.dart';
import 'package:project001_app_waiter/pages/table/view/table_tab.dart';
import '../deliver/deliver_tab.dart';
class MainBaseScreen extends StatefulWidget {
const MainBaseScreen({Key? key}) : super(key: key);
@override
State<MainBaseScreen> createState() => _MainBaseScreenState();
}
class _MainBaseScreenState extends State<MainBaseScreen> {
int currentIndex = 0;
final pageController = PageController();
final requestController = RequestsController();
final mainBaseController = MainBaseController();
/*Widget _getNotificationIcon() {
if (requestController.filterByKitchen(0).isEmpty) {
return const Icon(Icons.room_service_outlined);
} else {
return Badge(
badgeColor: Colors.red,
badgeContent: const Text(
'!',
style: TextStyle(
color: Colors.white,
fontSize: 10,
),
),
child: const Icon(Icons.room_service_outlined));
}
}*/
@override
Widget build(BuildContext context) {
return Scaffold(
body: PageView(
physics: const NeverScrollableScrollPhysics(),
controller: pageController,
children: [
const TableTab(),
const KitchenScreen(),
const DeliverTab(),
Container(color: Colors.blue),
],
),
// Barra de navegação
bottomNavigationBar: BottomNavigationBar(
currentIndex: currentIndex,
// A seleção das 'paginas/icones' 0,1,2,3
onTap: (index) {
setState(() {
currentIndex = index;
pageController.animateToPage(
index,
duration: const Duration(milliseconds: 800),
curve: Curves.ease,
);
});
},
type: BottomNavigationBarType.fixed,
backgroundColor: CustomColors.customSwatchColor,
selectedItemColor: Colors.white,
unselectedItemColor: Colors.white.withAlpha(100),
items: [
const BottomNavigationBarItem(
icon: Icon(Icons.table_bar_outlined),
label: 'Mesas',
),
const BottomNavigationBarItem(
icon: Icon(Icons.kitchen_outlined),
label: 'Cozinha',
),
BottomNavigationBarItem(
icon: Obx((){
return mainBaseController.notification.value;
}),
label: 'Pedidos',
),
const BottomNavigationBarItem(
icon: Icon(Icons.person_outline),
label: 'Perfil',
),
],
),
);
}
}
I'm putting a text as initial return to make it clear that it's not returning from the widget
Controller
import 'package:badges/badges.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../../requests/controller/requests_controller.dart';
class MainBaseController extends GetxController {
final requestController = RequestsController();
Rx<Widget> notification = const Text('Não').obs;
Widget _getNotificationIcon() {
if (requestController.filterByKitchen(0).isEmpty) {
return const Icon(Icons.room_service_outlined);
} else {
return Badge(
badgeColor: Colors.red,
badgeContent: const Text(
'!',
style: TextStyle(
color: Colors.white,
fontSize: 10,
),
),
child: const Icon(Icons.room_service_outlined));
}
}
Widget iconNot() => notification.value = _getNotificationIcon();
}
CodePudding user response:
class Controller extends GetxController{
var count = 0.obs;
increment() => count ;
}
try with only value instead of widget
CodePudding user response:
First of all, your MainBaseScreen can be a Stateless widget, even better, it can be a GetView<MainBaseController>
so you can access the MainBaseController using controller
variable.
In the MainBaseController:
RxString notification = const 'Não'.obs;
In the MainBaseScreen:
BottomNavigationBarItem(
icon: Obx(() => Text(controller.notification.value),
label: 'Pedidos',
),