通过应用程序在导航上动态更改Appbar内容?

发布于 2025-02-08 05:38:56 字数 2325 浏览 2 评论 0原文

我的应用程序中有导航,但是我想动态更改Appbar标题,具体取决于当前显示的页面。 主要问题是,我正在使用tabbarview来显示主要页面,但我也可以进入某些子页面。该部分现在正常工作,但是我不知道,我如何根据显示的页面 /子页面进行APPBAR更改

是主类,它保存Appbar:

class AppView extends StatefulWidget {
  const AppView({Key? key}) : super(key: key);

  @override
  State<AppView> createState() => _AppViewState();
}


class _AppViewState extends State<AppView> with TickerProviderStateMixin{

  late TabController _controller;

  @override
  void initState(){
    super.initState();
    _controller = TabController(length: 6, vsync: this);
    _controller.addListener(() {
      if(navKey.currentState!.canPop()){
        navKey.currentState!.pop();
      }
    });
  }


  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () async {
        if(navKey.currentState!.canPop()) {
          navKey.currentState!.pop();
        }
        return false;
    },
      child: Scaffold(
        appBar: AppBar(
          title: HeadLine(
            "Test",
            color: white,
          ),
        ),
          body: Navigator(
            key: navKey,
            onGenerateRoute: (_) => MaterialPageRoute(
              builder: (_) => TabBarView(
              controller: _controller,
              physics: NeverScrollableScrollPhysics(),
              children: [
                HomeScreen(),
                ChatOverviewScreen(),
                ForumOverviewScreen(),
                CalendarScreen(),
                GroupScreen(),
                ProfileScreen()
              ],
            )
            ),
          ),
      bottomNavigationBar: BottomBar(
        controller: _controller,
      )),
    );
  }
}

用于测试,目前只有一个按钮在以下页面之一上调用一个子页面:

class ChatOverviewScreen extends StatelessWidget {
  const ChatOverviewScreen({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Center(
      child: PrimaryButton(
        text: "Do Stuff",
        onPressed: () {
          navKey.currentState!.push(ChatDetailScreen.route());
        },
      ),
    );
  }
}

Navkey通过Basicaly在我的主文件中设置为全局变量,

final GlobalKey<NavigatorState> navKey = GlobalKey<NavigatorState>();

我想设置Appbar的标题,具体取决于显示哪个页面 /子页面,当它是子页面时,应该在也是箭头的箭头。

I've got my Navigation in my App working, but I would like to change the AppBar Title dynamically, depending on which page is currently displayed.
Main Problem is, I'm using a TabBarView, to show the main pages, but I can also get to some sub pages. That part is now working fine, but I can't figure out, how I could make the AppBar change depending on the displayed page / sub-page

Here is the main class, which holds the AppBar:

class AppView extends StatefulWidget {
  const AppView({Key? key}) : super(key: key);

  @override
  State<AppView> createState() => _AppViewState();
}


class _AppViewState extends State<AppView> with TickerProviderStateMixin{

  late TabController _controller;

  @override
  void initState(){
    super.initState();
    _controller = TabController(length: 6, vsync: this);
    _controller.addListener(() {
      if(navKey.currentState!.canPop()){
        navKey.currentState!.pop();
      }
    });
  }


  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () async {
        if(navKey.currentState!.canPop()) {
          navKey.currentState!.pop();
        }
        return false;
    },
      child: Scaffold(
        appBar: AppBar(
          title: HeadLine(
            "Test",
            color: white,
          ),
        ),
          body: Navigator(
            key: navKey,
            onGenerateRoute: (_) => MaterialPageRoute(
              builder: (_) => TabBarView(
              controller: _controller,
              physics: NeverScrollableScrollPhysics(),
              children: [
                HomeScreen(),
                ChatOverviewScreen(),
                ForumOverviewScreen(),
                CalendarScreen(),
                GroupScreen(),
                ProfileScreen()
              ],
            )
            ),
          ),
      bottomNavigationBar: BottomBar(
        controller: _controller,
      )),
    );
  }
}

For Testing, theres currently just a button which calls a sub page on one of these pages:

class ChatOverviewScreen extends StatelessWidget {
  const ChatOverviewScreen({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Center(
      child: PrimaryButton(
        text: "Do Stuff",
        onPressed: () {
          navKey.currentState!.push(ChatDetailScreen.route());
        },
      ),
    );
  }
}

navKey is set as global variable in my main file via

final GlobalKey<NavigatorState> navKey = GlobalKey<NavigatorState>();

Basicaly I want to set the Title of the AppBar depending on which page / subpage is shown and when it is a sub page, there should also be an arrow to get back.

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

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

发布评论

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

评论(1

弃爱 2025-02-15 05:38:56

您应该做这样的事情:

Widget _buildAppBarTitle(int index) {
  String title = '';

  switch (index) {
    case 0:
      title = 'YOUR TITLE';
      break;
    ... other case...
  }
  return Text(title);
}

Scaffold(
  appBar: AppBar(
    title: _buildAppBarTitle(_currentIndex),
  ),
  body: Navigator(
    ...
  ),
  bottomNavigationBar: BottomNavigationBar(
    onTap: (index) {
      setState(() {
        _currentIndex = index;
      });
    },
    currentIndex: _currentIndex,   
    items: [...],
  ),
)

您可以使用ENUM来识别页面,此外,您可以使用某些州经理。
flutter状态管理

You should do something like this:

Widget _buildAppBarTitle(int index) {
  String title = '';

  switch (index) {
    case 0:
      title = 'YOUR TITLE';
      break;
    ... other case...
  }
  return Text(title);
}

Scaffold(
  appBar: AppBar(
    title: _buildAppBarTitle(_currentIndex),
  ),
  body: Navigator(
    ...
  ),
  bottomNavigationBar: BottomNavigationBar(
    onTap: (index) {
      setState(() {
        _currentIndex = index;
      });
    },
    currentIndex: _currentIndex,   
    items: [...],
  ),
)

You can use enum to identify the pages, also, you can use some state manager.
Flutter State management

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