Home > front end >  Can I update a parent state from within a Dialog in Flutter?
Can I update a parent state from within a Dialog in Flutter?

Time:10-29

I'm trying to build a preferences dialog which contains user options for theme, font etc. I've got the selection within the dialog working responsively, but setState() doesn't update the state of the parent from within the dialog (despite the fact that I named the StateSetter function for my StatefulBuilder "updateDialogState").

How can I resolve this?

Minimum example:

class WritingScreenState extends State<WritingScreen> {
  late List<Section> sections;

  @override
  void initState() {
    super.initState();
    sections = []
  }

  @override
  Widget build(BuildContext context) {
    WrittenTextTheme currentTheme = WrittenTextTheme.light;

    return Scaffold(
      appBar: AppBar(
        actions: [
          IconButton(
            icon: Icon(Icons.tune),
            tooltip: "Preferences",
            onPressed: () {
              showDialog(context: context, builder: (context) {
                return AlertDialog(
                  title: Text("Preferences"),
                  content: StatefulBuilder(
                    builder: (context, StateSetter updateDialogState) {
                      return Column(
                        mainAxisSize: MainAxisSize.min,
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Text("Theme"),
                          Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Flexible(
                                flex: 1,
                                child: InkWell(
                                  onTap: () {
                                    updateDialogState(() {
                                      currentTheme = WrittenTextTheme.light;
                                    });
                                    setState(() {
                                      currentTheme = WrittenTextTheme.light;                                        
                                    });
                                  },
                                  child: Container(
                                    height: 50, 
                                    decoration: BoxDecoration(
                                      color: Colors.white,
                                      border: currentTheme == WrittenTextTheme.light ?
                                        Border.all(color: Theme.of(context).primaryColor, width: 3) :
                                        Border.all(),
                                      borderRadius: BorderRadius.circular(6)
                                    ),
                                    child: Align(
                                      alignment: Alignment.center,
                                      child: Text(
                                        "Aa",
                                        style: TextStyle(
                                          fontSize: 28,
                                          color: Colors.grey[800],
                                        )
                                      ),
                                    ),
                                  ),
                                ),
                              ),
                              Flexible(
                                flex: 1,
                                child: InkWell(
                                  onTap: () {
                                    updateDialogState(() {
                                      currentTheme = WrittenTextTheme.dark;
                                    });
                                    setState(() {
                                      currentTheme = WrittenTextTheme.dark;                                        
                                    });
                                  },
                                  child: Container(
                                    height: 50,
                                    decoration: BoxDecoration(
                                      color: Colors.blueGrey[800],
                                      border: currentTheme == WrittenTextTheme.dark ?
                                        Border.all(color: Theme.of(context).primaryColor, width: 3) :
                                        Border.all(),
                                      borderRadius: BorderRadius.circular(6)
                                    ),
                                    child: Align(
                                      alignment: Alignment.center,
                                      child: Text(
                                        "Aa",
                                        style: TextStyle(
                                          fontSize: 28,
                                          color: Colors.grey[100],
                                        )
                                      ),
                                    ),
                                  ),
                                ),
                              ),
                            ]
                          ),
                        ],
                      );
                    }
                  ),
                  actions: [
                    ElevatedButton(
                      child: Text("SAVE"),
                      onPressed: () {
                        //TODO: save changes
                        Navigator.pop(context);
                      },
                    )
                  ],
                );
              }); 
            }
          ),
        ],
      ),
    );
  }
}

CodePudding user response:

setState is indeed working. The problem resides here, the code in the build method will again be initialized when setState is called.

@override
  Widget build(BuildContext context) {
WrittenTextTheme currentTheme = WrittenTextTheme.light;

Update your application logic so that on setState you don't loose the new value set.

  • Related