Advertisement
yudiwbs

flutter_listviewbuilder

May 22nd, 2022
846
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import 'package:flutter/material.dart';
  2.  
  3. void main() {
  4.   runApp(const MyApp());
  5. }
  6.  
  7. class MyApp extends StatefulWidget {
  8.   const MyApp({Key? key}) : super(key: key);
  9.   @override
  10.   State<StatefulWidget> createState() {
  11.     return MyAppState();
  12.   }
  13. }
  14.  
  15. class MyAppState extends State<MyApp> {
  16.   List<String> data = []; //data untuk listview
  17.  
  18.   @override
  19.   void initState() {
  20.     super.initState();
  21.     // isi data listview
  22.     for (int i = 0; i < 20; i++) {
  23.       data.add("Data ke $i ");
  24.     }
  25.   }
  26.  
  27.   @override
  28.   Widget build(BuildContext context) {
  29.     return MaterialApp(
  30.       title: 'Welcome to Flutter',
  31.       home: Scaffold(
  32.         appBar: AppBar(
  33.           title: const Text('Welcome to Flutter'),
  34.         ),
  35.         body: Center(
  36.           //gunakan listview builder
  37.           child: ListView.builder(
  38.             itemCount: data.length,
  39.             itemBuilder: (context, index) {
  40.               return Container(
  41.                 decoration: BoxDecoration(border: Border.all()),
  42.                 padding: const EdgeInsets.all(14),
  43.                 child: Text(data[index]),
  44.               );
  45.             },
  46.           ),
  47.         ),
  48.       ),
  49.     );
  50.   }
  51. }
  52.  
Advertisement
RAW Paste Data Copied
Advertisement