如何在切换屏幕时在底部导航屏幕中更改Appbar标题?

发布于 2025-02-11 11:54:03 字数 3153 浏览 1 评论 0原文

我创建了一个底部的导航栏屏幕,并且想在屏幕从导航屏幕选项卡上更改时更改Appbar标题。在这里,我在其中创建了一个开关案例,以更改Appbar标题。但这会引发错误:发生了异常。 _casterror(类型'String'不是类型铸件中类型'rxString'的子类型)。我还尝试将“作为rxstring”删除,然后将Appbartitle更改为字符串。

导航屏幕

import 'package:flutter/material.dart';

import 'package:get/get.dart';
import 'package:onyx/app/modules/location/views/location_view.dart';

import 'package:onyx/app/modules/offers/views/offers_view.dart';
import 'package:onyx/app/modules/setting/views/setting_view.dart';

import 'package:onyx/custom_widgets/drawer.dart';
import 'package:onyx/style/style.dart';

import '../controllers/home_controller.dart';

class HomeView extends GetView<HomeController> {
  
  final screens = [
    const OffersView(
      isAppBar: false,
    ),
    const OffersView(
      isAppBar: false,
    ),
    LocationView(),
    SettingView()
  ];
  HomeView({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
          title: Obx(() => Text(
                controller.appBartitle.value,
                style: const TextStyle(color: Colors.black),
              ))),
      body: Obx(
        () => IndexedStack(
            index: controller.selectedtIndex.value, children: screens),
      ),
      bottomNavigationBar: Obx(
        () => BottomNavigationBar(
          type: BottomNavigationBarType.fixed,
          showSelectedLabels: true,
          backgroundColor: colorPrimary,
          selectedItemColor: Colors.white,
          unselectedItemColor: Colors.white54,
          currentIndex: controller.selectedtIndex.value,
          onTap: (index) => controller.onTapNavigationBar(index),
          items: const [
            BottomNavigationBarItem(
                icon: Icon(Icons.home_outlined), label: "Home"),
            BottomNavigationBarItem(
                icon: Icon(Icons.local_offer_outlined), label: "Offers"),
            BottomNavigationBarItem(
                icon: Icon(Icons.location_on_outlined), label: "Location"),
            BottomNavigationBarItem(
                icon: Icon(Icons.settings_outlined), label: "Settings"),
          ],
        ),
      ),
      drawer: const CDrawer(),
    );
  }
}

控制器

import 'package:get/get.dart';
import 'package:onyx/app/modules/home/views/home_view.dart';

class HomeController extends GetxController {
  var selectedtIndex = 0.obs;
  var appBartitle = "1t".obs;

  @override
  void onInit() {
    super.onInit();
  }

  @override
  void onReady() {
    super.onReady();
  }

  @override
  void onClose() {
    super.onClose();
  }

  onTapNavigationBar(index) {
    selectedtIndex.value = index;
    switch (index) {
      case 0:
        {
          appBartitle = '1t' as RxString;
        }
        break;
      case 1:
        {
          appBartitle = '2t' as RxString;
        }
        break;
      case 2:
        {
          appBartitle = '3t' as RxString;
        }
        break;
      case 3:
        {
          appBartitle = '4t' as RxString;
        }
        break;
    }
  }
}

I Have created a Bottom NavigationBar Screen and i want change the AppBar title as the screen changes from navigation screen tab. Here i have created method in it a switch case to change the AppBar title. but it throws an error : Exception has occurred.
_CastError (type 'String' is not a subtype of type 'RxString' in type cast). i also tried removing cast 'as RxString' and changing the appBartitle to String.

Navigation Screen

import 'package:flutter/material.dart';

import 'package:get/get.dart';
import 'package:onyx/app/modules/location/views/location_view.dart';

import 'package:onyx/app/modules/offers/views/offers_view.dart';
import 'package:onyx/app/modules/setting/views/setting_view.dart';

import 'package:onyx/custom_widgets/drawer.dart';
import 'package:onyx/style/style.dart';

import '../controllers/home_controller.dart';

class HomeView extends GetView<HomeController> {
  
  final screens = [
    const OffersView(
      isAppBar: false,
    ),
    const OffersView(
      isAppBar: false,
    ),
    LocationView(),
    SettingView()
  ];
  HomeView({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
          title: Obx(() => Text(
                controller.appBartitle.value,
                style: const TextStyle(color: Colors.black),
              ))),
      body: Obx(
        () => IndexedStack(
            index: controller.selectedtIndex.value, children: screens),
      ),
      bottomNavigationBar: Obx(
        () => BottomNavigationBar(
          type: BottomNavigationBarType.fixed,
          showSelectedLabels: true,
          backgroundColor: colorPrimary,
          selectedItemColor: Colors.white,
          unselectedItemColor: Colors.white54,
          currentIndex: controller.selectedtIndex.value,
          onTap: (index) => controller.onTapNavigationBar(index),
          items: const [
            BottomNavigationBarItem(
                icon: Icon(Icons.home_outlined), label: "Home"),
            BottomNavigationBarItem(
                icon: Icon(Icons.local_offer_outlined), label: "Offers"),
            BottomNavigationBarItem(
                icon: Icon(Icons.location_on_outlined), label: "Location"),
            BottomNavigationBarItem(
                icon: Icon(Icons.settings_outlined), label: "Settings"),
          ],
        ),
      ),
      drawer: const CDrawer(),
    );
  }
}

Controller

import 'package:get/get.dart';
import 'package:onyx/app/modules/home/views/home_view.dart';

class HomeController extends GetxController {
  var selectedtIndex = 0.obs;
  var appBartitle = "1t".obs;

  @override
  void onInit() {
    super.onInit();
  }

  @override
  void onReady() {
    super.onReady();
  }

  @override
  void onClose() {
    super.onClose();
  }

  onTapNavigationBar(index) {
    selectedtIndex.value = index;
    switch (index) {
      case 0:
        {
          appBartitle = '1t' as RxString;
        }
        break;
      case 1:
        {
          appBartitle = '2t' as RxString;
        }
        break;
      case 2:
        {
          appBartitle = '3t' as RxString;
        }
        break;
      case 3:
        {
          appBartitle = '4t' as RxString;
        }
        break;
    }
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

任性一次 2025-02-18 11:54:03

在OnTapNavigationbar方法中,您仅更改了页面索引,而不更新该方法上的标题
Controller.OntabChanged(索引)将解决您的问题。

In onTapNavigationBar method you have only changed page index and not update title on that method so just replace controller.onTapNavigationBar(index) with
controller.onTabChanged(index) it will solve your issue.

悲凉≈ 2025-02-18 11:54:03

您的主页课很好。 Homecontroller类需要一些更改。
我已经纠正了,并且按预期工作正常。代码在下面给出。

class HomeController extends GetxController {
  var selectedtIndex = 0.obs;
  var appBartitle = "".obs;

  @override
  void onInit() {
    super.onInit();
  }

  onTapNavigationBar(int index) {
    selectedtIndex.value = index;
    switch (index) {
      case 0:
        appBartitle.value = '1t';
        break;
      case 1:
        appBartitle.value = '2t';
        break;
      case 2:
        appBartitle.value = '3t';
        break;
      case 3:
        appBartitle.value = '4t';
        break;
    }
  }

  void onTabChanged(int index) {
    selectedtIndex.value = index;
  }
}

Your HomeView class is fine. There's need some changes in HomeController class.
I have corrected and it working fine as expected. The code is given below.

class HomeController extends GetxController {
  var selectedtIndex = 0.obs;
  var appBartitle = "".obs;

  @override
  void onInit() {
    super.onInit();
  }

  onTapNavigationBar(int index) {
    selectedtIndex.value = index;
    switch (index) {
      case 0:
        appBartitle.value = '1t';
        break;
      case 1:
        appBartitle.value = '2t';
        break;
      case 2:
        appBartitle.value = '3t';
        break;
      case 3:
        appBartitle.value = '4t';
        break;
    }
  }

  void onTabChanged(int index) {
    selectedtIndex.value = index;
  }
}
南七夏 2025-02-18 11:54:03

我也遇到了类似的要求。

实现这一目标的最佳选择是将提供商与ChangeNotifierProvider一起使用。

您可以使用 https://pub.dev/packages/provider

请参考: https://docs.flutter.dev/deveverment/data-andeferment/data-anda-anda-anda-anda-anda-backend/state/state -MGMT/Simple

I have also come across similar requirement.

Best choice to achieve this is to make use of providers in flutter along with ChangeNotifierProvider.

You can use https://pub.dev/packages/provider

Please refer : https://docs.flutter.dev/development/data-and-backend/state-mgmt/simple

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文