Last active
March 14, 2022 18:45
-
-
Save mkobuolys/f542682880bf16d81d6f6518fa15b11f to your computer and use it in GitHub Desktop.
Flutter Provider: notifyListeners does not update AlertDialog widget
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// https://dartpad.dev/f542682880bf16d81d6f6518fa15b11f | |
/// Answer to the Stack Overflow question: https://stackoverflow.com/questions/71468724/flutter-provider-notifylisteners-does-not-update-alertdialog-widget | |
import 'package:flutter/material.dart'; | |
import 'package:provider/provider.dart'; | |
const Color darkBlue = Color.fromARGB(255, 18, 32, 47); | |
void main() { | |
runApp(MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
theme: ThemeData.dark().copyWith( | |
scaffoldBackgroundColor: darkBlue, | |
), | |
debugShowCheckedModeBanner: false, | |
home: ChangeNotifierProvider( | |
create: (_) => ServicesProvider(), | |
child: const Scaffold( | |
body: Center( | |
child: MyWidget(), | |
), | |
), | |
), | |
); | |
} | |
} | |
class MyWidget extends StatelessWidget { | |
const MyWidget(); | |
showTextDialog(BuildContext context) { | |
final myModel = context.read<ServicesProvider>(); | |
return showDialog( | |
context: context, | |
builder: (_) => ChangeNotifierProvider.value( | |
value: myModel, | |
child: MyDialog(), | |
), | |
); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return ElevatedButton( | |
child: const Text('Show modal'), | |
onPressed: () => showTextDialog(context), | |
); | |
} | |
} | |
class MyDialog extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
final myModel = context.watch<ServicesProvider>(); | |
return AlertDialog( | |
content: IntrinsicHeight( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: [ | |
Row( | |
children: [ | |
Radio( | |
value: '0', | |
groupValue: myModel.paymentMethod, | |
onChanged: (value) => myModel.setPaymentMethod('0'), | |
activeColor: Colors.blue, | |
toggleable: true, | |
), | |
const SizedBox(width: 5.0), | |
const Text("Radio Button 1"), | |
], | |
), | |
const SizedBox(height: 10.0), | |
Row( | |
children: [ | |
Radio( | |
value: '1', | |
groupValue: myModel.paymentMethod, | |
onChanged: (value) => myModel.setPaymentMethod('1'), | |
activeColor: Colors.blue, | |
toggleable: true, | |
), | |
const SizedBox(width: 5.0), | |
const Flexible(child: Text("Radio Button 2")), | |
], | |
), | |
], | |
), | |
), | |
); | |
} | |
} | |
class ServicesProvider extends ChangeNotifier { | |
String _paymentMethod = '0'; | |
String get paymentMethod => _paymentMethod; | |
void setPaymentMethod(String method) { | |
_paymentMethod = method; | |
notifyListeners(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment