как во флаттере выпадающее значение и значок, но установить только значение? - PullRequest
0 голосов
/ 04 апреля 2020

enter image description here

import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';

class ChatDropDown extends StatefulWidget {
  static const routeName = 'chat_dropdown';

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

class _ChatDropDownState extends State<ChatDropDown> {
  List<String> _locations = [
    'Gujarat',
    'Maharatra',
    'Delhi',
    'Hariyana',
    'Chennai',
  ];

  String _selectedLocation;
  bool selectCircle = false;

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.only(left: 20.0, right: 20.0, top: 20.0),
      child: Container(
        alignment: Alignment.center,
//        height:double.infinity,
        decoration: BoxDecoration(boxShadow: [
          BoxShadow(
            blurRadius: 1.5,
            spreadRadius: 2.0,
            offset: Offset(1.0, 1.0),
            color: Colors.grey[300].withOpacity(0.7),
          )
        ], color: Colors.white, borderRadius: BorderRadius.circular(15.0)),
        child: DropdownButtonFormField<String>(
          isExpanded: true,
          decoration: InputDecoration(
//              focusColor: ,
            border: UnderlineInputBorder(
              borderRadius: const BorderRadius.all(
                const Radius.circular(10.0),
              ),
              borderSide: BorderSide.none,
            ),
            fillColor: Colors.white,
            errorStyle: TextStyle(
              color: Colors.red,
              fontSize: 15.0,
            ),
            filled: true,
            contentPadding: EdgeInsets.only(left: 15.0, right: 22.0),
//              hintText: "location",
            hintStyle: TextStyle(
              color: Color.fromRGBO(142, 184, 201, 1.0),
              fontSize: 15.0,
            ),
//                contentPadding: EdgeInsets.only(left: 10.0),
//                border: InputBorder.none,
          ),
          icon: Icon(
            Icons.keyboard_arrow_down,
            size: 25.0,
            color: Color.fromRGBO(156, 193, 208, 1.0),
          ),
          iconSize: 24,
          elevation: 5,
          style: TextStyle(color: Colors.black, fontSize: 20.0),
          onChanged: (String value) {
            if (selectCircle == false) {
              selectCircle = false;
            } else {
              selectCircle = true;
            }

        setState(() {
          _selectedLocation = value;
        });
      },
      hint: Text("Select Location"),
      value: _selectedLocation,
      items: _locations.map<DropdownMenuItem<String>>((String location) {
        return DropdownMenuItem<String>(
          value: location,
          child: Padding(
            padding: const EdgeInsets.only(top: 5.0),
            child: Column(
              children: <Widget>[
                Padding(
                  padding: const EdgeInsets.all(10.0),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: <Widget>[
                      Text(
                        location,
                      ),
                      _selectedLocation == location
                          ?  SvgPicture.asset(
                        iconCheckList,
                        height: 24.0,
                      )
                          : SvgPicture.asset(
                        iconCheckListLight,
                        height: 24.0,
                      ),
                    ],
                  ),
                ),
                SizedBox(
                  height: 10.0,
                ),
                Divider(
                  height: 1.0,
                  color: Colors.grey,
                  thickness: 1.0,
                ),
              ],
            ),
          ),
        );
      }).toList(),
    ),
  ),
);
 }
   }

1 Ответ

0 голосов
/ 06 апреля 2020

Вы можете добавить selectitembuilder и добавить любой макет на элемент, который хотите показать.

Добавить следующий код.

 DropdownButtonFormField<String>(
        isExpanded: true,
        // added code
        selectedItemBuilder: (context) {
          return _locations.map((String location) {
            return DefaultTextStyle(
                style: TextStyle(), child: Text(location));
          }).toList();
        },
        // continue your original code
...