Future<void> ChangeProfile(BuildContext context) {
final pro = Provider.of<Pro>(context, listen: false);
FirebaseFirestore fireStore = FirebaseFirestore.instance;
US(context);
int count = 0;
return showDialog<void>(
context: context,
builder: (context) {
return StatefulBuilder(
builder: (BuildContext context, StateSetter setState) {
return AlertDialog(
// RoundedRectangleBorder - Dialog 화면 모서리 둥글게 조절
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10.0)),
//Dialog Main Title
title: Column(
children: <Widget>[
SizedBox(
width: 50.w,
height: 50.h,
child: Image.asset(
pro.currentProfile
),
)
],
),
//
content: SizedBox(
width: 400.w,
height: 400.h,
child: Column(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
PrintProfile(1,"imagename");
This is my AlertDialog code I used Statefu
lBuilder to change the screen inside the AlertDialog too
class PrintProfile extends StatefulWidget {
PrintProfile(this.num,this.text);
final int num;
final String text;
@override
State<PrintProfile> createState() => _PrintProfileState();
}
class _PrintProfileState extends State<PrintProfile> {
@override
Widget build(BuildContext context) {
ParentState parent = context.findAncestorStateOfType<ParentState>();
final int num = widget.num;
final String text = widget.text;
final pro = Provider.of<Pro>(context);
final double tophe = MediaQuery.of(context).padding.top;
US(context);
return SizedBox(
width: 70.w,
height: 70.h,
child: OutlinedButton(
onPressed: (){
setState(() {
pro.currentProfile= text;
});
},
child: Image.asset(text),
),
);
}
}
PrintProfile widget. When this widget is called from AlertDialog, the output part of AlertDialog, pro.currentProfile, must be changed and the parent AlertDialog setstate must be set.
ParentState parent = context.findAncestorStateOfType<ParentState>();
I found such a solution, but since the parent widget is an AlertDialog, I have to use another method. But I don't know.
Summary of Question: I want to make the setstate run in the parent AlertDialog when a button is pressed in the child widget in the AlertDialog using StatefulBuilder.
CodePudding user response:
This class will give you clear answer for use parent class setstate from current class, the same method you can do for your AlertDialog class
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'main1.dart';
void main() {
runApp(MaterialApp(
home: Modalbtn(),
));
}
class Modalbtn extends StatefulWidget {
@override
_ModalbtnState createState() => _ModalbtnState();
}
class _ModalbtnState extends State<Modalbtn> {
String value = "0";
// Pass this method to the child page.
void _update(String newValue) {
setState(() => value = newValue);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Column(
children: [
IconButton(
onPressed: () {
showModalBottomSheet(
context: context,
builder: (BuildContext context) {
return Container(
height: 200,
child: Column(
children: [StatefulModalbtn(update: _update)],
),
);
});
},
icon: Icon(Icons.add),
iconSize: 20,
),
Text(
value,
style: TextStyle(fontSize: 40),
),
],
),
),
);
}
}
import 'package:flutter/material.dart';
class StatefulModalbtn extends StatelessWidget {
final ValueChanged<String> update;
StatefulModalbtn({required this.update});
@override
Widget build(BuildContext context) {
return ElevatedButton(
onPressed: () => update("100"), // Passing value to the parent widget.
child: Text('Update (in child)'),
);
}
}