Flutter 可重复使用的颤振组件(页脚)

Flutter 可重复使用的颤振组件(页脚),flutter,Flutter,我已经实现了一个通用组件footer菜单,它应该显示footer菜单中的项目。页脚菜单将贯穿整个应用程序,我只想在小部件中包含页脚菜单组件,并将所需的菜单项作为参数传递 import 'package:flutter/material.dart'; class FooterMenu extends StatefulWidget { final List<Map<String, dynamic>> menuItemsMap; List<BottomNavig

我已经实现了一个通用组件
footer菜单
,它应该显示footer菜单中的项目。页脚菜单将贯穿整个应用程序,我只想在小部件中包含页脚菜单组件,并将所需的菜单项作为参数传递

import 'package:flutter/material.dart';

class FooterMenu extends StatefulWidget {
  final List<Map<String, dynamic>> menuItemsMap;
  List<BottomNavigationBarItem> navItems = [];

  FooterMenu({Key key, @required this.menuItemsMap}) : super(key: key) {
    this.buildNavItems();
  }

  void buildNavItems() {
    menuItemsMap.forEach((element) {
      navItems.add(BottomNavigationBarItem(
        icon: element['icon'],
        title: Text(element['text']),
      ));
    });
  }


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

class _FooterMenuState extends State<FooterMenu> {
  int _selectedIndex = 0;

  _FooterMenuState(); //constructor

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      bottomNavigationBar: BottomNavigationBar(
        items: widget.navItems,
        currentIndex: _selectedIndex,
        // selectedItemColor: Colors.amber[800],
        onTap: _onItemTapped,
      ),
    );
  }
}
导入“包装:颤振/材料.省道”;
类FooterMenu扩展StatefulWidget{
最终清单menuItemsMap;
列表导航项=[];
FooterMenu({Key,@required this.menuItemsMap}):超级(Key:Key){
this.buildNavItems();
}
void buildNavItems(){
menuItemsMap.forEach((元素){
navItems.add(BottomNavigationBarItem(
图标:元素['icon'],
标题:文本(元素['Text']),
));
});
}
@凌驾
_FooterMenuState createState()=>\u FooterMenuState();
}
类页脚自定义扩展状态{
int _selectedIndex=0;
_FooterMenuState();//构造函数
void\u未映射(整数索引){
设置状态(){
_selectedIndex=索引;
});
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
底部导航栏:底部导航栏(
项目:widget.navItems,
currentIndex:_selectedIndex,
//selectedItemColor:Colors.amber[800],
onTap:\u未映射,
),
);
}
}

现在如何将其包含在已定义了
build
方法的页面上?甚至可能吗?

您所说的构建方法是什么意思?如果您的意思是如何将您自己构建的自定义BottomNavigationBar包含在另一个带有支架的页面中,只需返回BottomNavigationBar,而不返回带有支架的BottomNavigationBar,只需执行以下操作即可:

class _FooterMenuState extends State<FooterMenu> {
  int _selectedIndex = 0;

  _FooterMenuState(); //constructor

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

  @override
  Widget build(BuildContext context) {
    return BottomNavigationBar(
        items: widget.navItems,
        currentIndex: _selectedIndex,
        // selectedItemColor: Colors.amber[800],
        onTap: _onItemTapped,
    );
  }
}



// Some Other class that you want to include your FooterMenu in
Widget build(BuildContext context) {
  return Scaffold(
    //other widgets
    bottomNavigationBar: FooterMenu(<Your argument menu items>),
  );
}

class\u footermenstate扩展状态{
int _selectedIndex=0;
_FooterMenuState();//构造函数
void\u未映射(整数索引){
设置状态(){
_selectedIndex=索引;
});
}
@凌驾
小部件构建(构建上下文){
返回底部导航栏(
项目:widget.navItems,
currentIndex:_selectedIndex,
//selectedItemColor:Colors.amber[800],
onTap:\u未映射,
);
}
}
//要包含页脚菜单的其他类
小部件构建(构建上下文){
返回脚手架(
//其他小部件
底部导航栏:页脚菜单(),
);
}

是什么阻止您修改
build
方法?你能提供更多的信息吗?