Change an entire Scaffold body

Asked

Viewed 142 times

0

Hello!

I’m trying to finalize an app, but I stopped in a problem.

I have a screen with a Scaffold and a action in appBar with widget that returns the date.

I would like to know how to update a text with the date inside the body. I know it can be updated. Yeah, I can print it and see it changes the value but I can’t change the text.

data file.Dart

class Data extends StatefulWidget {
  @override
  _DataState createState() => _DataState();
}

class _DataState extends State<Data> {
  StoreChamada testeData = StoreChamada();
  DateTime selectDate = DateTime.now();

  Future<Null> _selectdate(BuildContext context) async {

    final DateTime picked = await showDatePicker(
        context: context,
        initialDate: selectDate,
        firstDate: DateTime(2000, 1),
        lastDate: DateTime(2050));
    testeData.setData(DateFormat('dd-MM').format(picked));
    if (picked != null && picked != selectDate)
      setState(() {

        selectDate = picked;

      });
  }
  @override
  Widget build(BuildContext context) {
    var data = DateFormat('dd-MM').format(selectDate);
    return Row(children: <Widget>[
      Text(data + '  '),
      IconButton(
        icon: Icon(IconData(59670, fontFamily: 'MaterialIcons')),
        onPressed: () {
          setState(() {
            _selectdate(context);
          });
        },
      )
    ]);
  }
}

function I call to update the value of the variable sdata with the date I use on the body.

void setData(value) => sdata = value;

list screen that has the text.

 body: Container(
          child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Text(sdata == null
              ? '${DateFormat('dd-MM').format(DateTime.now())}'
              : sdata),

I tried several things, I thought about using a setstate(){} on the body but I don’t even know if that would be possible. Anyway, if anyone can help.

inserir a descrição da imagem aqui

That’s the screen I’d like to change. I have a widget with text and date that are updated as soon as I change the date, but I can’t update the yellow part which is where the data I update is located depending on the date. The date value is changed but the screen does not update, only when I hotreload.

  • It was not clear what the problem is, you need to update which scaffold? What is the structure of widgets? If you are instantiating your widget, you cannot use a callback?

  • The one with the text sdata, the last snippet of the code.

  • Ok, how are your widgets structured? You have a widget with a scaffold, and within this you have your widget to select the date?

  • Yes. I have a screen with a scaffold -> appBar(action-> widget that changes the date, the first snippet of code.

1 answer

2

I’m not sure I understand your problem, but if you need to pass some data from a child widget to a priest, there are different ways to do this, but the simplest is to simply pass a function to the child widget that will be called every time you want to run some code, something like:

Your child widget receives the function as a constructor parameter and will call it every event:

class SelectDate extends StatelessWidget {
  SelectDate({Key key, this.onChange}) : super(key: key);
  
  final Function(DateTime) onChange;
  
  @override
  Widget build(BuildContext context) {
    return IconButton(
        icon: Icon(Icons.calendar_today),
        onPressed: () {
          onChange?.call(DateTime.now());
        },
      );
  }
}

And then in your Parent Widget, in your Scaffold, when you create your Child Widget:

Scaffold(
  appBar: AppBar(
    title: Text(widget.title),
  ),
  body: Center(
    child: Column(
      children: [
        SelecteDate(
          onChange: (data) {
            /// Faça algo com a nova data
          }
        )
      ]
    ),
  ),
);

If this was your problem, it can be solved so anyway I saw that you are making unnecessary use of the method setState, I suggest you take a look at documentation about what these methods do and how to use them to your advantage. There are also several tutorials on the Flutter channel on youtube, one of which may be interesting for your case is the Inheritedwidget. And also Future<Null> where Null is not a valid type, if you have nothing to return from this function the correct is Future<void>

  • Thank you so much for the answer, I can’t test now but as soon as I test.

  • If it doesn’t, explain your problem better and I’ll see how I can help

  • Man, I actually put the setstate because I was trying everything rsrs. i have a widget that is the date and it draws im iconbutton and a text with the date. I put this widget in the appBar action. when I change the date I can even change the date of the text of this widget and the varive sdata only that I needed to redesign the screen to bring the firestrore information, when I change the date and donated hotreload it works rsrs.

  • From what I saw of your image, the code I put of example can be applied without any problem.

  • Dear thank you very much, I will try to use your solution, but I’m thinking of giving a studied in modular I think it will help me a lot, including in this solution.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.