Я использую AppValueNotifier для создания значения с именем showStreamDialogNotifier, которое будет меняться в зависимости от некоторого потока. Когда это значение станет истинным, я хочу отобразить диалоговое окно в форме входа.
Код: Выделить всё
class AppValueNotifier{
ValueNotifier showStreamDialogNotifier = ValueNotifier(false);
void updateShowStreamDialogNotifier({required bool? newShowStreamDialog, required String? newStreamDialogTitle, required String? newStreamDialogMsg}) {
if (newShowStreamDialog != null) {
showStreamDialogNotifier = ValueNotifier(newShowStreamDialog);
}
}
}
Код: Выделить всё
class Login extends StatefulWidget {
const Login({super.key});
@override
State createState() => _LoginState();
}
class _LoginState extends State {
Future showStreamDialog({required BuildContext context, required String title, required String msg}) {
return showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text(title),
content: SingleChildScrollView(
child: ListBody(
children: [
Text(msg),
],
),
),
actions: [
TextButton(
child: const Text('OK'),
onPressed: () {
final globalsOneTimeRead = context.read();
globalsOneTimeRead.updateStreamDialogVariables(newShowStreamDialog: false, newStreamDialogTitle: '', newStreamDialogMsg: '');
context.pop();
},
),
],
);
}
);
}
@override
Widget build(BuildContext context) {
AppValueNotifier appValueNotifier = AppValueNotifier();
return Consumer(
builder: (context, globals, child) => Scaffold(
body: Center(
child: Column(
children: [
Expanded(
child: ValueListenableBuilder( //THE ERROR POINTS TO THIS LINE
valueListenable: appValueNotifier.showStreamDialogNotifier,
builder: (context, value, child) {
if (value == true) {
showStreamDialog(context: context, title: globals.streamDialogTitle, msg: globals.streamDialogMsg);
}
return Text('');
},
),
),
]
)
)
)
}
}
Источник: https://stackoverflow.com/questions/781 ... lenotifier