Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to pass the selected ListTile to another screen?

The page displays the US states as a list. And when you click on the ListTile, a checkmark appears next to the state. The user can only select one state. How to track the state on which the click was made and transfer it to another screen? Send on button click "Save".

List < UsaStatesModel > _statesList = [];
late final _checkedValue =
  List < bool > .generate(_statesList.length, (index) => false);

@override
Widget bodyWidget(BuildContext context) {
  return Scaffold(
    appBar: AppBar(
      title: 'States'
      actions: GestureDetector(
        onTap: () {},
        child: Icon(
          Icons.save,
          size: 26.0,
        ),
      ),
    ),
    body: BlocConsumer < BlocA, BlocAState > (
      listener: (context, state) {
        if (state is StatesLoadedState) {
          _statesList = state.states;
        }
      },
      builder: (context, state) {
        if (state is StatesLoadedState) {
          return Padding(
            padding: const EdgeInsets.only(top: 142),
              child: ListView.separated(
                itemCount: _statesList.length,
                itemBuilder: (context, index) {
                  return ListTile(
                    trailing: !_checkedValue[index] ?
                    const SizedBox.shrink(): Image.asset(
                        Assets.assetsCheckmark,
                        width: 13,
                        height: 10,
                      ),
                      title: Text(
                        _statesList[index].name,
                      ),
                      onTap: () => _checkState(index),
                  );
                },
                separatorBuilder: (context, index) {
                  return const Divider();
                },
              ),
          ),
        ),
      );
    }
  }
)
}

void _checkState(int index) {
  setState(
    () {
      for (var i = 0; i < _checkedValue.length; i++) {
        _checkedValue[i] = false;
      }
      _checkedValue[index] = true;
    },
  );
}
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Create a variable like this on top

int currentIndex = 0;

Whenever a list is selected update this index

void _checkState(int index) {
  setState(
    () {
      for (var i = 0; i < _checkedValue.length; i++) {
        _checkedValue[i] = false;
      }
      _checkedValue[index] = true;
     currentIndex = index; //update here
    },
  );
}

Now

_stateList[currentIndex]

Will give you the currently selected state

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading