使用Supabase导航到Flutter中的详细信息页面

k5ifujac  于 2023-04-13  发布在  Flutter
关注(0)|答案(1)|浏览(157)

谁能给我解释一下如何用Flutter和Supabase从ListViw.builder中创建一个链接来在“细节页面”上显示细节?
这对于ListView.builder页面来说没问题,我从Supabase数据库中获取数据,但是它将数据从列表索引传递到我遇到麻烦的详细信息页面。
谢谢你

// final _mySupabaseTable = Supabase.instance.client.from('xxx').stream(primaryKey: ['id']).order('id', ascending: false);

  ...

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Padding(
        padding: const EdgeInsets.all(8.0),
    child: StreamBuilder<List<Map<String, dynamic>>>(
    stream: _mySupabaseTable,
    builder: (context, snapshot) {
    if (!snapshot.hasData){
    return const Center(
    child: CircularProgressIndicator(),
    );
    }
    final items = snapshot.data!;
    return ListView.builder(
    itemCount: items.length,
    itemBuilder: (context, index) {
    return InkWell(
    onTap: () =>
    Navigator.push(context,
    MaterialPageRoute(builder: (context)
    => PageDetail()
    ),
    ),

    child: Card(
    child: Padding(
    padding: const EdgeInsets.all(8.0),
    child: Column(
    crossAxisAlignment: CrossAxisAlignment.start,
    children: [
    ListTile(
    title: Text(items[index]['date'] + " / "+ items[index]['heure'],
    style: const TextStyle(fontSize: 12.0, color: Colors.grey),),
    trailing: const Icon(Icons.arrow_forward_ios),
    ),
0kjbasz6

0kjbasz61#

我终于找到了答案。。

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {

  final _future = Supabase.instance.client
      .from('name_of_my_supabase_table')
      .select<List<Map<String, dynamic>>>().order('id', ascending: false);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("Test Supabase ListView.builder"),
      ),
      body: FutureBuilder<List<Map<String, dynamic>>>(
        future: _future,
        builder: (context, snapshot) {
          if (!snapshot.hasData) {
            return const Center(child: CircularProgressIndicator());
          }
          final items = snapshot.data!;
          return Padding(
            padding: const EdgeInsets.all(8.0),
            child: ListView.builder(
              itemCount: items.length,
              itemBuilder: ((context, index) {
                final item = items[index];
                return Card(
                  child: InkWell(
                    onTap: () =>
                        Navigator.push(context,
                          MaterialPageRoute(builder: (context)
                          => DetailPage(itemDetail: items[index])
                          ),
                        ),
                    child: ListTile(
                      title: Text(item['date']),
                    ),
                  ),
                );
              }),
            ),
          );
        },
      ),
    );
  }
}

class DetailPage extends StatelessWidget {
  const DetailPage({Key? key, this.itemDetail}) : super(key: key);

  final itemDetail;

  @override
  Widget build(BuildContext context) {
    return  Scaffold(
      appBar: AppBar(
        title: const Text('Detail Page'),
      ),
      body: Center(
        child: Text('Detail Page ' + itemDetail['date']),
      ),
    );
  }
}

相关问题