ListTile
sample.packages.flutter.lib.src.material.list_tile.517.
Here is an example of using a [StatefulWidget] to keep track of the
selected index, and using that to set the selected
property on the
corresponding [ListTile].
import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
static const String _title = 'Flutter Code Sample';
@override
Widget build(BuildContext context) {
return MaterialApp(
title: _title,
home: Scaffold(
appBar: AppBar(title: const Text(_title)),
body: const MyStatefulWidget(),
),
);
}
}
class MyStatefulWidget extends StatefulWidget {
const MyStatefulWidget({Key? key}) : super(key: key);
@override
State createState() => _MyStatefulWidgetState();
}
class _MyStatefulWidgetState extends State {
int _selectedIndex = 0;
@override
Widget build(BuildContext context) {
return ListView.builder(
itemCount: 10,
itemBuilder: (BuildContext context, int index) {
return ListTile(
title: Text('Item $index'),
selected: index == _selectedIndex,
onTap: () {
setState(() {
_selectedIndex = index;
});
},
);
},
);
}
}
SHARE: