Home > Net >  get the position after setState in flutter
get the position after setState in flutter

Time:11-09

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

CodePudding user response:

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

CodePudding user response:

If your Container or any widget is moving dynamically, you can get its location/position using Rect Getter

https://pub.dev/packages/rect_getter

  • Related