Flutter 桌面探索 自定义可拖拽导航栏_flutter 导航栏
import 'package:flutter/material.dart';
class DraggableNavigationBarItem {
final Icon icon;
final String title;
final double height;
DraggableNavigationBarItem({
@required this.icon,
@required this.title,
this.height = 60.0,
});
}
class DraggableNavigationBar extends StatefulWidget {
final List<DraggableNavigationBarItem> items;
final ValueChanged<int> onItemSelected;
final int initialIndex;
DraggableNavigationBar({
Key key,
@required this.items,
this.onItemSelected,
this.initialIndex = 0,
}) : super(key: key);
@override
_DraggableNavigationBarState createState() => _DraggableNavigationBarState();
}
class _DraggableNavigationBarState extends State<DraggableNavigationBar> {
int _selectedIndex = 0;
@override
void initState() {
super.initState();
_selectedIndex = widget.initialIndex;
}
// 其他代码略...
}
这个代码实例定义了DraggableNavigationBarItem
类来表示导航栏中的每个项,并且定义了DraggableNavigationBar
类作为有状态的widget,它管理着选中的项目。initState
方法中使用了传入的initialIndex
来设置初始选中的项。这个例子为后续的拖拽逻辑和UI设计提供了基本框架。
评论已关闭