我有这个Flutter 代码,目前它很短,仍然是可管理的,但我预见到它将在future 非常长,所以我想将代码拆分到单独的文件中.此代码包含位于应用程序左侧的导航栏.我希望将代码的导航轨道部分(从Row小部件开始)转移到另一个文件

//*main.dart*//
class _MainAppState extends State<MainApp> {

  int index = 0;
  bool expanded = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: Stack(
          children: <Widget>[
            Padding(
              padding: const EdgeInsets.only(left: 72.0),
              child: Column(
                children: <Widget>[
                  Expanded(
                    child: SfPdfViewer.network(
                      "https://cdn.syncfusion.com/content/PDFViewer/flutter-succinctly.pdf",
                    ),
                  ),
                ],
              ),
            ),
            Row(
              children: <Widget>[
                MouseRegion(
                  onEnter: (PointerEnterEvent event) {
                    expanded = true;
                    setState(() {});
                  },
                  onHover: (PointerHoverEvent event) {},
                  onExit: (PointerExitEvent event) {
                    expanded = false;
                    setState(() {});
                  },
                  child: NavigationRail(
                    selectedIndex: index,
                    extended: expanded,
                    destinations: const <NavigationRailDestination>[
                      NavigationRailDestination(
                        icon: Icon(Icons.one_k),
                        label: Text("one"),
                      ),
                      NavigationRailDestination(
                        icon: Icon(Icons.two_k),
                        label: Text("two"),
                      ),
                      NavigationRailDestination(
                        icon: Icon(Icons.three_k),
                        label: Text("three"),
                      ),
                    ],
                    onDestinationSelected: (int i) {
                      index = i;
                      setState(() {});
                    },
                  ),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}

我试图创建一个返回整个行小部件的函数,但我面临着MouseRegion将无法访问展开的变量和setState函数的问题

//*nav_rail.dart*//
Widget buildNavRail(BuildContext context){
  return 
        Row(
          children: <Widget>[
            MouseRegion(
              onEnter: (PointerEnterEvent event) {
                expanded = true;
                setState(() {});
              },
              onHover: (PointerHoverEvent event) {},
              onExit: (PointerExitEvent event) {
                expanded = false;
                setState(() {});
              },
              child: NavigationRail(
                selectedIndex: index,
                extended: expanded,
                destinations: const <NavigationRailDestination>[
                  NavigationRailDestination(
                    icon: Icon(Icons.one_k),
                    label: Text("one"),
                  ),
                  NavigationRailDestination(
                    icon: Icon(Icons.two_k),
                    label: Text("two"),
                  ),
                  NavigationRailDestination(
                    icon: Icon(Icons.three_k),
                    label: Text("three"),
                  ),
                ],
                onDestinationSelected: (int i) {
                  index = i;
                  setState(() {});
                },
              ),
            ),
          ],
        );
}

我已经通过import 'nav_rail.dart'导入了文件.在Flutter 中做这件事的正确方法是什么?如果无法完成,是否存在类似于C/C++#Include的功能,在该功能中,预处理器会直接将文件内容复制到#Include所在的位置

推荐答案

你可以做一些参数. 下面是一个例子:

//*nav_rail.dart*//
class NavRail extends StatefulWidget {
// ? means this paramater can be null.
// without ?, u need to add `required` like my example.
  final Function? onEnter;
  final Function? onExit;
  final Function? onDestinationSelected;
  final bool? expanded;
  // or can not be null
  // final bool expanded;
  final int? selectedIndex;
  const NavRail({
    super.key,
    this.onEnter,
    this.onExit,
    this.onDestinationSelected,
    this.expanded = false, //default value for nullable expanded,or u can make a if(widget.expanded == null) in use
    // or can not be null
    // required this.expanded,
    this.selectedIndex,
  });

  @override
  State<NavRail> createState() => _NavRailState();
}

class _NavRailState extends State<NavRail> {
  @override
  Widget build(BuildContext context) {
    return Row(
      children: <Widget>[
        MouseRegion(
          onEnter: (PointerEnterEvent event) {
            widget.onEnter?.call(event);
          },
          onHover: (PointerHoverEvent event) {},
          onExit: (PointerExitEvent event) {
            widget.onExit?.call(event);
          },
          child: NavigationRail(
            selectedIndex: widget.selectedIndex,
            extended: widget.expanded,
            destinations: const <NavigationRailDestination>[
              NavigationRailDestination(
                icon: Icon(Icons.one_k),
                label: Text("one"),
              ),
              NavigationRailDestination(
                icon: Icon(Icons.two_k),
                label: Text("two"),
              ),
              NavigationRailDestination(
                icon: Icon(Icons.three_k),
                label: Text("three"),
              ),
            ],
            onDestinationSelected: (int i) {
              widget.onDestinationSelected?.call(i);
            },
          ),
        ),
      ],
    );
  }
}

然后,您可以按如下方式使用此小部件:

            NavRail(
              expanded: expanded,
              selectedIndex: index,
              onEnter: (PointerEnterEvent event) {
                setState(() {
                  // do updates in setState, not after value changes.
                  expanded = true;
                });
              },
              onExit: (PointerExitEvent event) {
                setState(() {
                  expanded = false;
                });
              },
              // ... and other things u need to change.
            ),

你可以做任何参数,像你的destinations,只需做List< NavigationRailDestination> destinations作为新的参数即可.

Flutter相关问答推荐

Dexing时出错.将Flutter 升级到3.19.0后

我怎样才能判断一个字符串值是否为空(&Q;&Q;)?

在Android Studio的新用户界面中未找到热重新加载

Android Studio中的Ffltter&;Couchbase:进程C:/Program Files/Git/bin/bash以非零退出值35结束

Flutter 块消费者仅对特定的状态属性更改做出react

如何画三角形的圆角?

SliverAppbar 呈蓝色

尽管海拔设置为 0,如何删除 appBar 下的下划线

升级到 Flutter 3.10 后,Flutter 键盘填充不起作用. Flutter 3.10 如何防止 BottomSheet 被键盘覆盖?

在 Flutter 中显示有关提供者通知的对话框

如何在 Flutter 中测试动画图标

具有 BLOC 和存储库模式的 Flutter Workmanager

有没有办法为文本中的下划线创建自定义样式?

Flutter HLS .m3u8 视频无法实时播放

如何在Flutter中制作带有波浪边框的圆圈来显示图像?

Banner Ad.test 广告单元 ID 是否已过时?

将一个 Flutter 应用程序集成到另一个 Flutter 应用程序中

使用 Firebase/Flutter 进行应用判断 - 100% 无效请求

setState 不会在第一次更新 - Flutter

带图像封面的 ElevatedButton