scrolling to the top when typing on TextField on Flutter

Issue

I added TextFormField on SliverPersistentHeaderDelegate. After scroll to the middle/bottom and then typing on to the textField, SliverList automatically come to the top. How do I disable this?

Full Code.

class MyWidget extends StatelessWidget {
  static const String route = '/myWidget';

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Container(
            child: CustomScrollView(
              slivers: <Widget>[
                SliverPersistentHeader(
                  pinned: true,
                  delegate: MyDynamicHeader(),
                ),
                SliverList(
                    delegate: SliverChildBuilderDelegate((BuildContext context, int index) {
                      return Container(
                        height: 200,
                        color: Color(Random().nextInt(0xffffffff)),
                      );
                    },
                    )
                )
              ],
            )
        )
    );
  }
}

class MyDynamicHeader extends SliverPersistentHeaderDelegate {
  int index = 0;

  @override
  Widget build(BuildContext context, double shrinkOffset, bool overlapsContent) {
    return LayoutBuilder(
        builder: (context, constraints) {
          final Color color = Colors.primaries[index];
          final double percentage = (constraints.maxHeight - minExtent)/(maxExtent - minExtent);

          if (++index > Colors.primaries.length-1)
            index = 0;

          return Container(
            decoration: BoxDecoration(
                boxShadow: [BoxShadow(blurRadius: 4.0, color: Colors.black45)],
                gradient: LinearGradient(
                    colors: [Colors.blue, color]
                )
            ),
            height: constraints.maxHeight,
            child: SafeArea(
                child: Center(
                  child: Row(
                    children: <Widget>[
                      CircularProgressIndicator(
                        value: percentage,
                        valueColor: AlwaysStoppedAnimation<Color>(Colors.white),
                      ),
                      InkWell(
                        onTap: (){
                          print('is working');
                        },
                        child: Container(
                          height: 50,
                          width: 100,
                          color: Palette.orelPay,
                        ),
                      ),
                      Expanded(child: TextFormField(
                        //controller: searchController,
                        decoration: InputDecoration(
                            contentPadding:
                            EdgeInsets.fromLTRB(20.0, 15.0, 20.0, 15.0),
                            hintText: "Search on OrelBuy",
                            border: OutlineInputBorder(
                                borderSide:
                                BorderSide(color: Palette.background, width: 32.0),
                                borderRadius: BorderRadius.circular(50.0)),
                            fillColor: Palette.background,
                            filled: true,
                            enabledBorder: OutlineInputBorder(
                                borderSide:
                                BorderSide(color: Palette.background, width: 32.0),
                                borderRadius: BorderRadius.circular(50.0)),
                            focusedBorder: OutlineInputBorder(
                                borderSide:
                                BorderSide(color: Palette.background, width: 32.0),
                                borderRadius: BorderRadius.circular(50.0))),
                      ),)
                    ],
                  ),
                )
            ),
          );
        }
    );
  }

  @override
  bool shouldRebuild(SliverPersistentHeaderDelegate old) => false;

  @override
  double get maxExtent => 250.0;

  @override
  double get minExtent => 80.0;
}

Solution

you can use physics of CustomScrollView widget to control scroll.

CustomScrollView(
      physics: PageScrollPhysics(), // added
      slivers: <Widget>[

Answered By – Viren V Varasadiya

Answer Checked By – David Goodson (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *