get the position after setState in flutter

Viewed 17

Below is my simple example. After press the button, the container's position will change. Are there any methods to print the container's new position of dx and dy automatically after press the button?

class Data extends StatefulWidget {
  DataState createState() => DataState();
}

class DataState extends State<Data> {
  double height = 10.0;

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
        appBar: AppBar(
          leading: IconButton(
            icon: Icon(Icons.add),
            onPressed: () {
              setState(() {
                height += 10.0;
              });
            },
          ),
        ),
        body: Column(
          children: [
            SizedBox(
              height: height,
            ),
            Container(
              color: Colors.red,
              height: 25
            )
          ],
        ));
  }
}
2 Answers

You can use MediaQuery and Size

@override
  Widget build(BuildContext context) {
    Size size = MediaQuery.of(context).size;
    return Container(
      alignment: Alignment.center,
      height: size.height / 10,
      width: size.width,
      child: Text(
        text,
        style: TextStyle(fontSize: 20, color: color),
      ),
    );
  }
Related