Can I update a parent state from within a Dialog in Flutter?

WalrusGumboot

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);
                      },
                    )
                  ],
                );
              }); 
            }
          ),
        ],
      ),
    );
  }
}

Ronak Jain

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.

Este artigo é coletado da Internet.

Se houver alguma infração, entre em [email protected] Delete.

editar em
0

deixe-me dizer algumas palavras

0comentários
loginDepois de participar da revisão

Artigos relacionados

How can I access md-dialog onComplete from within dialog controller?

update checkbox and return value from dialog in flutter

How can I update the data fetched from a future bilder in Flutter?

Flutter update state coming from another screen

How to call function of a parent from child and do not update the parent state?

StateError (Bad state: field does not exist within the DocumentSnapshotPlatform). How can I get a doc from firestore?

How can I export a state from a parent file to a child file in React js?

How can I update only part of the state

Vue.js Update child component data from within parent

Swift can't update view from parent

How To Update State In Parent Component From Child Component In React Native?

How can I update the model in parent scope from inside the child scope?

How do I change this.state of a component from parent?

Flutter: Update state of sibling widget

how to update parent state in child component and pass to parent state back?

How can I remove the action bar from a dialog

I want to update parent record from child records value in oracle

Flutter: How to set state of parent widget

Update state on Parent component from input in Child using props and useState Hook

How to update parent's component using state from child component in React?

How to update the correct state value in a parent component that gets its value from a child component using hooks in react?

Can can I avoid a TextInput losing focus when its parent's state changes?

Update parent state when Vuetify snackbar timeouts

How can I remove commas or whatever from within a string?

Can I use a roslyn analyzer from within the solution it is defined in?

How can I launch Nuke from within Maya?

How can I render a partial view from within a layout file?

How can I select the last element from a list within a for loop?

How can I raise an exception from within a Mysql trigger?

TOP lista

quentelabel

Arquivo