icemanbsi / searchable_dropdown

MIT License
107 stars 162 forks source link

```rubyModifying preselectedValue does not take effect #93

Open azhengyongqin opened 4 years ago

azhengyongqin commented 4 years ago

Use it in the following way... After modifying the preselectedValue in other positions, the selected value of the component remains unchanged

import 'package:flutter/material.dart';
import 'package:searchable_dropdown/searchable_dropdown.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: DemoPage(),
    );
  }
}

class DemoPage extends StatefulWidget {
  @override
  _DemoPageState createState() => _DemoPageState();
}

class _DemoPageState extends State<DemoPage> {
  String preselectedValue = "dolor sit";

  List<String> items = [
    "dolor sit",
    "sit",
    "dolor",
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: SearchableDropdown.single(
        isExpanded: true,
        value: preselectedValue,
        items: items
            .map(
              (e) => DropdownMenuItem(
                child: Text(e),
                value: e,
              ),
            )
            .toList(),
        onChanged: (value) {
          setState(() {
            preselectedValue = value;
          });
        },
      ),
    );
  }
}

i fix the code for show the dolor sit, make sure the item is on the value dropitem...

Screenshot_1595003004

_Originally posted by @rekiyagami in https://github.com/icemanbsi/searchable_dropdown/issues/85#issuecomment-660204493_

chaiyifam commented 4 years ago

because the value "preselectValue" is ref to "dolor sit" u need too make sure the value is null or place it in InitState() :D

chaiyifam commented 4 years ago

String preselectedValue ;

or

IniState(){ String preselectedValue = "dolor sit"; }