Skip to content

Instantly share code, notes, and snippets.

@filiph
Created March 31, 2021 19:54
Show Gist options
  • Save filiph/9d83065beddb641f12583d81c7ba14ac to your computer and use it in GitHub Desktop.
Save filiph/9d83065beddb641f12583d81c7ba14ac to your computer and use it in GitHub Desktop.
Scrollbar widget of the week
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primaryColor: Colors.white,
scrollbarTheme: ScrollbarThemeData(
// Uncomment the following line to style all the app's scrollbars.
// thumbColor: MaterialStateProperty.all(Colors.blue),
),
),
debugShowCheckedModeBanner: false,
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
bool _isAlwaysShown = true;
bool _showTrackOnHover = false;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Scrollbar'),
),
body: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Expanded(
child: Scrollbar(
isAlwaysShown: _isAlwaysShown,
showTrackOnHover: _showTrackOnHover,
child: ListView.builder(
itemCount: 20,
itemBuilder: (context, index) => MyItem(index),
),
),
),
Divider(height: 1),
Container(
color: Colors.white,
child: Padding(
padding: const EdgeInsets.all(20.0),
child: SizedBox(
width: double.infinity,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SwitchListTile(
value: _isAlwaysShown,
title: Text('Toggle isAlwaysShown'),
onChanged: (value) =>
setState(() => _isAlwaysShown = value),
),
SwitchListTile(
value: _showTrackOnHover,
title: Text('Toggle showTrackOnHover'),
onChanged: (value) =>
setState(() => _showTrackOnHover = value),
),
],
),
),
),
)
],
),
);
}
}
class MyItem extends StatelessWidget {
final int index;
const MyItem(this.index);
@override
Widget build(BuildContext context) {
final color = Colors.primaries[index % Colors.primaries.length];
final hexRgb = color.shade500.toString().substring(10, 16).toUpperCase();
return ListTile(
contentPadding: EdgeInsets.symmetric(horizontal: 20, vertical: 20),
leading: AspectRatio(
aspectRatio: 1,
child: Container(
color: color,
)),
title: Text('Material Color #${index + 1}'),
subtitle: Text('#$hexRgb'),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment