LollolCat
LollolCat

Reputation: 23

How to align more sliders?

How could I align multiple sliders in flutter?

There's the screenshot of what I'm coding

Thanks in advance!

Upvotes: 0

Views: 459

Answers (2)

KuKu
KuKu

Reputation: 7492

Here is my suggestion what you want.

By wrapping each field with Expanded widget and
adding flex value to adjust alignment.

enter image description here

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: _buildBody(),
      floatingActionButton: FloatingActionButton(
        onPressed: () {},
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }

  Widget _buildBody() {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 20),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: [
          Row(
            children: [
              Expanded(
                flex: 1,
                child: Text(
                  'Red',
                  style: TextStyle(
                    fontSize: 20,
                    color: Colors.black,
                  ),
                ),
              ),
              Expanded(
                flex: 3,
                child: Slider(
                  value: 0.8,
                  inactiveColor: Colors.red,
                  activeColor: Colors.red,
                  onChanged: (value) {},
                ),
              ),
              Expanded(
                flex: 1,
                child: Text(
                  '202.0',
                  style: TextStyle(
                    fontSize: 20,
                    color: Colors.black,
                  ),
                ),
              ),
            ],
          ),
          Row(
            children: [
              Expanded(
                flex: 1,
                child: Text(
                  'Green',
                  style: TextStyle(
                    fontSize: 20,
                    color: Colors.black,
                  ),
                ),
              ),
              Expanded(
                flex: 3,
                child: Slider(
                  value: 0.7,
                  inactiveColor: Colors.green,
                  activeColor: Colors.green,
                  onChanged: (value) {},
                ),
              ),
              Expanded(
                flex: 1,
                child: Text(
                  '160.0',
                  style: TextStyle(
                    fontSize: 20,
                    color: Colors.black,
                  ),
                ),
              ),
            ],
          ),
          Row(
            children: [
              Expanded(
                flex: 1,
                child: Text(
                  'Blue',
                  style: TextStyle(
                    fontSize: 20,
                    color: Colors.black,
                  ),
                ),
              ),
              Expanded(
                flex: 3,
                child: Slider(
                  value: 0.6,
                  inactiveColor: Colors.blue,
                  activeColor: Colors.blue,
                  onChanged: (value) {},
                ),
              ),
              Expanded(
                flex: 1,
                child: Text(
                  '173.0',
                  style: TextStyle(
                    fontSize: 20,
                    color: Colors.black,
                  ),
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }
}

Upvotes: 1

Ankit Gada
Ankit Gada

Reputation: 194

Container(
      padding: EdgeInsets.all(16),
      child: Column(
        children: [
          Row(
            children: [
              Container(width: 40, child: Text("Red")),
              Expanded(
                flex: 2,
                child: Slider(
                  value: 0.2,
                  onChanged: (value) {},
                ),
              ),
              Container(width: 40, child: Text("250")),
            ],
          ),
          Row(
            children: [
              Container(width: 40, child: Text("Green")),
              Expanded(
                flex: 2,
                child: Slider(
                  value: 0.2,
                  onChanged: (value) {},
                ),
              ),
              Container(width: 40, child: Text("250")),
            ],
          ),
          Row(
            children: [
              Container(width: 40, child: Text("Red")),
              Expanded(
                flex: 2,
                child: Slider(
                  value: 0.2,
                  onChanged: (value) {},
                ),
              ),
              Container(width: 40, child: Text("250")),
            ],
          ),
        ],
      ),
    )

This can be the possible way you have to give fixed height to surrounded widgets.

Upvotes: 1

Related Questions