Rob*_* J. 5 state-management flutter flutter-provider flutter-state
我正在尝试为我的应用程序实现自定义注销解决方案,无论用户当前在哪里,一旦Logout button点击,应用程序将导航回Login page.
我的想法是,与其监听每个组件的状态更改,不如在主组件上使用一个监听器 -> MyApp。
为简单起见,我将项目精简到最低限度。这是我的 Profile 类的样子:
class Profile with ChangeNotifier {
bool _isAuthentificated = false;
bool get isAuthentificated => _isAuthentificated;
set isAuthentificated(bool newVal) {
_isAuthentificated = newVal;
notifyListeners();
}
}
Run Code Online (Sandbox Code Playgroud)
现在,在 下Main,我已注册此提供程序如下:
void main() => runApp(
MultiProvider(
providers: [
ChangeNotifierProvider(
create: (_) => Profile(),
)
],
child: MyApp(),
),
);
Run Code Online (Sandbox Code Playgroud)
最后是MyApp组件:
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return Consumer<Profile>(
builder: (context, profile, _) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
brightness: Brightness.light,
primaryColor: Color.fromARGB(255, 0, 121, 107),
accentColor: Color.fromARGB(255, 255, 87, 34),
),
home: buildBasePage(context, profile),
);
},
);
}
Widget buildBasePage(BuildContext context, Profile currentProfile) {
return !currentProfile.isAuthentificated
? LoginComponent()
: MyHomePage(title: 'Flutter Demo Home Page test');
}
}
Run Code Online (Sandbox Code Playgroud)
我的想法是,由于MyApp组件是master,我应该能够创建一个使用者,如果当前用户通过身份验证,它将收到通知,并会相应地做出响应。
发生的情况是,当我在例如MyHomePage组件中并单击Logout()如下所示的方法时:
void _logout() {
Provider.of<Profile>(context, listen: false).isAuthentificated = false;
}
Run Code Online (Sandbox Code Playgroud)
我希望在更改属性时,初始MyApp组件会做出反应并生成LoginPage;事实并非如此。我试过从Consumer改为 ,Provider.of<Profile>(context, listen: false)但结果相同。
我需要做什么才能使这个概念发挥作用?这样做是否正确?
我的意思是我肯定可以Profile以某种方式更新我的课程,我添加以下方法:
logout(BuildContext context) {
_isAuthentificated = false;
Navigator.push(
context, MaterialPageRoute(builder: (context) => LoginComponent()));
}
Run Code Online (Sandbox Code Playgroud)
然后简单地调用Provider.of<Profile>(context, listen: false).logout(),但是我认为 Provider 包是为此而设计的……或者我错过了什么?
对此问题的任何帮助将不胜感激。
我不知道为什么它对你不起作用。这是我根据您的描述构建的完整示例。有用!
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
class Profile with ChangeNotifier {
bool _isAuthentificated = false;
bool get isAuthentificated {
return this._isAuthentificated;
}
set isAuthentificated(bool newVal) {
this._isAuthentificated = newVal;
this.notifyListeners();
}
}
void main() {
return runApp(
MultiProvider(
providers: [
ChangeNotifierProvider<Profile>(
create: (final BuildContext context) {
return Profile();
},
)
],
child: MyApp(),
),
);
}
class MyApp extends StatelessWidget {
@override
Widget build(final BuildContext context) {
return Consumer<Profile>(
builder: (final BuildContext context, final Profile profile, final Widget child) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(primarySwatch: Colors.blue),
home: profile.isAuthentificated ? MyHomePage() : MyLoginPage(),
);
},
);
}
}
class MyHomePage extends StatelessWidget {
@override
Widget build(final BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text("Home [Auth Protected]")),
body: Center(
child: RaisedButton(
child: const Text("Logout"),
onPressed: () {
final Profile profile = Provider.of<Profile>(context, listen: false);
profile.isAuthentificated = false;
},
),
),
);
}
}
class MyLoginPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text("Login")),
body: Center(
child: RaisedButton(
child: const Text("Login"),
onPressed: () {
final Profile profile = Provider.of<Profile>(context, listen: false);
profile.isAuthentificated = true;
},
),
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3877 次 |
| 最近记录: |