Skip to content

Instantly share code, notes, and snippets.

@muirandy
Created October 24, 2018 07:48
Show Gist options
  • Save muirandy/b2f487ed01da60ecadf169959b3ac17d to your computer and use it in GitHub Desktop.
Save muirandy/b2f487ed01da60ecadf169959b3ac17d to your computer and use it in GitHub Desktop.
Flutter: Trying to get a Widget that scrolls in 2D.
import 'package:flutter/material.dart';
void main() {
runApp(new TestApp());
}
class TestApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
home: new TestAppHomePage(),
);
}
}
class TestAppHomePage extends StatefulWidget {
TestAppHomePageState createState() => new TestAppHomePageState();
}
class TestAppHomePageState extends State<TestAppHomePage> {
ScrollController _verticalScrollController;
ScrollController _horizontalScrollController;
@override
void initState() {
_verticalScrollController = new ScrollController();
_horizontalScrollController = new ScrollController();
}
@override
void dispose() {
_verticalScrollController.dispose();
_horizontalScrollController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return new Scaffold(
body: _buildNormalScrollView(),
// body: _buildGridScrollView(),
// body: _buildNestedScrollView(),
);
}
Widget _buildGridScrollView() {
return new GridView.builder(
primary: false,
controller: _horizontalScrollController,
scrollDirection: Axis.horizontal,
itemCount: 1,
itemBuilder: (BuildContext context, int index) {
_createContent();
},
gridDelegate:
new SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 4),
);
}
Widget _buildNormalScrollView() {
return new GridView.count(
primary: false,
controller: _horizontalScrollController,
scrollDirection: Axis.horizontal,
crossAxisCount: 1,
crossAxisSpacing: 20.0,
children: <Widget>[
SizedBox(
width: 6800.0,
child: _createContent(),
)
],
);
}
Widget _buildNestedScrollView() {
return NestedScrollView(
controller: _horizontalScrollController,
scrollDirection: Axis.horizontal,
headerSliverBuilder: (context, boxIsScrolled) {
return <Widget>[
SliverAppBar(
title: Text("Example"),
pinned: true,
floating: true,
forceElevated: boxIsScrolled,
)
];
},
body: SizedBox(
width: 2800.0,
child: Row(
children: <Widget>[
SizedBox(
width: 6800.0,
child: _createContent(),
)
],
),
),
);
}
Widget _createContent() {
return new SingleChildScrollView(
primary: false,
controller: _verticalScrollController,
scrollDirection: Axis.vertical,
child: new SizedBox(
height: 2300.0,
width: 2800.0,
child: new Row(
children: _createContentX(20),
),
),
);
}
List<Widget> _createContentX(int count) {
List<Widget> widgets = [];
for (int i = 0; i < count; i++) {
widgets.add(new Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: _createText(),
));
}
return widgets;
}
List<Widget> _createText() {
return <Widget>[
new Text("Line1"),
new Text("Line2"),
new Text("Line3"),
new Text("Line4"),
new Text("Line5"),
new Text("Line6"),
new Text("Line7"),
new Text("Line8"),
new Text("Line9"),
new Text("Line10"),
new Text("Line11"),
new Text("Line12"),
new Text("Line13"),
new Text("Line14"),
new Text("Line15"),
];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment