2

我已经用颤振实现了以下底部导航栏

import 'package:flutter/material.dart';

class Test extends StatelessWidget  {
@override
Widget build(BuildContext context) {
// TODO: implement build
return new Scaffold(
    bottomNavigationBar: new BottomNavigationBar(
        items: [
          new BottomNavigationBarItem(
              icon: new Icon(Icons.add),
              title: new Text("trends")
          ),
          new BottomNavigationBarItem(
              icon: new Icon(Icons.location_on),
              title: new Text("feed")
          ),
          new BottomNavigationBarItem(
              icon: new Icon(Icons.people),
              title: new Text("community")
          )
        ]
    )
);}}

我如何制作,onTap在每个BottomNavigationBar项目上打开新.dart课程?我已经尝试使用TabBarViewfor 选项卡,但不知道如何处理底部导航栏。

4

1 回答 1

7

你可以这样做

import 'package:flutter/material.dart';

class Test extends StatefulWidget {
  @override
  TestState createState() {
    return new TestState();
  }
}

class TestState extends State<Test> {
  int _currentIndex = 0;

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      bottomNavigationBar: new BottomNavigationBar(
        currentIndex: _currentIndex,
        onTap: (newIndex) => setState((){_currentIndex = newIndex;}),
        items: [
          new BottomNavigationBarItem(
              icon: new Icon(Icons.add),
              title: new Text("trends")
          ),
          new BottomNavigationBarItem(
              icon: new Icon(Icons.location_on),
              title: new Text("feed")
          ),
          new BottomNavigationBarItem(
              icon: new Icon(Icons.people),
              title: new Text("community")
          ),
        ],
      ),
      body: new IndexedStack(
        index: _currentIndex,
        children: <Widget>[
          new YourCustomTrendsWidget(),
          new YourCustomFeedWidget(),
          new YourCustomCommunityWidget(),
        ],
      ),
    );
  }
}
于 2018-05-21T12:43:23.160 回答