Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter position fixed equivalent

Tags:

flutter

dart

Is it possible to fix an object in the screen that stays fixed regardless of scrolling?

Something similar to CSS position fixed.

like image 871
Axel Márquez Avatar asked Mar 30 '18 00:03

Axel Márquez


People also ask

How do you give an absolute position in flutter?

To specify an absolute position for a widget as x-y coordinates, nest it in a Positioned widget that is itself nested in a Stack widget. Positioned gives you access to properties like top , left , right , and bottom , similar to CSS.

How do you fix the position of a container in flutter?

Here's how you do it:Step 1: Wrap the Stack's child widget inside the Position widget. Step 2: Inside the Position widget, add the top , right , bottom , left property and give it a value. For example, setting top:15 and right:0 will position a widget on the top right of your screen with 15 px space from the top.

How do you use the position in flutter?

Positioned is a widget that comes built-in with flutter SDK. Postioned does exactly what it sounds like, which is it arbitrarily positioned widgets on top of each other. It is usually used to position child widgets in Stack widget or similar. It only works for Stateless and Stateful widgets.


2 Answers

You can absolutely position a child of a Stack widget using the Positioned widget.

The minimal example below places the red box above the list view, by placing the child in a Positioned widget after the ListView in the Stack's children.

List<String> todos = [...]; return new Stack(   children: <Widget>[     new ListView(      children: todos        .map((todo) => new ListTile(title: new Text(todo)))        .toList(),      ),      new Positioned(        left: 30.0,        top: 30.0,        child: new Container(          width: 100.0,          height: 80.0,          decoration: new BoxDecoration(color: Colors.red),          child: new Text('hello'),         )       ),    ], ); 

And here it is inside of a Scaffold body. If you add more items you'll find that the list scrolls without moving the red box.

an absolutely positioned example

like image 177
Jonah Williams Avatar answered Sep 27 '22 15:09

Jonah Williams


You could use Positioned widget in a Stack Widget with AspectRatio widget and use the % distance like the below code.

@override Widget build(BuildContext context) { Size size = MediaQuery.of(context).size; //get the screen size  List<String> todos = [...];  //the below if to get the aspect ratio of the screen i am using the app only in landscape //if you need to use it in portrait you should add the sizes below if((size.width / size.height) > 1.76){   aspect = 16 / 9; }else if((size.width / size.height) < 1.77 && (size.width / size.height) >= 1.6){   aspect = 16 / 10; }else{   aspect = 4 /3; }  return new Scaffold(   body: new Center(       //layoutBuilder i can use the constraints to get the width and height of the screen       child: new LayoutBuilder(builder: (context, constraints) {         return new AspectRatio(           aspectRatio: aspect,           child: new Column(             children: <Widget>[                new ListView(                     children: todos                     .map((todo) => new ListTile(title: new Text(todo)))                     .toList(),                ),                new Positioned(                     //constraints.biggest.height to get the height                      // * .05 to put the position top: 5%                     top: constraints.biggest.height * .05,                     left: constraints.biggest.width * .30,                     child: new Container(                               width: 100.0,                               height: 80.0,                               decoration: new BoxDecoration(color: Colors.red),                               child: new Text('hello'),                           ),                     ),             ],           ),         ),       }),     ),   ); } } 

Hope it will help you....

like image 39
JLouage Avatar answered Sep 27 '22 17:09

JLouage