flutter 滚动到顶部

时间:2020-08-20

父组件用的是SingleChildScrollView

SingleChildScrollView(
                controller: scrollController,
                child: Column(
                  children: [
                    Html(
                      data: snapshot.data.content,
                      style: {
                        'p': Style(
                          padding: EdgeInsets.all(5),
                        ),
                      },
                    ),
                    Container(
                      child: FloatingActionButton(
                        child: IconButton(
                          icon: Icon(Icons.refresh),
                          onPressed: () {
                            getOne();
                            scrollController.jumpTo(scrollController.position.minScrollExtent);
                          },
                        ),
                        onPressed: () {},
                        tooltip: '再来一题',
                      ),
                      margin: EdgeInsets.fromLTRB(0, 0, 0, 10),
                    ),
                  ],
                ),
);

定义controller

ScrollController scrollController = ScrollController();

这段代码就是滚动到顶部

scrollController.jumpTo(scrollController.position.minScrollExtent); 

完整参考代码

import 'package:flutter/material.dart';
import 'package:flutter_html/flutter_html.dart';
import 'package:flutter_html/style.dart';
import 'package:toast/toast.dart';
import 'package:wyzda/Ajax.dart';
import 'package:wyzda/Work.dart';
import 'package:wyzda/bean/DaAnBean.dart';

class WorkState extends State<Work> {
  Future<DaAnBean> daan;
  String title = '加载中...';
  String content = '';
  ScrollController scrollController = ScrollController();

  @override
  void initState() {
    super.initState();
    getOne();
  }

  void getOne(){
    daan = Ajax().getOne(widget.id);
    if (daan != null) {
      daan.then((value) => {
        setState(() {
          title = value.title;
        })
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text(title),
        ),
        body: FutureBuilder(
          future: daan,
          builder: (context, snapshot) {
            if (snapshot.hasData) {
              return SingleChildScrollView(
                controller: scrollController,
                child: Column(
                  children: [
                    Html(
                      data: snapshot.data.content,
                      style: {
                        'p': Style(
                          padding: EdgeInsets.all(5),
                        ),
                      },
                    ),
                    Container(
                      child: FloatingActionButton(
                        child: IconButton(
                          icon: Icon(Icons.refresh),
                          onPressed: () {
                            getOne();
                            scrollController.jumpTo(scrollController.position.minScrollExtent);
                          },
                        ),
                        onPressed: () {},
                        tooltip: '再来一题',
                      ),
                      margin: EdgeInsets.fromLTRB(0, 0, 0, 10),
                    ),
                  ],
                ),
              );
            } else if (snapshot.hasError) {
              return Center(
                child: Text("${snapshot.error}"),
              );
            }
            return Center(
              child: CircularProgressIndicator(),
            );
          },
        ));
  }
}