Created
March 31, 2021 19:54
-
-
Save filiph/9d83065beddb641f12583d81c7ba14ac to your computer and use it in GitHub Desktop.
Scrollbar widget of the week
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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