gpt4 book ai didi

Flutter 提供程序状态管理、注销概念

转载 作者:行者123 更新时间:2023-12-02 18:51:04 25 4
gpt4 key购买 nike

我正在尝试为我的应用程序实现自定义注销解决方案,无论用户当前位于何处,一旦Logout button单击后,应用程序将导航回 Login page .

我的想法是,我不会在每个组件上监听状态更改,而是在主组件上有一个监听器 -> MyApp .

为了简单起见,我将项目精简到最低限度。我的 Profile 类如下所示:

class Profile with ChangeNotifier {
bool _isAuthentificated = false;
bool get isAuthentificated => _isAuthentificated;
set isAuthentificated(bool newVal) {
_isAuthentificated = newVal;
notifyListeners();
}
}

现在,在Main下,我已按如下方式注册该提供程序:

void main() => runApp(
MultiProvider(
providers: [
ChangeNotifierProvider(
create: (_) => Profile(),
)
],
child: MyApp(),
),
);

最后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');
}
}

我的想法是,如 MyApp组件是,我应该能够创建一个消费者,如果当前用户通过身份验证,它将收到通知,并会做出相应的响应。

发生的情况是,当我在例如MyHomePage组件,然后我单击 Logout()方法如下:

  void _logout() {
Provider.of<Profile>(context, listen: false).isAuthentificated = false;
}

我希望在更改属性时,初始 MyApp组件会 react 并生成 LoginPage ;事实并非如此。我尝试从 Consumer 进行更改至Provider.of<Profile>(context, listen: false)但结果相同。

为了让这个概念发挥作用,我需要做什么?这样做是否正确?

我的意思是我肯定可以更新我的 Profile在某种程度上,我添加了以下方法:

  logout(BuildContext context) {
_isAuthentificated = false;

Navigator.push(
context, MaterialPageRoute(builder: (context) => LoginComponent()));
}

然后只需调用 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;
},
),
),
);
}
}

关于Flutter 提供程序状态管理、注销概念,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60108556/

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