Flutter 如何在Flatter中的抽屉中再添加一个抽屉?

Flutter 如何在Flatter中的抽屉中再添加一个抽屉?,flutter,flutter-layout,flutter-dependencies,flutter-animation,flutter-web,Flutter,Flutter Layout,Flutter Dependencies,Flutter Animation,Flutter Web,strong文本当我按下抽屉中的设置,然后打开一个新抽屉时。当我点击抽屉中的按钮时,如何实现抽屉 因此,基本上您需要设置状态以更改抽屉小部件的内容。即,最初显示列表项1、2、3。单击项目1时,设置布尔值。如果布尔值为真,则显示列表项4、5、6 不确定这是否有意义。现在正在我的手机上度假,所以我无法提供代码示例。classmyapp{ class MyApp extends StatelessWidget { @override Widget build(BuildContext con

strong文本当我按下抽屉中的设置,然后打开一个新抽屉时。当我点击抽屉中的按钮时,如何实现抽屉


因此,基本上您需要设置状态以更改抽屉小部件的内容。即,最初显示列表项1、2、3。单击项目1时,设置布尔值。如果布尔值为真,则显示列表项4、5、6

不确定这是否有意义。现在正在我的手机上度假,所以我无法提供代码示例。

classmyapp{
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        appBar: AppBar(),
        drawer: MyDrawer(),
        body: Center(
          child: Center(child: Text('Text')),
        ),
      ),
    );
  }
}

class MyDrawer extends StatefulWidget {
  @override
  _DrawerState createState() => _DrawerState();
}

class _DrawerState extends State<MyDrawer> {
  int myIndex;
  PageController _controller;

  @override
  void initState() {
    super.initState();
    _controller = PageController(initialPage: 0);
  }

  //The Logic where you change the pages
  _onChangePage(int index){
    if(index != 0) setState(() => myIndex = index); //change myIndex if you're Selecting between Settings and Explore
    _controller.animateToPage(index.clamp(0, 1),
      duration: const Duration(milliseconds: 500), curve: Curves.linear);
  }

  @override
  void dispose() {
    _controller?.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Drawer(
        child: PageView.builder(
          controller: _controller,
          physics: NeverScrollableScrollPhysics(), //so the user can not move between pages
          itemCount: 2,
          itemBuilder: (context, index) {
            // Original Drawer
            if (index == 0) return MyWidget(
                explore: () => _onChangePage(1),
                settings: () => _onChangePage(2),
              );
            //Second Drawer form the PageView
              switch(myIndex){
                case 1:
                  return MyExploreAll(goBack: () => _onChangePage(0));
                case 2:
                default:
                  return MySettings(goBack: () => _onChangePage(0));
              }
          },
        )
      );
  }
}

//The Menu Drawer (Your first image)
class MyWidget extends StatelessWidget {
  final VoidCallback explore;
  final VoidCallback settings;

  MyWidget({this.explore, this.settings});

  @override
  Widget build(BuildContext context) {
    return CustomScrollView(
      slivers: [
        SliverList(
          delegate: SliverChildListDelegate([
            ListTile(
              title: Text('Send Money'),
              onTap: () => print('Send Money'),
            ),
            ListTile(
              title: Text('Explore All Amazon Pay'),
              onTap: () => print('Explore All Amazon Pay'),
            ),
            const Divider(color: Colors.grey, thickness: 1,),
            ListTile(
              title: Text('Try Prime'),
              onTap: () => print('Try Prime'),
            ),
            ListTile(
              title: Text('Explore All Programs'),
              trailing: const Icon(Icons.arrow_forward_ios),
              onTap: explore,
            ),
            const Divider(color: Colors.grey, thickness: 1,),
            ListTile(
              title: Text('Fun Zone'),
              onTap: () => print('Fun Zone'),
            ),
            const Divider(color: Colors.grey, thickness: 1,),
            //More Stuff
            ListTile(
              title: Text('Settings'),
              trailing: const Icon(Icons.arrow_forward_ios),
              onTap: settings,
            ),
          ])
        )
      ],
    );
  }
}

// The settings Drawer(second image)
class MySettings extends StatelessWidget {
  final VoidCallback goBack;

  MySettings({this.goBack});

  @override
  Widget build(BuildContext context) {
    return CustomScrollView(
      slivers: [
        SliverList(
          delegate: SliverChildListDelegate([
            ListTile(
              leading: const Icon(Icons.arrow_back_ios),
              title: Text('Main Menu'),
              onTap: goBack,
            ),
            ListTile(
              title: Text('Settings', textScaleFactor: 3,),
              onTap: () => print('Settings'),
            ),
            const Divider(color: Colors.grey, thickness: 1,),
            ListTile(
              title: Text('Change Country'),
              onTap: () => print('Change Country'),
            ),
            ListTile(
              title: Text('ETC'),
              onTap: () => print('ETC'),
            ),
            const Divider(color: Colors.grey, thickness: 1,),
            ListTile(
              title: Text('Dummy Text'),
              onTap: () => print('Dummy Text'),
            ),
          ])
        )
      ],
    );
  }
}

class MyExploreAll extends StatelessWidget {
  final VoidCallback goBack;

  MyExploreAll({this.goBack});

  @override
  Widget build(BuildContext context) {
    return CustomScrollView(
      slivers: [
        SliverList(
          delegate: SliverChildListDelegate([
            ListTile(
              leading: const Icon(Icons.arrow_back_ios),
              title: Text('Main Menu'),
              onTap: goBack,
            ),
            ListTile(
              title: Text('Explore All', textScaleFactor: 3,),
              onTap: () => print('Explore'),
            ),
            const Divider(color: Colors.grey, thickness: 1,),
          ])
        )
      ],
    );
  }
}


class MyInnerDrawer extends StatelessWidget {
  final String name;
  final PageController _controller;

  MyInnerDrawer(this._controller, this.name);

  @override
  Widget build(BuildContext context) {
    return Column(children: [
      ListTile(
        title: Text(name),
        trailing: const Icon(Icons.arrow_back_ios),
        onTap: () => _controller.animateToPage(0,
            duration: const Duration(milliseconds: 500), curve: Curves.linear),
      )
    ]);
  }
}
@凌驾 小部件构建(构建上下文){ 返回材料PP( debugShowCheckedModeBanner:false, 家:脚手架( appBar:appBar(), 抽屉:MyDrawer(), 正文:中( 子:居中(子:文本('Text')), ), ), ); } } 类MyDrawer扩展StatefulWidget{ @凌驾 _抽屉状态createState()=>\u抽屉状态(); } 类_抽屉状态扩展状态{ int-myIndex; PageController\u控制器; @凌驾 void initState(){ super.initState(); _控制器=页面控制器(初始页面:0); } //更改页面的逻辑 _onChangePage(int索引){ if(index!=0)setState(()=>myIndex=index);//如果在设置和浏览之间进行选择,请更改myIndex _控制器.animateToPage(索引钳(0,1), 持续时间:常量持续时间(毫秒:500),曲线:Curves.linear); } @凌驾 无效处置(){ _控制器?.dispose(); super.dispose(); } @凌驾 小部件构建(构建上下文){ 回程抽屉( 子项:PageView.builder( 控制器:_控制器, 物理:NeverScrollableScrollPhysics(),//因此用户无法在页面之间移动 物品计数:2, itemBuilder:(上下文,索引){ //原始出票人 如果(索引==0)返回MyWidget( 浏览:()=>(第1页), 设置:()=>\u更改页面(2), ); //第二个抽屉形成页面视图 开关(myIndex){ 案例1: 返回MyExploreAll(goBack:()=>onChangePage(0)); 案例2: 违约: 返回MySettings(goBack:()=>onChangePage(0)); } }, ) ); } } //菜单抽屉(您的第一张图像) 类MyWidget扩展了无状态Widget{ 最后一步是探索; 最终的回调设置; MyWidget({this.explore,this.settings}); @凌驾 小部件构建(构建上下文){ 返回自定义滚动视图( 条子:[ 银表( 委托:SliverChildListDelegate([ 列表砖( 标题:文本(“汇款”), onTap:()=>打印('Send Money'), ), 列表砖( 标题:文本(“浏览所有亚马逊支付”), onTap:()=>print('Explore All Amazon Pay'), ), 常数分隔器(颜色:Colors.grey,厚度:1,), 列表砖( 标题:文本(“Try Prime”), onTap:()=>打印('Try Prime'), ), 列表砖( 标题:文本(“浏览所有程序”), 尾随:常量图标(图标。箭头\前进\ ios), onTap:探索, ), 常数分隔器(颜色:Colors.grey,厚度:1,), 列表砖( 标题:文本(“娱乐区”), onTap:()=>打印('Fun Zone'), ), 常数分隔器(颜色:Colors.grey,厚度:1,), //更多的东西 列表砖( 标题:文本(“设置”), 尾随:常量图标(图标。箭头\前进\ ios), onTap:设置, ), ]) ) ], ); } } //设置抽屉(第二幅图) 类MySettings扩展了无状态小部件{ 最后一次回撤; MySettings({this.goBack}); @凌驾 小部件构建(构建上下文){ 返回自定义滚动视图( 条子:[ 银表( 委托:SliverChildListDelegate([ 列表砖( 前导:常量图标(图标。箭头\u后退\u ios), 标题:文本(“主菜单”), onTap:goBack, ), 列表砖( 标题:Text('Settings',textScaleFactor:3,), onTap:()=>打印('Settings'), ), 常数分隔器(颜色:Colors.grey,厚度:1,), 列表砖( 标题:文本(“变更国家”), onTap:()=>打印('Change Country'), ), 列表砖( 标题:文本('ETC'), onTap:()=>打印('ETC'), ), 常数分隔器(颜色:Colors.grey,厚度:1,), 列表砖( 标题:文本(“伪文本”), onTap:()=>打印('Dummy Text'), ), ]) ) ], ); } } 类MyExploreAll扩展了无状态小部件{ 最后一次回撤; MyExploreAll({this.goBack}); @凌驾 小部件构建(构建上下文){ 返回自定义滚动视图( 条子:[ 银表( 委托:SliverChildListDelegate([ 列表砖( 前导:常量图标(图标。箭头\u后退\u ios), 标题:文本(“主菜单”), onTap:goBack, ), 列表砖( 标题:Text('Explore All',textScaleFactor:3,), onTap:()=>打印('Explore'), ), 常数分隔器(颜色:Colors.grey,厚度:1,), ]) ) ], ); } } 类MyInnerDrawer扩展了无状态小部件{ 最后的字符串名; 最终页面控制器_控制器; MyInnerDrawer(this.\u控制器,this.name); @凌驾 小部件构建(构建上下文){ 返回列(子项:[ 列表砖( 标题:文本(名称),
 class _DrawerState extends State<MyDrawer> {
  PageController _controller;

  @override
  void initState() {
    super.initState();
    _controller = PageController(initialPage: 0);
  }

  _onChangePage(int index){
    _controller.jumpToPage(index);
  }

  @override
  void dispose() {
    _controller?.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Drawer(
        child: PageView.builder(
          controller: _controller,
          physics: NeverScrollableScrollPhysics(),
          itemCount: 3,
          itemBuilder: (context, index) {
            switch(index){
             case 1:
              return MyExploreAll(goBack: () => _onChangePage(0));
             case 2:
              return MySettings(goBack: () => _onChangePage(0));
             case 0:
             default:
               return MyWidget(
                explore: () => _onChangePage(1),
                settings: () => _onChangePage(2),
              );
           }
          },
        )
      );
  }
}