Skip to content

Setting the starting index will work for buildItemDetail but will not work for buildListItem (Always starts Index 0) - onFocusItem #24

@ctonyc

Description

@ctonyc

Setting the _focusedIndex

int _focusedIndex = 4;

OnItemFocus

void _onItemFocus(int index) {
    setState(() {
      _focusedIndex = index;
    });
  }

Build Item Detail

    Widget _buildItemDetail() {
    List<Widget> list = [];
    String bullet = "\u2022 ";
    if (data.length > _focusedIndex) {
      var array = data[_focusedIndex]["data"];

      for(int i = 0; i < array.length; i++ ){
        list.add(const SizedBox(
          height: 15,
        ));
        list.add(Align(
          alignment: Alignment.centerLeft,
          child: Padding(
            padding: const EdgeInsets.fromLTRB(20,0,20,0),
            child: Row(
              children: [
                Text(
                  bullet,
                  style: const TextStyle(
                    fontSize: 25,
                    fontWeight: FontWeight.bold,
                  ),
                ),
                Expanded(
                  child: Text(
                      array[i],
                    style: const TextStyle(
                      fontSize: 16,
                    ),
                  ),
                ),
                ]

            ),
          ),
        )
        );
      }
      return Expanded(
        child: Column(
          children: list,
        ),
      );
    }

    return const SizedBox(
      height: 150,
      child: Text("No Data"),
    );
  }

Build List Item

Widget _buildListItem(BuildContext context,int index) {
    //horizontal

    if (index == data.length) {
      return const Center(
        child: CircularProgressIndicator(),
      );
    }

    return SizedBox(
      width: 150,
      child: Column(
        children: <Widget>[
          SizedBox(
            height: 200,
            width: 150,
            child: Column(
              children: [
                Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: Container(
                    color: Colors.lightBlue,
                    height: 126,
                    width: 126,
                  ),
                ),
                const SizedBox(height: 20),
                Text(
                data[index]['week'],
                style: const TextStyle(
                  fontSize: 20,
                )
              )
            ]
            ),
          )
        ],
      ),
    );
  }

Build Widget

    Widget build(BuildContext context) {
    return Scaffold(   
      body: Align(
        alignment: Alignment.center,
        child: Column(
          children: [
            SizedBox(
              height: 200,
              child: ScrollSnapList(
                onItemFocus: _onItemFocus,
                itemSize: 150,
                itemBuilder: _buildListItem,
                itemCount: data.length,
                dynamicItemSize: true,
                // key: sslKey,
                // dynamicSizeEquation: customEquation, //optional
              ),
            ),
            _buildItemDetail(),
          ],
        ),

      ),
    );
  }

Build list item will be on index 0 when it loads while build item detail will be on index 4. After clicking the scroll snaplist everything works fine as it sets it to correct index. Expectation was both to be on index 4.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions