android 从详细视图返回时,Flutter TabBar重置索引

fivyi3re  于 2023-06-04  发布在  Android
关注(0)|答案(2)|浏览(287)

这是我的场景:

  • 该应用程序有一个带有5个标签的标签栏
  • 某些视图具有详图视图

详细视图的实现:

onTap: () => Navigator.push(context,
          MaterialPageRoute(builder: (context) => RPlanDetail(lesson))),

(RPlanDetail是详细视图,lesson是传递的数据)
基础视图

**现在问题来了:**当我返回到基本视图时,选项卡栏上的索引是错误的。因此,选项卡栏指示您在第一个视图中,而您在另一个视图中。

我该怎么解决这个问题?
如果您需要更多信息,请告诉我。
谢谢你的帮助!

2skhul33

2skhul331#

我有一个代码演示如下:

import 'package:flutter/material.dart';

void main() {
  runApp(
    MaterialApp(
      home: TabBarDemo(),
    ),
  );
}

class TabBarDemo extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return TabBarDemoState();
  }
}

class TabBarDemoState extends State<TabBarDemo> with TickerProviderStateMixin {
  static int _index = 0;
  TabController _controller;

  @override
  void initState() {
    super.initState();
    _controller = TabController(
        initialIndex: TabBarDemoState._index, length: 3, vsync: this);
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        bottom: TabBar(
          controller: _controller,
          tabs: [
            Tab(icon: Icon(Icons.directions_car)),
            Tab(icon: Icon(Icons.directions_transit)),
            Tab(icon: Icon(Icons.directions_bike)),
          ],
          onTap: (int index) {
            TabBarDemoState._index = index;
          },
        ),
        title: Text('Tabs Demo'),
      ),
      body: TabBarView(
        controller: _controller,
        children: [
          Center(
            child: FlatButton(
              color: Colors.red,
              child: Text("Go to Detail page"),
              onPressed: () {
                Navigator.push(
                    context,
                    MaterialPageRoute(
                        builder: (ct) => DetailPage(TabBarDemoState._index)));
              },
            ),
          ),
          Center(
            child: FlatButton(
              color: Colors.red,
              child: Text("Go to Detail page"),
              onPressed: () {
                Navigator.push(
                    context,
                    MaterialPageRoute(
                        builder: (ct) => DetailPage(TabBarDemoState._index)));
              },
            ),
          ),
          Center(
            child: FlatButton(
              color: Colors.red,
              child: Text("Go to Detail page"),
              onPressed: () {
                Navigator.push(
                    context,
                    MaterialPageRoute(
                        builder: (ct) => DetailPage(TabBarDemoState._index)));
              },
            ),
          ),
        ],
      ),
    );
  }
}

class DetailPage extends StatelessWidget {
  DetailPage(this.index);
  final int index;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: Center(
        child: Text("Detail page $index"),
      ),
    );
  }
}

希望能帮到你。但我有一个建议,你应该取代Navigator.pushNavigator.pushName和使用导航命名路线,因为它不创建一个新的屏幕。您可以阅读更多:https://flutter.dev/docs/cookbook/navigation/named-routes

amrnrhlw

amrnrhlw2#

在'initState()'中设置Tab Controller;”
不要将Tab Controller放在“build(BuildContext context)”中

@override
 void initState() {
 super.initState();
 _tabController = new TabController(length: 3, vsync: this);
 }

相关问题