| 1 | // Copyright 2014 The Flutter Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | import 'package:flutter/material.dart'; |
| 6 | |
| 7 | /// Flutter code sample for [ListTile.selected]. |
| 8 | |
| 9 | void main() => runApp(const ListTileApp()); |
| 10 | |
| 11 | class ListTileApp extends StatelessWidget { |
| 12 | const ListTileApp({super.key}); |
| 13 | |
| 14 | @override |
| 15 | Widget build(BuildContext context) { |
| 16 | return const MaterialApp(home: ListTileExample()); |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | class ListTileExample extends StatefulWidget { |
| 21 | const ListTileExample({super.key}); |
| 22 | |
| 23 | @override |
| 24 | State<ListTileExample> createState() => _ListTileExampleState(); |
| 25 | } |
| 26 | |
| 27 | class _ListTileExampleState extends State<ListTileExample> { |
| 28 | int _selectedIndex = 0; |
| 29 | |
| 30 | @override |
| 31 | Widget build(BuildContext context) { |
| 32 | return Scaffold( |
| 33 | appBar: AppBar(title: const Text('Custom List Item Sample' )), |
| 34 | body: ListView.builder( |
| 35 | itemCount: 10, |
| 36 | itemBuilder: (BuildContext context, int index) { |
| 37 | return ListTile( |
| 38 | title: Text('Item $index' ), |
| 39 | selected: index == _selectedIndex, |
| 40 | onTap: () { |
| 41 | setState(() { |
| 42 | _selectedIndex = index; |
| 43 | }); |
| 44 | }, |
| 45 | ); |
| 46 | }, |
| 47 | ), |
| 48 | ); |
| 49 | } |
| 50 | } |
| 51 | |