7

Is it possible to change the default position of the item within a CupertinoPicker to the center between the two lines? The default position seems to be rather towards the upper line.

This is the code to reproduce the CupertinoPicker

import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: MyHomePage(),
    );
  }
}


class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: CupertinoPicker(
        itemExtent: 50,
        onSelectedItemChanged: (int index) {
          print(index);
        },
        children: <Widget>[
          Text("Item 1"),
          Text("Item 2"),
          Text("Item 3"),
        ],
      ),
    );
  }
}

This it what it looks like:

enter image description here

Thanks for any tips, hints and advice!

Community
  • 1
  • 1
Jonathan Rhein
  • 1,616
  • 3
  • 23
  • 47

2 Answers2

12

Wrap your children widgets with a Center widget:

CupertinoPicker(
  itemExtent: 50,
  onSelectedItemChanged: (int index) {
    print(index);
  },
  children: <Widget>[
    Center(child: Text("Item 1")),
    Center(child: Text("Item 2")),
    Center(child: Text("Item 3")),
  ],
),
J. S.
  • 8,905
  • 2
  • 34
  • 44
0

If Center does not work use Align widget.

Align(
      alignment: Alignment.center,
      child: Text("Item 1"),
),
Jay Parikh
  • 1
  • 1
  • 3