FilledStacks / FilledStacks/flutter-tutorials

Shared model

Offen
#146 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
Vorherrschende Sprache
Dart
Sterne
4.8k
Forks
1.7k
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beschreibung

Hi, I have a question, I have this code:

StartUpScreen:

```
class StartUpScreen extends StatelessWidget {
const StartUpScreen({Key? key}) : super(key: key);

@override
Widget build(BuildContext context) {
var userProvider = context.read();
return ViewModelBuilder.reactive(
viewModelBuilder: () => StartUpViewModel(),
onModelReady: (model) => model.handleStartUpLogic(),
builder: (context, model, child) => Scaffold(
backgroundColor: Colors.white,
body: model.isBusy
? const CircularProgressIndicator()
: model.isLogged
? const HomeScreen()
: LoginScreen()),
);
}
}
```

and relative view model:

```
class StartUpViewModel extends BaseViewModel {
final AuthenticationService _authenticationService = locator();

bool _isLogged = false;
bool get isLogged {
return _isLogged;
}

Future handleStartUpLogic() async {
setBusy(true);
bool hasLoggedInUser = _authenticationService.isUserLoggedIn();
if (hasLoggedInUser) {
_isLogged = true;
setBusy(false);
} else {
_isLogged = false;
setBusy(false);
}
}
}
```

on lunch application I'm not logged and the app show me LoginScreen:

```
class LoginScreen extends StatelessWidget {
LoginScreen({Key? key}) : super(key: key);

final emailController = TextEditingController();
final passwordController = TextEditingController();

@override
Widget build(BuildContext context) {
return ViewModelBuilder.reactive(
viewModelBuilder: () => LoginViewModel(),
builder: (context, model, child) => Scaffold(
backgroundColor: Colors.white,
body: Padding(
padding: const EdgeInsets.symmetric(horizontal: 50),
child: Column(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
SizedBox(
height: 150,
child: Image.asset('assets/images/title.png'),
),
InputField(
placeholder: 'Email',
controller: emailController,
),
verticalSpaceSmall,
InputField(
placeholder: 'Password',
password: true,
controller: passwordController,
),
verticalSpaceMedium,
Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.end,
children: [
BusyButton(
title: 'Login',
busy: model.isBusy,
onPressed: () {
model.login(
email: emailController.text,
password: passwordController.text,
);
},
)
],
),
verticalSpaceMedium,
],
),
),
),
);
}
}
```

and relative view model:

```
class LoginViewModel extends BaseViewModel {
final AuthenticationService _authenticationService = locator();
final DialogService _dialogService = locator();

Future login({
required String email,
required String password,
}) async {
try {
setBusy(true);
await _authenticationService.loginWithEmail(
email: "myemail",
password: "mypass!",
);
setBusy(false);
} catch (e) {
setBusy(false);
await _dialogService.showDialog(
title: 'Login Failure',
description: 'General login failure. Please try again later',
);
}
}
}

```

The question is, how to change model.isLogged on StartUpViewModel for reload the widget and show the LoginScreen simil to classic provider? I could use navigation but this operation can be useful for other pages as well. for example, in classic provider i've a Page A with total coins, in Page B i add 10 coin, with provider i can update the value with notifyListeners(); without call the server. How to emulate this? it's possible?

Beitragsleitfaden

Für dieses Repository ist kein Beitragsleitfaden indexiert

Rechercherichtung

Beginne mit dem Lesen von StartUpScreen und StartUpViewModel und vergleiche anschließend deren Verwendung von ViewModelBuilder mit LoginScreen und LoginViewModel. Prüfe, wie das bestehende reaktive Modell nach dem Login reagiert und wie das Provider/notifyListeners-Beispiel den gemeinsam genutzten Zustand darstellt. Erledigt ist die Aufgabe, wenn der Issue einen dokumentierten, konsistenten Ansatz zum Aktualisieren gemeinsam genutzter Werte und zum erneuten Erstellen abhängiger Seiten ohne Navigation oder einen zusätzlichen Serveraufruf enthält.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
dart, flutter
Bereich
mobile
Issue-Typ
Feature
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Muss geklärt werden
Anfängerfreundlichkeit
25/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.