在选项卡initstate()之间切换多次调用

时间:2019-09-19 05:04:09

标签: flutter dart flutter-layout

在选项卡initstate()之间切换多次调用。

我的标签栏A,B,C和D中有4个标签。

情况(1),如果我像从选项卡A切换到选项卡B一样工作正常。

情况(2),但是如果我转到选项卡A到C,则选项卡'B'的initstate()调用了两次

案例(1)的结果

颤振:A

颤抖:B

案例(2)的结果

颤振:A

颤抖:B

颤振:C

颤抖:B

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> with SingleTickerProviderStateMixin{

  TabController _controller;

  void initState() {
    super.initState();
    _controller = TabController(length: 4, vsync: this);
    _controller.addListener(_handleSelected);
  }

  bool alarm = false;

// Function for handle tap event of tab
  void _handleSelected() async {
  }


  Widget build(BuildContext context) {
    return DefaultTabController(
      length: 4,
      child: Scaffold(
        appBar: AppBar(
          bottom: TabBar(
            controller: _controller,
            tabs: [
              Tab(text: "A"),
              Tab(text: "B"),
              Tab(text: "C"),
              Tab(text: "D"),
            ],
          ),
          actions: [
            Switch(
              value: alarm,
              onChanged: (value) {
              },
              activeTrackColor: Color(0xffff6b6b),
              activeColor: Color(0xffff0000),
            ),
          ],
        ),
        body: TabBarView(
          controller: _controller,
          children: [
            A(),
            B(),
            C(),
            D(),
          ],
        ),
      ),
    );
  }
}

2 个答案:

答案 0 :(得分:0)

您可以使用IndexedStack小部件来解决此类问题。

_MyHomePageState中,使用一个变量来管理所选页面的索引;

class _MyHomePageState extends State<MyHomePage> with SingleTickerProviderStateMixin{

int _selectedPage;

/////
Your code 
/////

}

在脚手架中实施IndexedStack

body: IndexedStack(
          index:_selectedPage,
          children: [
            A(),
            B(),
            C(),
            D(),
          ],
        ),

现在在_handleSelected ()方法句柄中,从控制器获取最新的页面索引,并使用setState更新选项卡栏

void _handleSelected () async {
 int index = _controller.page ;// get index from controller (I am not sure about exact parameter name for selected index) ;
setState((){
_selectedPage = index;
});
}

答案 1 :(得分:0)

要使有状态的小部件保持活动状态(不重建或重新渲染),可以使用AutomaticKeepAliveClientMixin。通过这种方式,您可以通过更改``wantKeepAlive''参数轻松确定需要重建的窗口小部件。

以下是A类的示例:

class A extends StatefulWidget {
  @override
  _AState createState() => _AState();
}

class _AState extends State<A> with AutomaticKeepAliveClientMixin{
  bool _isLoading;
  @override
  void initState() {
    super.initState();
    Future.delayed(Duration(seconds: 3)).then((_){
      setState(() {
        _isLoading = false;
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      child: _isLoading == false ?
        Text("A")
        : CircularProgressIndicator(),
    );
  }

  @override
  bool get wantKeepAlive => true;
}