How do I jump to a section in a list?
I created a list of drinks and below the list is the category of all the drink types, so I want to jump to a specific section on the list by the relevant button. I want to jump to the Gin section on the list when I press the Gin button and to the Whiskey section on the list when I press the whiskey button
The List implementation class
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:pass_data/cartitem.dart';
import 'package:pass_data/ginCocktails.dart';
import 'package:pass_data/main_drawer.dart';
import 'package:provider/provider.dart';
import 'cart.dart';
import 'dish_object.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider(
create: (context) => cartitem(),
child: MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData(
backgroundColor: Colors.green,
),
home: MyHomePage(title: "The Wing Republic"),
),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
List<Drinks> _dishes = List<Drinks>();
List<Drinks> _cartList = List<Drinks>();
@override
void initState() {
super.initState();
_populateDishes();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
actions: <Widget>[
Padding(
padding: const EdgeInsets.only(right: 16.0, top: 8.0),
child: GestureDetector(
child: Stack(
alignment: Alignment.topCenter,
children: <Widget>[
Icon(
Icons.shopping_basket,
size: 35.0,
),
if (_cartList.length > 0)
Padding(
padding: const EdgeInsets.only(left: 2.0),
child: CircleAvatar(
radius: 8.0,
backgroundColor: Colors.red,
foregroundColor: Colors.white,
child: Text(
_cartList.length.toString(),
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 12.0,
),
),
),
),
],
),
onTap: () {
if (_cartList.isNotEmpty)
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => Cart(_cartList),
),
);
setState(() {
if (_cartList.isEmpty) {
Fluttertoast.showToast(
msg: "Cart Empty, Add items to Cart",
toastLength: Toast.LENGTH_LONG,
gravity: ToastGravity.BOTTOM,
timeInSecForIosWeb: 1,
backgroundColor: Colors.red,
textColor: Colors.white,
fontSize: 16.0);
}
});
},
),
),
],
backgroundColor: Colors.redAccent[100],
),
drawer: MainDrawer(),
body: Container(
child: SingleChildScrollView(
child: Align(
alignment: Alignment.center,
child: Column(mainAxisSize: MainAxisSize.min, children: <
Widget>[
Container(
width: 1000,
height: 500,
child: GridView.builder(
padding: const EdgeInsets.all(4.0),
gridDelegate:
SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2),
itemCount: _dishes.length,
itemBuilder: (context, index) {
var item = _dishes[index];
return Card(
elevation: 4.0,
child: Stack(
fit: StackFit.loose,
alignment: Alignment.center,
children: <Widget>[
Column(
mainAxisAlignment:
MainAxisAlignment.center,
children: <Widget>[
Text(item.category),
Text(
item.brandName,
textAlign: TextAlign.center,
style: Theme.of(context)
.textTheme
.subhead,
),
Text(
"R" + item.price.toString(),
textAlign: TextAlign.right,
)
],
),
Padding(
padding: const EdgeInsets.only(
right: 8.0,
bottom: 8.0,
),
child: Align(
alignment: Alignment.bottomRight,
child: GestureDetector(
child: (!_cartList.contains(item))
? Icon(
Icons.add_circle,
color: Colors.green,
)
: Icon(
Icons.remove_circle,
color: Colors.red,
),
onTap: () {
setState(() {
if (!_cartList.contains(item))
_cartList.add(item);
else
_cartList.remove(item);
});
},
),
),
),
],
));
})),
Container(
margin: EdgeInsets.symmetric(vertical: 20.0),
height: 40.0,
width: 500,
child: ListView(
scrollDirection: Axis.horizontal,
children: <Widget>[
RaisedButton(
child: Text("GIN COCKTAILS"),
),
SizedBox(
width: 5,
),
RaisedButton(
child: Text("WHISK(e)Y COCKTAILS"),
),
SizedBox(
width: 5,
),
RaisedButton(
child: Text("COGNAC / BRANDY COCKTAILS"),
),
SizedBox(
width: 5,
),
RaisedButton(
child: Text("RUM COCKTAILS"),
),
SizedBox(
width: 5,
),
RaisedButton(
child: Text("TEQUILLA COCKTAILS"),
),
SizedBox(
width: 5,
),
RaisedButton(
child: Text("VODKA COCKTAILS"),
),
SizedBox(
width: 5,
),
RaisedButton(
child: Text("MOCKTAILS"),
),
SizedBox(
width: 5,
),
RaisedButton(
child: Text("SHOOTERS"),
),
SizedBox(
width: 5,
),
RaisedButton(
child: Text("BEERS & CIDERS"),
),
SizedBox(
width: 5,
),
RaisedButton(
child: Text("VODKA"),
),
SizedBox(
width: 5,
),
RaisedButton(
child: Text("LIQUEURS"),
),
SizedBox(
width: 5,
),
RaisedButton(
child: Text("WINE BY THE GLASS"),
),
SizedBox(
width: 5,
),
RaisedButton(
child: Text("WHITE WINE"),
),
SizedBox(
width: 5,
),
RaisedButton(
child: Text("RED WINE"),
),
SizedBox(
width: 5,
),
RaisedButton(
child: Text("MOSCATO | ROSE/BLUSH"),
),
SizedBox(
width: 5,
),
RaisedButton(
child: Text("BUBBLES"),
),
SizedBox(
width: 5,
),
RaisedButton(
child: Text("MCC"),
),
SizedBox(
width: 5,
),
RaisedButton(
child: Text("CHAMPAGNE"),
),
SizedBox(
width: 5,
),
RaisedButton(
child: Text("COGNAC"),
),
SizedBox(
width: 5,
),
RaisedButton(
child: Text("TEQUILLA"),
),
SizedBox(
width: 5,
),
RaisedButton(
child: Text("NON-ALCOHOLIC"),
),
]))
])))));
}
void _populateDishes() {
var list = <Drinks>[
Drinks(
category: 'Gin Cocktails',
brandName: "Clover Club",
price: 65,
),
Drinks(
category: 'Gin Cocktail',
brandName: 'Tanqueray Sling',
price: 99,
),
Drinks(
category: 'Gin Cocktail',
brandName: 'French 75',
price: 84,
),
Drinks(
category: 'Gin Cocktail',
brandName: '1934 Cosmo',
price: 85,
),
Drinks(
category: 'Gin Cocktail',
brandName: "Sevilla Roseate",
price: 80,
),
Drinks(
category: 'Gin Cocktail',
brandName: 'TWR+ Sevilla Gimlet',
price: 75,
),
Drinks(
category: 'WHISK(E)Y Cocktails',
brandName: "Keep Walking Sling",
price: 65,
),
Drinks(
category: 'WHISK(E)Y Cocktail',
brandName: 'TWR+ Old Fashion',
price: 85,
),
Drinks(
category: 'WHISK(E)Y Cocktail',
brandName: 'Gold Standard',
price: 120,
),
Drinks(
category: 'WHISK(E)Y Cocktail',
brandName: 'Same as Jamie',
price: 90,
),
Drinks(
category: 'WHISK(E)Y Cocktail',
brandName: "Caskmates Sling",
price: 95,
),
Drinks(
category: 'WHISK(E)Y Cocktail',
brandName: 'The County Cork',
price: 100,
),
Drinks(
category: 'WHISK(E)Y Cocktail',
brandName: 'Johnie Walker Red ',
price: 25,
)
];
setState(() {
_dishes = list;
});
}
}