Flutter(80):Scroll組件之SingleChildScrollView

Flutter教學(xué)目錄持續(xù)更新中

Github源代碼持續(xù)更新中

1.SingleChildScrollView介紹

有一個子widget的可滾動的widget,子內(nèi)容超過父容器時可以滾動。

2.SingleChildScrollView屬性

  • scrollDirection = Axis.vertical:滑動方向
  • reverse = false:是否倒序
  • padding:內(nèi)邊距
  • primary:當(dāng)內(nèi)容不足以滾動時,是否支持滾動;
  • physics:控制用戶滾動視圖的交互
  • controller:滑動控制器
  • child:

3.使用

  ScrollController _scrollController;

  @override
  void initState() {
    _scrollController = ScrollController();
    _scrollController.addListener(() {
      print('_scrollController ${_scrollController.offset}');
    });
    super.initState();
  }

  _myChildren() {
    return [
      Container(
        height: 300,
        color: Colors.blue,
      ),
      Container(
        height: 300,
        color: Colors.yellow,
      ),
      Container(
        height: 300,
        color: Colors.red,
      ),
      Container(
        height: 300,
        color: Colors.green,
      ),
    ];
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('SingleChildScrollView'),
      ),
      body: SingleChildScrollView(
        controller: _scrollController,
        reverse: true,
        child: ListBody(
          children: _myChildren(),
        ),
        restorationId: 's1',
      ),
    );
  }

  @override
  void dispose() {
    _scrollController.dispose();
    super.dispose();
  }
image.png

Flutter教學(xué)目錄持續(xù)更新中

Github源代碼持續(xù)更新中

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容