加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

dart – 在flutter中从tab1切换到tab3会产生错误 – 最小颤振App

发布时间:2020-12-14 14:56:08 所属栏目:百科 来源:网络整理
导读:我目前有一个TabBarView.TabBarView有3个选项卡. 第二个选项卡有一个页面,其中还有两个选项卡.现在的问题是我是否直接从tab0切换到tab2.我收到以下错误: flutter: ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞════════════════════
我目前有一个TabBarView.TabBarView有3个选项卡.
第二个选项卡有一个页面,其中还有两个选项卡.现在的问题是我是否直接从tab0切换到tab2.我收到以下错误:

flutter: ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
flutter: The following assertion was thrown while finalizing the widget tree:
flutter: 'package:flutter/src/widgets/scroll_position.dart': Failed assertion: line 627 pos 12: 'pixels !=
flutter: null': is not true.
flutter:
flutter: Either the assertion indicates an error in the framework itself,or we should provide substantially
flutter: more information in this error message to help you determine and fix the underlying cause.
flutter: In either case,please report this assertion by filing a bug on GitHub:
flutter:   https://github.com/flutter/flutter/issues/new
flutter:
flutter: When the exception was thrown,this was the stack:
flutter: #2      ScrollPosition.dispose (package:flutter/src/widgets/scroll_position.dart)
flutter: #3      ScrollPositionWithSingleContext.dispose (package:flutter/src/widgets/scroll_position_with_single_context.dart:260:11)
flutter: #4      ScrollableState.dispose (package:flutter/src/widgets/scrollable.dart:324:14)
flutter: #5      StatefulElement.unmount (package:flutter/src/widgets/framework.dart:3821:12)
flutter: #6      _InactiveElements._unmount (package:flutter/src/widgets/framework.dart:1697:13)
flutter: #7      _InactiveElements._unmount.<anonymous closure> (package:flutter/src/widgets/framework.dart:1695:7)
flutter: #8      ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:3676:14)
flutter: #9      _InactiveElements._unmount (package:flutter/src/widgets/framework.dart:1693:13)

这是应用程序本身

Page : main.dart
import 'package:flutter/material.dart';
import "TabsPage.dart";

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

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

class MyHomePage extends StatefulWidget {

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

  final String title;

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

class _MyHomePageState extends State<MyHomePage>  with SingleTickerProviderStateMixin {

  TabController _tabController;
  var tabBarView;
  var bottomNavigationBar;

  void initializeTabs()
  {
    _tabController = new TabController(vsync: this,length: 3);
  }

  Widget getBottomNavigationBar() {
    bottomNavigationBar = new Material(
            child: new TabBar(controller: _tabController,tabs: <Tab>[
              new Tab(text: "PageA" ),new Tab(text: "PageB"),new Tab(text: "PageC"),]));
    return bottomNavigationBar;
  }

  @override
  void initState() {
    super.initState();
    initializeTabs();
  }

  @override
  Widget build(BuildContext context) {


    return new Scaffold(
      appBar: new AppBar(
        title: new Text("Tabs demo"),automaticallyImplyLeading: false,bottomNavigationBar: getBottomNavigationBar(),//Add the bottom Navigation Bar
      body:  new TabBarView(controller: _tabController,children: <Widget>[
        new Text("PageA"),new MyTest(),new Text("PageC"),]),//The container that will display the pages
      );
  }
}

这是第二页

import 'package:flutter/material.dart';

class MyTest extends StatefulWidget {
    MyTest();

    @override
    MyTestState createState() => new MyTestState();
}

    class MyTestState extends State<MyTest> with SingleTickerProviderStateMixin,RouteAware {
        bool isAlive;
        TabController _tabController;
        var tabBarView;

        MyTestState();

        @override
        void initState() {
            super.initState();
            _tabController = new TabController(vsync: this,length: 2,initialIndex: 1);

        }

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

        @override
        Widget build(BuildContext context) {
            var scaffold = Scaffold(
                appBar: new AppBar(
                    automaticallyImplyLeading: false,bottom: new PreferredSize(
                        preferredSize: new Size(200.0,15.0),child: new Container(
                            width: 200.0,child: new TabBar(
                                controller: _tabController,tabs: [
                                    new Container(
                                        child: new Tab(text: 'TabA'),new Container(
                                        child: new Tab(text: 'TabB'),],body: new TabBarView(controller: _tabController,children: <Widget>[

                    new Text("Hello World in TabA"),new Text("Hello World in TabB"),//The container that will display the pages
                );

            return scaffold;
        }
}

如果我从第二页的正文中替换TabBarView,则不会发生此问题

解决方法

我很久以前就报告了这个bug

https://github.com/flutter/flutter/issues/11267

https://github.com/flutter/flutter/issues/11350

我意识到你不能拥有嵌套的脚手架.如果您嵌套Scaffolds,则行为将变为未定义且不受颤振团队的支持.

重构您的应用程序,以便Scaffold不再嵌套

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读