GotJimmy / accordion

Other
47 stars 44 forks source link

I have an issue when i added Textformfield in textformfield when i search item the accordian will closed automatically #52

Closed mdsrkhnafd closed 1 year ago

mdsrkhnafd commented 1 year ago

Capture

Please Solve this ASAP when i seacrh item it will closed automatically

GotJimmy commented 1 year ago

Please send me a screen recording video. You probably have the accordion wrapped in your state management, right?

alimtu commented 1 year ago

yes when i setState in AccordionSection content AccordionSection automatically closed

please fix this ASAP

mdsrkhnafd commented 1 year ago

Please send me a screen recording video. You probably have the accordion wrapped in your state management, right?

https://github.com/GotJimmy/accordion/assets/72373676/c633d8d1-c511-4325-a1f5-c06c5d8cefd2

GotJimmy commented 1 year ago

Did you wrap the whole Accordion or just your widgets within the first AccordionSection?

mdsrkhnafd commented 1 year ago

Did you wrap the whole Accordion or just your widgets within the first AccordionSection?

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

class PatientEmrScreen extends StatefulWidget { const PatientEmrScreen({Key? key}) : super(key: key);

@override State createState() => _PatientEmrScreenState(); }

class _PatientEmrScreenState extends State { TextEditingController complaintController = TextEditingController(); TextEditingController diagnosisController = TextEditingController(); // Your other controllers...

List _dataList1 = [ 'Apple', 'Banana', 'Cherry', ];

List _dataList2 = [ 'Carrot', 'Broccoli', 'Lettuce', ];

List _filteredData1 = []; List _filteredData2 = [];

String _selectedItem1 = ''; String _selectedItem2 = '';

String _selectedSuggestion1 = ''; String _selectedSuggestion2 = '';

void _filterData(String query, List data, TextEditingController controller) { setState(() { if (query.isEmpty) { if (controller == complaintController) { _filteredData1 = []; } else if (controller == diagnosisController) { _filteredData2 = []; } } else { if (controller == complaintController) { _filteredData1 = data .where((item) => item.toLowerCase().contains(query.toLowerCase())) .toList(); } else if (controller == diagnosisController) { _filteredData2 = data .where((item) => item.toLowerCase().contains(query.toLowerCase())) .toList(); } } }); }

void _selectItem(String item, TextEditingController controller) { setState(() { if (controller == complaintController) { _selectedItem1 = item; complaintController.text = item; _filteredData1 = []; _selectedSuggestion1 = item; } else if (controller == diagnosisController) { _selectedItem2 = item; diagnosisController.text = item; _filteredData2 = []; _selectedSuggestion2 = item; } }); }

void _saveSelectedItem1() { if (_selectedSuggestion1.isNotEmpty) { print('Selected item from List 1: $_selectedSuggestion1'); } else { print('No item selected from List 1'); } }

void _saveSelectedItem2() { if (_selectedSuggestion2.isNotEmpty) { print('Selected item from List 2: $_selectedSuggestion2'); } else { print('No item selected from List 2'); } }

final _headerStyle = const TextStyle( color: Color(0xffffffff), fontSize: 15, fontWeight: FontWeight.bold, );

@override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text('Patient EMR'), centerTitle: true, ), body: Accordion( maxOpenSections: 1, headerBackgroundColorOpened: Colors.blue, scaleWhenAnimating: true, headerPadding: const EdgeInsets.symmetric(vertical: 15, horizontal: 10), openAndCloseAnimation: true, children: [ AccordionSection( isOpen: false, header: Text('Presenting Complaint', style: _headerStyle), content: Column( children: [ Focus( onFocusChange: (hasFocus) { // This callback will be called when the focus changes }, child: TextFormField( controller: complaintController, onChanged: (value) { _filterData(value, _dataList1, complaintController); }, decoration: const InputDecoration( labelText: 'Search List 1', border: OutlineInputBorder(), ), ), ), if (_filteredData1.isNotEmpty) SizedBox( height: 500, child: ListView.builder( shrinkWrap: true, itemCount: _filteredData1.length, itemBuilder: (context, index) { final item = _filteredData1[index]; return ListTile( onTap: () { _selectItem(item, complaintController); }, title: Text(item), ); }, ), ), if (_selectedSuggestion1.isNotEmpty) ElevatedButton( onPressed: _saveSelectedItem1, child: const Text('Save 1'), ), const SizedBox( height: 15, ), ], ), ), AccordionSection( isOpen: false, header: Text( 'Diagnosis', style: _headerStyle, ), content: Column( children: [ Padding( padding: const EdgeInsets.all(16.0), child: Focus( onFocusChange: (value) {

                },
                child: TextFormField(
                  controller: diagnosisController,
                  onChanged: (value) {
                    _filterData(value, _dataList2, diagnosisController);
                  },
                  decoration: const InputDecoration(
                    labelText: 'Search List 2',
                    border: OutlineInputBorder(),
                  ),
                ),
              ),
            ),
            if (_filteredData2.isNotEmpty)
              SizedBox(
                height: 500,
                child: ListView.builder(
                  shrinkWrap: true,
                  itemCount: _filteredData2.length,
                  itemBuilder: (context, index) {
                    final item = _filteredData2[index];
                    return GestureDetector(
                      onTap: () {
                        _selectItem(item, diagnosisController);
                      },
                      child: ListTile(
                        title: Text(item),
                      ),
                    );
                  },
                ),
              ),
            if (_selectedSuggestion2.isNotEmpty)
              ElevatedButton(
                onPressed: _saveSelectedItem2,
                child: const Text('Save 2'),
              ),
            const SizedBox(
              height: 15,
            ),
          ],
        ),
      ),
    ],
  ),
);

} }

void main() { runApp(MaterialApp(home: PatientEmrScreen())); }

mdsrkhnafd commented 1 year ago

Did you wrap the whole Accordion or just your widgets within the first AccordionSection?

Please Solve the issue ASAP i am stuck

GotJimmy commented 1 year ago

I cannot solve an issue that is not the problem of Accordion!
My guess is that your StatefulWidget rebuilds the entire Accordion widget and thereby putting it in its original state (playing its opening animation). I would leave the Accordion in a StatelessWidget and extract the portion with the textfield and button into a separate widget with its own handling of the state. In short: you're rebuilding too much of your UI (the Accordion), not only the part that needs it (the textfield inside one AccordionSection).

https://github.com/GotJimmy/accordion/assets/6039735/6961ccda-e8c2-403b-8367-ff9a0a9400c9

mdsrkhnafd commented 1 year ago

I cannot solve an issue that is not the problem of Accordion! My guess is that your StatefulWidget rebuilds the entire Accordion widget and thereby putting it in its original state (playing its opening animation). I would leave the Accordion in a StatelessWidget and extract the portion with the textfield and button into a separate widget with its own handling of the state. In short: you're rebuilding too much of your UI (the Accordion), not only the part that needs it (the textfield inside one AccordionSection).

screenshot.mov

in a Textformfield i am searching data and then select the item and then press the button is not working how to handle see my code implement this type of code and Videos and Image that i have implement. You Only Impelement the simple TextFormField i Implement this type of field but when i am searching some data is not working

GotJimmy commented 1 year ago

Again, you need to extract everything from that one AccordionSection body into a separate widget that handles its own state as I suggested before. Have you tried that by now?

mdsrkhnafd commented 1 year ago

it cant work for me why this is closed