1

For Example This is the First Dropdownbutton For Example This is the First Dropdown Sorry i dont have enough Reputation to post the images

Where the Tag will be Select A Region and Another one will be showing which will be the cities where the cities will be listed down there depends on the region selected above somewhat like that.

user10040349
  • 283
  • 3
  • 7
  • 16

1 Answers1

1

Each time you call setState the build method of your widget will be called and the visual tree gets reconstructed where needed. So, in the onChanged handler for your DropdownButton, save the selection in setState and conditionally add the second DropdownButton. Here's a working example (which may be a little rough around the edges :) ):

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  String _selectedRegion;
  String _selectedSecond;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text('Something before'),
            DropdownButton<String>(
              value: _selectedRegion,
              items: ['Arizona', 'California']
                  .map((region) => DropdownMenuItem<String>(
                      child: Text(region), value: region))
                  .toList(),
              onChanged: (newValue) {
                setState(() {
                  _selectedRegion = newValue;
                });
              },
            ),
            _addSecondDropdown(),
            Text('Something after'),
          ],
        ),
      ),
    );
  }

  Widget _addSecondDropdown() {
    return _selectedRegion != null
        ? DropdownButton<String>(
            value: _selectedSecond,
            items: ['First', 'Second']
                .map((region) => DropdownMenuItem<String>(
                    child: Text(region), value: region))
                .toList(),
            onChanged: (newValue) {
              setState(() {
                _selectedSecond = newValue;
              });
            })
        : Container(); // Return an empty Container instead.
  }
}

Luke Freeman has a great blog post about Managing visibility in Flutter if you need this in a more extensive/reusable way.

Derek Lakin
  • 16,179
  • 36
  • 51
  • is it possible that the value of _selectedRegion is for a parameter in a Uri.http( 'mainLink', 'path', {'region': '$_selectedRegion'}); so the requirement will be used as a parameter in fetching a data's for a dropdownbutton. – user10040349 Jul 31 '18 at 05:51
  • Yes, of course :) In the `onChanged` handler I'd call `setState` to change the UI to a "busy" state while you fetch the data. Once the network call completes, call `setState` again to change the UI to the final state. – Derek Lakin Jul 31 '18 at 09:19
  • is it possible to add a circular progress bar as he gets the data? – user10040349 Aug 02 '18 at 02:59
  • Yes. Use a flag that shows when the app is busy and in the `build` method create the UI you want for that busy state. When the network call completes, call `setState` and clear the flag. – Derek Lakin Aug 02 '18 at 06:17
  • 1
    Thanks <3, another question, lets say that the fetch or the $_selectedRegion returned nothing so there's no data populating the DropdownmenuItems so this error will pop up value == null || : items.where((DropdownMenuItem item) => item.value == value).length == 1': is not true is it possible to populate it with as No Region Available or is it possible just to disable the dropdownbutton or to put a function or a logic inside the items. where if the mapped or map returns null I will just populate it with a hardcoded data or a hint saying that theres no data or null – user10040349 Aug 02 '18 at 10:29
  • Yes, it's just another piece of information to check when building your widgets in the `build` method. A flag for busy determines whether to show your busy UI, then the length of the returned items to determine whether to populate the list or show an error/empty state. – Derek Lakin Aug 02 '18 at 11:22
  • Thaank you so much <3 Um one more clarification, please cite some example i can't grasp the idea about information checking on the build method if my idea is right its found on below or top level of Widget build(BuildContext context) { right? – user10040349 Aug 02 '18 at 11:30
  • This question (and it's answer) should help: https://stackoverflow.com/questions/47065098/how-work-with-progress-indicator-in-flutter – Derek Lakin Aug 02 '18 at 12:31
  • If I want the seconddropdown's list to be different depending on the firstdropdown, are there any tips to do so? There seems to be an error that occurs looking for the dropdown value if it is no longer in the list. See: https://stackoverflow.com/questions/61572284/how-to-resolve-flutter-dropdownbuttonformfield-dynamic-selection-checking-for-dr – Raycherr May 05 '20 at 13:33