skip to Main Content

When creating a stateless widget, the constructor is const

class MyWidget extends StatelessWidget {
  const MyWidget({super.key});

  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      child: ListView(
      ),
    );
  }
}

If I add a ScrollController(), then it is not a const anymore

class MyWidget extends StatelessWidget {
  final ScrollController _scrollController = ScrollController();
  MyWidget({super.key});

  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      controller: _scrollController,
      child: ListView(
        controller: _scrollController,
      ),
    );
  }
}

Does it has an important impact on performances, as it is still a stateless widget. const in general really increase performances as it creates a blueprint that can be reused. Do I have a better solution performance-wise for my widget with ScrollController ?

Thank you

2

Answers


  1. Chosen as BEST ANSWER

    Adding as a required argument does not change anything because it will required to ba called as not a const

    class MyWidget extends StatelessWidget {
      final ScrollController scrollController;
      const MyWidget({
        required this.scrollController,
        super.key,
      });
    
      @override
      Widget build(BuildContext context) {
        return SingleChildScrollView(
          controller: scrollController,
          child: ListView(
            controller: scrollController,
          ),
        );
      }
    }
    
    MyWidget(scrollController: ScrollController()) //no const
    

  2. If you are that concerned over the const keyword, you can "lift up state".

    Meaning, refactor the code to pass the controller one widget to another:

    class Parent extends StatefulWidget {
      const Parent({Key? key}) : super(key: key);
    
      @override
      _ParentState createState() => _ParentState();
    }
    
    class _ParentState extends State<Parent> {
      late final ScrollController _scrollController;
    
      @override
      Widget build(BuildContext context) {
        return Child(
          scrollController: _scrollController,
        );
      }
    
      @override
      void dispose() {
        _scrollController.dispose();
        super.dispose();
      }
    
      @override
      void initState() {
        super.initState();
        _scrollController = ScrollController();
      }
    }
    
    

    And pass it to the child:

    class Child extends StatelessWidget {
      final ScrollController scrollController;
    
      const Child({Key? key, required this.scrollController}) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return SingleChildScrollView(
          controller: scrollController,
          child: ListView(
            controller: scrollController,
          ),
        );
      }
    }
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search