如何从分开的抽屉文件中调用振颤路线?

时间:2019-05-19 04:48:48

标签: dart flutter

我正在按照以下步骤设置应用程序:

  1. main.dart(导入点3、4、5,并映射到路线上)
  2. module / DrawerMenu.dart(调用以通过路线导航到第3、4、5点)
  3. module / Dashboard.dart(将导入点2作为菜单)
  4. module / Login.dart(作为菜单导入点2)
  5. module / Profile.dart(导入点2作为菜单)

我尝试了这些代码,但显示了以下错误:

I/flutter (28092): #6      _DrawerMenuState.build.<anonymous closure>
package:route/module/DrawerMenu.dart:45
I/flutter (28092): #7      _InkResponseState._handleTap 
package:flutter/…/material/ink_well.dart:511
I/flutter (28092): #8      _InkResponseState.build.<anonymous closure> 
package:flutter/…/material/ink_well.dart:566
I/flutter (28092): #9      GestureRecognizer.invokeCallback 
package:flutter/…/gestures/recognizer.dart:166
I/flutter (28092): #10     TapGestureRecognizer._checkUp 
package:flutter/…/gestures/tap.dart:240
I/flutter (28092): #11     TapGestureRecognizer.acceptGesture 
package:flutter/…/gestures/tap.dart:211
I/flutter (28092): #12     GestureArenaManager.sweep 
package:flutter/…/gestures/arena.dart:156
I/flutter (28092): #13     _WidgetsFlutterBinding&BindingBase&GestureBinding.handleEvent 
package:flutter/…/gestures/binding.dart:225
I/flutter (28092): #14     _WidgetsFlutterBinding&BindingBase&GestureBinding.dispatchEvent 
package:flutter/…/gestures/binding.dart:199
I/flutter (28092): #15     _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerEvent 
package:flutter/…/gestures/binding.dart:156
I/flutter (28092): #16     _WidgetsFlutterBinding&BindingBase&GestureBinding._flushPointerEventQueue 
package:flutter/…/gestures/binding.dart:102
I/flutter (28092): #17     _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerDataPacket 
package:flutter/…/gestures/binding.dart:86
I/flutter (28092): #21     _invoke1  (dart:ui/hooks.dart:233:10)
I/flutter (28092): #22     _dispatchPointerDataPacket  (dart:ui/hooks.dart:154:5)
I/flutter (28092): (elided 3 frames from package dart:async)
I/flutter (28092):
I/flutter (28092): Handler: onTap
I/flutter (28092): Recognizer:
I/flutter (28092):   TapGestureRecognizer#b7ab7(debugOwner: GestureDetector, state: ready, won arena, finalPosition:
I/flutter (28092):   Offset(95.6, 329.1), sent tap down)
I/flutter (28092): 
═══════════════════

我的这些代码: 1. main.dart

import 'package:flutter/material.dart';
import 'module/Dashboard.dart';
import 'module/Login.dart';
import 'module/Profile.dart';

void main () => runApp(RouteApp());

class RouteApp extends StatefulWidget{
  @override
  _RouteAppState createState() => _RouteAppState();
}

class _RouteAppState extends State<RouteApp>{
  @override
  Widget build(BuildContext context){
    return MaterialApp(
      initialRoute: '/',
      routes: {
        '/': (context) => Dashboard(),
        '/login': (context) => Login(),
        'profile': (context) => Profile()
      },
    );
  }
}
  1. DrawerMenu.dart
import 'package:flutter/material.dart';

class DrawerMenu extends StatefulWidget {
  @override
  _DrawerMenuState createState() => _DrawerMenuState();
}

class _DrawerMenuState extends State<DrawerMenu> {
  @override
  Widget build(BuildContext context) {
    return Drawer(
        child: ListView(
      children: <Widget>[
        UserAccountsDrawerHeader(
          accountName: Text('Mulyawan Sentosa'),
          accountEmail: Text('mulyawan@flazhost.com'),
        ),
        ListTile(
          leading: FlutterLogo(size: 20),
          title: Text('Dashboard'),
          onTap: () {
            setState(() {
              Navigator.of(context).pushNamed('/');
              Navigator.pop(context);
            });
          },
        ),
        ListTile(
          leading: FlutterLogo(size: 20),
          title: Text('Login'),
          onTap: () {
            setState(() {
              Navigator.of(context).pushNamed('/login');
              Navigator.pop(context);
            });
          },
        ),
        ListTile(
          leading: FlutterLogo(size: 20),
          title: Text('Profile'),
          onTap: () {
            setState(() {
              Navigator.of(context).pushNamed('/profile');
              Navigator.pop(context);
            });
          },
        ),
      ],
    ));
  }
}
  1. Dashboard.dart
import 'package:flutter/material.dart';
import './DrawerMenu.dart';

class Dashboard extends StatefulWidget {
  @override
  _DashboardState createState() => _DashboardState();
}

class _DashboardState extends State<Dashboard> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        drawer: DrawerMenu(),
        appBar: AppBar(
          title: Text('Dashboard'),
        ),
        body: Container(
          child: Text('Halaman Dashboard'),
        ),
      );
  }
}
  1. Login.dart
import 'package:flutter/material.dart';

class Login extends StatefulWidget{
  @override
  _LoginState createState() => _LoginState();
}

class _LoginState extends State<Login>{
  @override
  Widget build(BuildContext context){
    return Container(
      child: Text('Halaman Login'),
    );
  }
}
  1. Profile.dart
import 'package:flutter/material.dart';
import './DrawerMenu.dart';

class Profile extends StatefulWidget{
  @override
  _ProfileState createState() => _ProfileState();
}

class _ProfileState extends State<Profile>{
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        drawer: DrawerMenu(),
        appBar: AppBar(
          title: Text('Profile'),
        ),
        body: Container(
          child: Text('Halaman Profile'),
        ),
      );
  }
}

设置好后,我需要从分开的抽屉中呼叫路由。请帮忙吗?

1 个答案:

答案 0 :(得分:1)

在onTap函数中,再次删除setState()(因为不需要)。并将弹出功能移到顶部:

onTap: () {
  Navigator.pop(context);
  Navigator.pushNamed(context, '/profile');
}

使用先前的方法,首先要导航,然后尝试再次恢复。我知道您打算关闭DrawerMenu。为此,请先关闭DrawerMenu,然后导航至所需位置。

您的“个人资料”页面路由名称也不以“ /”开头。这不是强制性的,但不建议这样做。

用脚手架或材料(如果不需要脚手架)小部件包装LoginPage。