Skip to content

DraggableScrollableSheet reset to initialChildSize when setState() is called #99508

@vandir

Description

@vandir

The problem is caused by setState(). Is it a bug?

Steps to Reproduce

Run the sample app (dartpad) and click the floating button.

Sample app (click to expand)
import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: MyHomePage(),
    );
  }
}

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

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final List<Color> colors = [
    Colors.green.shade400,
    Colors.green.shade900,
  ];
  late DraggableScrollableController _controller;

  @override
  void initState() {
    _controller = DraggableScrollableController();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: DraggableScrollableSheet(
        controller: _controller,
        initialChildSize: 1,
        minChildSize: 0,
        maxChildSize: 1,
        builder: (context, controller) {
          return Container(
            color: Colors.blueGrey.shade100,
            child: ListView.builder(
              controller: controller,
              itemCount: 30,
              itemBuilder: (context, index) {
                return Container(
                  margin: const EdgeInsets.fromLTRB(20, 10, 20, 0),
                  height: 30,
                  color: colors[index % colors.length],
                  child: Center(child: Text('$index')),
                );
              },
            ),
          );
        },
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () async {
          await _controller.animateTo(
            _controller.size - 0.2,
            duration: const Duration(seconds: 3),
            curve: Curves.linear,
          );
          setState(() {});
        },
        child: const Icon(Icons.expand_more),
      ),
    );
  }
}

Expected results:
The DraggableScrollableSheet should not change its size after the animation ends.

Actual results:
The DraggableScrollableSheet reset its size to initialChildSize after the animation ends.

Metadata

Metadata

Assignees

No one assigned

    Labels

    r: invalidIssue is closed as not valid

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions