【问题标题】:How to implement fragment transaction in Flutter?如何在 Flutter 中实现分片事务?
【发布时间】:2019-09-11 05:02:34
【问题描述】:

我在屏幕顶部有一个选项卡布局,在屏幕底部有一个底部导航视图。

我需要在中间有一个容器并交换视图,这类似于 Android 中的片段事务。

如何在 Flutter 中实现这一点?

    home: DefaultTabController(
      length: prefList.length,
      child: Scaffold(
        appBar: AppBar(
          backgroundColor: const Color(0xff00234a),
          bottom: TabBar(
            tabs: tabList,
            isScrollable: true,
            indicatorColor: const Color(0xffffffff),
          ),
          title: Text('Test Demo'),
          centerTitle: true,
        ),
        body: Container(
          child: TabBarView(
            children: tabList,
            controller: controller,
          ),
        ),
        // Set the bottom navigation bar
        bottomNavigationBar: Material(
          // set the color of the bottom navigation bar
          color: const Color(0xff00234a),
          // set the tab bar as the child of bottom navigation bar
          child: TabBar(
            tabs: <Tab>[
              Tab(
                icon: Icon(Icons.favorite),
              ),
              Tab(
                icon: Icon(Icons.adb),
              ),
              Tab(
                icon: Icon(Icons.airplay),
              ),
              Tab(
                icon: Icon(Icons.gamepad),
              ),
              Tab(
                icon: Icon(Icons.videogame_asset),
              ),
            ],
            // setup the controller
            controller: controller,
          ),
        ),
      ),
    ),
  );
}

我需要将 TabView 小部件替换为 Android 中用于替换不同片段的容器布局之类的东西!

【问题讨论】:

  • 顶部的TabBar 和底部的TabBar 是一样的吗? TabView,在Android上具有作为片段过渡的滑动效果。您想创建一个自定义布局小部件以在不同页面上重复使用它吗?

标签: flutter


【解决方案1】:

从颤振documentation你可以使用这个实现底部导航

    int _selectedIndex = 0;
static const TextStyle optionStyle = TextStyle(fontSize: 30, fontWeight: FontWeight.bold);
static const List<Widget> _widgetOptions = <Widget>[
  Text(
    'Index 0: Home',
    style: optionStyle,
  ),
  Text(
     'Index 1: Business',
     style: optionStyle,
  ),
  Text(
     'Index 2: School',
     style: optionStyle,
  ),
];

void _onItemTapped(int index) {
  setState(() {
    _selectedIndex = index;
  });
}

@override
Widget build(BuildContext context) {
  return Scaffold(
    appBar: AppBar(
      title: const Text('BottomNavigationBar Sample'),
    ),
    body: Center(
      child: _widgetOptions.elementAt(_selectedIndex),
    ),
    bottomNavigationBar: BottomNavigationBar(
      items: const <BottomNavigationBarItem>[
        BottomNavigationBarItem(
          icon: Icon(Icons.home),
          title: Text('Home'),
        ),
        BottomNavigationBarItem(
          icon: Icon(Icons.business),
          title: Text('Business'),
        ),
        BottomNavigationBarItem(
          icon: Icon(Icons.school),
          title: Text('School'),
        ),
      ],
      currentIndex: _selectedIndex,
      selectedItemColor: Colors.amber[800],
      onTap: _onItemTapped,
    ),
  );
}

用于选项卡式屏幕

//lifted from a project im doing
@override
  Widget build(BuildContext context) {
    return DefaultTabController(
      length: 4,
          child: Scaffold(
        appBar: AppBar(centerTitle: true,        
          title: Text("tabs", style: TextStyle(fontWeight: FontWeight.bold, color: Colors.white)),
          bottom: TabBar(

            indicatorColor: Colors.white,
            tabs: <Widget>[
              Tab(
                icon: Image.asset(
                  "assets/icons/tools.png",
                  height: 38,
                ),
                child: Text("s1",style: TextStyle(fontSize: 10),),
              ),
              Tab(
                icon: Image.asset(
                  "assets/icons/il.png",
                  height: 38,
                ),
                child: Text("s2",style: TextStyle(fontSize: 10)),
              ),
              Tab(
                icon: Icon(Icons.lock),
                child: Text("s3"),
              ),
              Tab(
                icon: Icon(Icons.lock),
                child: Text("s4"),
              ),
            ],
          ),
        ),
        body: TabBarView(children: <Widget>[
          Screen1(),
          Screen2(),
          Screen3(),
          Screen4()
        ],),
      ),
    );

【讨论】:

    猜你喜欢
    • 2022-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-04
    相关资源
    最近更新 更多