如何在flutter中有这样的滚动条视图?

时间:2019-04-28 12:23:47

标签: dart flutter scrollbar

enter image description here

我希望在一列中具有这样的滚动条视图。

那我该如何用颤振来获得它?

我尝试过

SingleChildScrollView(
            ....
          ),

但是没有滚动条出现,我也不知道要制作它们

2 个答案:

答案 0 :(得分:3)

Flutter 现在有 Scrollbar 小部件。

只需用 Scrollbar 包裹 SingleChildScrollView 或任何 ScrollView 小部件。

代码示例:

class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Scrollbar(
        // Child can also be SingleChildScrollView, GridView, etc.
        child: ListView.builder(
          itemCount: 20,
          itemBuilder: (context, index) {
            return ListTile(
              title: Text('Index $index'),
            );
          },
        ),
      ),
    );
  }
}

Screenshot

滚动条(本周小工具):https://youtu.be/DbkIQSvwnZc

答案 1 :(得分:0)

您可以使用this project

pubspec.yaml

dev_dependencies:
  flutter_test:
    sdk: flutter
  draggable_scrollbar: 0.0.4

代码:

import 'package:flutter/material.dart';
import 'package:draggable_scrollbar/draggable_scrollbar.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  ScrollController _rrectController = ScrollController();
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: "Test",
      home: Scaffold(
        body: Center(
          child: DraggableScrollbar.rrect(
            controller: _rrectController,
            backgroundColor: Colors.blue,
            child: ListView.builder(
              controller: _rrectController,
              itemCount: 100,
              itemExtent: 100.0,
              itemBuilder: (context, index) {
                return Container(
                  padding: EdgeInsets.all(8.0),
                  child: Material(
                    elevation: 4.0,
                    borderRadius: BorderRadius.circular(4.0),
                    color: Colors.green[index % 9 * 100],
                    child: Center(
                      child: Text(index.toString()),
                    ),
                  ),
                );
              },
            ),
          ),
        ),
      ),
    );
  }
}

结果: enter image description here