Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: some elements expanding upwards, some downwards #103

Open
lff5 opened this issue Jun 2, 2021 · 1 comment
Open

Bug: some elements expanding upwards, some downwards #103

lff5 opened this issue Jun 2, 2021 · 1 comment

Comments

@lff5
Copy link

lff5 commented Jun 2, 2021

Hi.
Video explains it all:
gif

I am expecting the panel to open downward, because if there is a text preview then at expanding the preview part would remain in place (that should be the default behavior), but in my app it mostly opens upward.

Why is 1-99 opening upward and 100 and up opening downward? Is it a bug?
Is there any way to control in which direction the expandable panel should open?

Code to reproduce.

scrollable_positioned_list: ^0.2.0-nullsafety.0
expandable: ^5.0.1

import 'package:flutter/material.dart';
import 'package:scrollable_positioned_list/scrollable_positioned_list.dart';
import 'dart:math';
import 'package:expandable/expandable.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: MyHomePage(),
    );
  }
}

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

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

class _MyHomePageState extends State<MyHomePage> {
  List<int> heights = [];
  Key key = UniqueKey();
  int startIndex = 100;
  final controller = ItemScrollController();

  @override
  void initState() {
    super.initState();
    Random random = new Random();
    for (int i = 0; i < 500; i++) heights.add(random.nextInt(100) + 30);
  }

  Widget itemBuilderInstant(BuildContext context, int index) {
    final controller = ExpandableController();
    return GestureDetector(
      onTap: controller.toggle,
      child: ExpandablePanel(
        controller: controller,
        collapsed: Container(
            color: index % 5 == 0 ? Colors.amber : Colors.white,
            height: heights[index].toDouble(),
            child: Text("Index $index, ${heights[index]}")),
        expanded: Container(
            color: index % 5 == 0 ? Colors.amber : Colors.white,
            height: heights[index].toDouble() * 2,
            child: Text("Index $index, ${heights[index]}")),
      ),
    );

  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      floatingActionButton: TextButton(
          onPressed: () {
            controller.scrollTo(
                index: Random().nextInt(heights.length),
                duration: Duration(milliseconds: 500));
            Future.delayed(Duration(milliseconds: 200)).then((value) {
              setState(() {
                key = UniqueKey();
                startIndex = Random().nextInt(heights.length);
              });
            });
          },
          child: Text("scroll and swap")),
      floatingActionButtonLocation: FloatingActionButtonLocation.centerTop,
      body: ScrollablePositionedList.builder(
          itemScrollController: controller,
          initialScrollIndex: startIndex,
          key: key,
          itemCount: heights.length,
          itemBuilder: itemBuilderInstant),
    );
  }
}

Flutter doc

[✓] Flutter (Channel stable, 2.0.6, on Mac OS X 10.15.7 19H524 darwin-x64, locale en-EE)
[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
[✓] Xcode - develop for iOS and macOS
[✓] Chrome - develop for the web
[✓] Android Studio (version 4.1)
[✓] VS Code (version 1.56.2)
[✓] Connected device (2 available)

@AlienKevin
Copy link

I encountered the exact same issue when using flutter-expandable in conjunction with scrollable_positioned_list. I found a solution by using another plugin called scroll-to-index instead of scrollable_positioned_list. Scroll-to-index is more flexible as it works on any scrollable widgets and it doesn't seem to interfere with flutter-expandable. See example code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants