I have a variable friendsList
that is passed to the FriendsFeed class. I reference the friendsList
in the FriendsFeed State with widget.friendsList
. Is using widget.[my_variable_name]
to reference StatefulWidget variables the professional way to do so? I can't help but feel like there's a cleaner way to do so.
import 'package:flutter/material.dart';
class FriendsFeed extends StatefulWidget {
FriendsFeed(this.friendsList);
final List<dynamic> friendsList;
@override
_FriendsFeedState createState() => _FriendsFeedState();
}
class _FriendsFeedState extends State<FriendsFeed> {
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
final ColorScheme colorScheme = Theme.of(context).colorScheme;
final Color oddItemColor = colorScheme.primary.withOpacity(0.05);
final Color evenItemColor = colorScheme.primary.withOpacity(0.15);
return ReorderableListView(
padding: const EdgeInsets.symmetric(horizontal: 40),
children: <Widget>[
for (int index = 0; index < widget.friendsList.length; index++)
ListTile(
key: Key('$index'),
tileColor:
widget.friendsList[index].isOdd ? oddItemColor : evenItemColor,
title: Text('Item ${widget.friendsList[index]}'),
),
],
onReorder: (int oldIndex, int newIndex) {
setState(() {
if (oldIndex < newIndex) {
newIndex -= 1;
}
final int item = widget.friendsList.removeAt(oldIndex);
widget.friendsList.insert(newIndex, item);
});
},
);
}
}