Authentication 如何在具有身份验证的NavigationDrawer的Bloc登录颤振中使用路由
我实现了与指南页面完全相同的Flatter Bloc登录示例: 接下来,我在页面中添加了导航抽屉 但我不知道如何在页面间导航,我尝试了普通的导航器。推送但不起作用,我在MaterialApp中使用了路由,不幸的是,它没有起作用 main.dartAuthentication 如何在具有身份验证的NavigationDrawer的Bloc登录颤振中使用路由,authentication,flutter,dart,bloc,Authentication,Flutter,Dart,Bloc,我实现了与指南页面完全相同的Flatter Bloc登录示例: 接下来,我在页面中添加了导航抽屉 但我不知道如何在页面间导航,我尝试了普通的导航器。推送但不起作用,我在MaterialApp中使用了路由,不幸的是,它没有起作用 main.dart void main() { BlocSupervisor.delegate = SimpleBlocDelegate(); final userRepository = UserRepository(); runApp( BlocP
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(),
},
);
}
}
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');
},
),
],
),
);
}
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(),
},
);
}
}
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');
},
),
],
),
);
}
void main(){
BlocSupervisor.delegate=SimpleBlocDelegate();
final userRepository=userRepository();
runApp(
BlocProvider(
创建:(上下文){
返回AuthenticationBloc(userRepository:userRepository)
…添加(AppStarted());
},
子:应用程序(userRepository:userRepository),
),
);
}
类应用程序扩展了无状态小部件{
最终用户存储库用户存储库;
App({Key Key,@required this.userRepository}):super(Key:Key);
@凌驾
小部件构建(构建上下文){
返回材料PP(
debugShowCheckedModeBanner:false,
主页:BlocBuilder(
生成器:(上下文、状态){
如果(状态为AuthenticationAuthenticated){
//返回计数器();
//返回ListPage();
返回ProfilePage();
}
如果(状态为AuthenticationUnauthenticated){
返回登录页面(userRepository:userRepository);
}
如果(状态为AuthenticationLoading){
返回LoadingIndicator();
}
返回第页();
},
),
initialRoute:“/”,
路线:{
“/”:(上下文)=>SplashPage(),
“/counter”:(上下文)=>counter(),
“/profile”:(上下文)=>ProfilePage(),
“/list”:(上下文)=>ListPage(),
},
);
}
}
导航\u抽屉.省道
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(),
},
);
}
}
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');
},
),
],
),
);
}
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(),
},
);
}
}
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');
},
),
],
),
);
}
小部件构建(构建上下文){
回程抽屉(
子:ListView(
//重要提示:从ListView中删除任何填充。
填充:EdgeInsets.zero,
儿童:[
容器(…),
列表砖(
标题:文本(“概要文件”),
onTap:(){
/*TODO:检查令牌不为空*/
pushNamed(上下文“/profile”);
},
),
列表砖(
标题:文本(“计数器”),
onTap:(){
pushNamed(上下文“/counter”);
},
),
列表砖(
标题:文本(“我的列表”),
onTap:(){
Navigator.pushNamed(上下文“/list”);
},
),
],
),
);
}
这很简单,您忘记将Scaffold小部件作为Material小部件的子部件使用,并且Scaffold有一个属性抽屉,您可以将抽屉传递给它。如下图所示:
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(),
},
);
}
}
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');
},
),
],
),
);
}
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(),
},
);
}
}
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');
},
),
],
),
);
}
void main(){
BlocSupervisor.delegate=SimpleBlocDelegate();
final userRepository=userRepository();
runApp(
BlocProvider(
创建:(上下文){
返回AuthenticationBloc(userRepository:userRepository)
…添加(AppStarted());
},
子:应用程序(userRepository:userRepository),
),
);
}
类应用程序扩展了无状态小部件{
最终用户存储库用户存储库;
App({Key Key,@required this.userRepository}):super(Key:Key);
@凌驾
小部件构建(构建上下文){
返回材料PP(
debugShowCheckedModeBanner:false,
家:脚手架(
抽屉:构建(上下文),
正文:BlocBuilder(
生成器:(上下文、状态){
如果(状态为AuthenticationAuthenticated){
//返回计数器();
//返回ListPage();
返回ProfilePage();
}
如果(状态为AuthenticationUnauthenticated){
返回登录页面(userRepository:userRepository);
}
如果(状态为AuthenticationLoading){
返回LoadingIndicator();
}
返回第页();
},
),
),
initialRoute:“/”,
路线:{
“/”:(上下文)=>SplashPage(),
“/counter”:(上下文)=>counter(),
“/profile”:(上下文)=>ProfilePage(),
“/list”:(上下文)=>ListPage(),
},
);
}
}
导航\u抽屉.省道
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(),
},
);
}
}
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');
},
),
],
),
);
}
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(),
},
);
}
}
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');
},
),
],
),
);
}
小部件构建(构建上下文){
回程抽屉(
子:ListView(
//重要提示:从ListView中删除任何填充。
填充:EdgeInsets.zero,
儿童:[
容器(…),
列表砖(
标题:文本(“概要文件”),
onTap:(){
/*TODO:检查令牌不为空*/
pushNamed(上下文“/profile”);
},
),
列表砖(
标题:文本(“计数器”),
onTap:(){
pushNamed(上下文“/counter”);
},
),
列表砖(
标题:文本(“我的列表”),
onTap:(){
Navigator.pushNamed(上下文“/list”);
},
),
],
),
);
}
但我不建议你以这种方式与集团合作