gpt4 book ai didi

authentication - 如何在Bloc Login Flutter中使用带有身份验证的抽屉导航中的路线

转载 作者:行者123 更新时间:2023-12-03 03:04:40 24 4
gpt4 key购买 nike

我实现了Flutter Bloc Login示例,就像指南页面here一样:

接下来,我在页面上添加了NavigationDrawer。

但是我不知道如何在页面之间导航,我尝试了普通的 Navigator.push ,但没有用,并且我在MaterialApp中使用了路由,但不幸的是,这种方式没有用。

main.dart

void main() {
BlocSupervisor.delegate = SimpleBlocDelegate();
final userRepository = UserRepository();
runApp(
BlocProvider<AuthenticationBloc>(
create: (context) {
return AuthenticationBloc(userRepository: userRepository)
..add(AppStarted());
},
child: App(userRepository: userRepository),
),
);
}

class App extends StatelessWidget {
final UserRepository userRepository;
App({Key key, @required this.userRepository}) : super(key: key);

@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: BlocBuilder<AuthenticationBloc, AuthenticationState>(
builder: (context, state) {
if (state is AuthenticationAuthenticated) {
//return Counter();
//return ListPage();
return ProfilePage();
}
if (state is AuthenticationUnauthenticated) {
return LoginPage(userRepository: userRepository);
}
if (state is AuthenticationLoading) {
return LoadingIndicator();
}
return SplashPage();
},
),
initialRoute: '/',
routes: {
'/': (context) => SplashPage(),
'/counter': (context) => Counter(),
'/profile': (context) => ProfilePage(),
'/list': (context) => ListPage(),
},
);
}
}

navigation_drawer.dart
  Widget build(BuildContext context) {
return Drawer(
child: ListView(
// Important: Remove any padding from the ListView.
padding: EdgeInsets.zero,
children: <Widget>[
Container(...),
ListTile(
title: Text('Profile'),
onTap: () {
/* TODO: check token is not null */
Navigator.pushNamed(context, '/profile');

},
),
ListTile(
title: Text('Counter'),
onTap: () {
Navigator.pushNamed(context, '/counter');
},
),
ListTile(
title: Text('My List'),
onTap: () {
Navigator.pushNamed(context, '/list');
},
),
],
),
);
}

最佳答案

很简单,您忘记使用支架小部件作为的子代。如下所示:
main.dart

void main() {
BlocSupervisor.delegate = SimpleBlocDelegate();
final userRepository = UserRepository();
runApp(
BlocProvider<AuthenticationBloc>(
create: (context) {
return AuthenticationBloc(userRepository: userRepository)
..add(AppStarted());
},
child: App(userRepository: userRepository),
),
);
}

class App extends StatelessWidget {
final UserRepository userRepository;
App({Key key, @required this.userRepository}) : super(key: key);

@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
drawer: build(context),
body: BlocBuilder<AuthenticationBloc, AuthenticationState>(
builder: (context, state) {
if (state is AuthenticationAuthenticated) {
//return Counter();
//return ListPage();
return ProfilePage();
}
if (state is AuthenticationUnauthenticated) {
return LoginPage(userRepository: userRepository);
}
if (state is AuthenticationLoading) {
return LoadingIndicator();
}
return SplashPage();
},
),
),
initialRoute: '/',
routes: {
'/': (context) => SplashPage(),
'/counter': (context) => Counter(),
'/profile': (context) => ProfilePage(),
'/list': (context) => ListPage(),
},
);
}
}
navigation_drawer.dart
Widget build(BuildContext context) {
return Drawer(
child: ListView(
// Important: Remove any padding from the ListView.
padding: EdgeInsets.zero,
children: <Widget>[
Container(...),
ListTile(
title: Text('Profile'),
onTap: () {
/* TODO: check token is not null */
Navigator.pushNamed(context, '/profile');

},
),
ListTile(
title: Text('Counter'),
onTap: () {
Navigator.pushNamed(context, '/counter');
},
),
ListTile(
title: Text('My List'),
onTap: () {
Navigator.pushNamed(context, '/list');
},
),
],
),
);
}
但我不建议您以这种方式与集团合作。

关于authentication - 如何在Bloc Login Flutter中使用带有身份验证的抽屉导航中的路线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60093852/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com