0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

[Flutter]ドロップダウンで選択中の文字色だけ変える

Posted at

はじめに

FlutterのDropdownButtonを利用して、ドロップダウンメニューが表示されているときのみ、選択中のアイテムの文字色を変更します。
下記が今回実装したパーツの動作となります。
demo_dropdown.gif

実装に関してはサンプルコードベースで簡単に実装しました。
Flutterはまだまだ初心者なのでベスプラなどあれば優しくご指摘ください。

実装方法

具体的にはDropdownButtonのオプションとして用意されているselectedItemBuilderを活用することで実装できます。

※コードはFlutter公式ドキュメント(DropdownButton classのソースコードを一部変更する形で記述しています。

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

const List<String> list = <String>['One', 'Two', 'Three', 'Four'];

void main() => runApp(const DropdownButtonApp());

class DropdownButtonApp extends StatelessWidget {
  const DropdownButtonApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: const Text('DropdownButton Sample')),
        body: const Center(
          child: DropdownButtonExample(),
        ),
      ),
    );
  }
}

class DropdownButtonExample extends StatefulWidget {
  const DropdownButtonExample({super.key});

  @override
  State<DropdownButtonExample> createState() => _DropdownButtonExampleState();
}

class _DropdownButtonExampleState extends State<DropdownButtonExample> {
  String dropdownValue = list.first;

  @override
  Widget build(BuildContext context) {
    return DropdownButton<String>(
      value: dropdownValue,
      icon: const Icon(Icons.arrow_downward),
      elevation: 16,
      style: const TextStyle(color: Colors.deepPurple),
      underline: Container(
        height: 2,
        color: Colors.deepPurpleAccent,
      ),
      onChanged: (String? value) {
        // This is called when the user selects an item.
        setState(() {
          dropdownValue = value!;
        });
      },
      items: list.map<DropdownMenuItem<String>>((String value) {
        return DropdownMenuItem<String>(
          value: value,
          child: Text(
            value,
            style: TextStyle(
                color: value == dropdownValue
                    ? Colors.redAccent /// 選択中の文字色だけ変更する。
                    : Colors.deepPurpleAccent),
          ),
        );
      }).toList(),
      selectedItemBuilder: (context) {
        return list.map<DropdownMenuItem<String>>((String value) {
          return DropdownMenuItem<String>(
            value: value,
            child: Text(
              value,
              style: const TextStyle(
                color: Colors.deepPurpleAccent, /// ドロップダウンメニューを表示していないときの文字色はselectedItemBuilderで個別に定義する。
              ),
            ),
          );
        }).toList();
      },
    );
  }
}

さいごに

今回はドロップダウンの細かい設定を変える例として、ドロップダウンメニューを表示しているときのみ選択しているアイテムの文字色を変更する方法を紹介しました。
Flutterはまだまだ学習中ですが、簡単にでもアウトプットしていきます。

0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?