LoginSignup
1
0

More than 3 years have passed since last update.

FlutterでiOSのSegmented ControlのようなUIを作る

Posted at

はじめに

業務でiOSのSegmented ControlのようなUIを作成したので、備忘録代わりにまとめてみます。
MaterialSegmentedControlを使いました.
また、よりiOSらしくするCupertinoSegmentedControlも試してみました。

動作イメージ

スクリーンショット 2021-01-31 17.07.30.png

実装

_currentSelectionで選択したindexを持っておきます。
disabledChildrenに値を入れると、選択不可になります。

class _OrderSelectorState
    extends State<OrderSelector> {
  int _currentSelection = 0;

  @override
  Widget build(BuildContext context) => MaterialSegmentedControl(
    children: const {
      0: Text('First'),
      1: Text('Second'),
      2: Text('Third'),
      3: Text('Fourth'),
    },
    selectionIndex: _currentSelection,
    borderColor: Colors.grey,
    selectedColor: Colors.red,
    unselectedColor: Colors.yellow,
    borderRadius: 8,
    horizontalPadding: const EdgeInsets.all(16),
    disabledChildren: const [3],
    onSegmentChosen: (int index) {
      print('selected : $index');
      setState(() {
        _currentSelection = index;
      });
    },
  );
}

よりiOSらしく

CupertinoSegmentedControlを使った場合の動作イメージも載せておきます。
実装はほぼ同じでした。
スクリーンショット 2021-01-31 17.19.52.png

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0