1
0
mirror of https://github.com/flutter/samples.git synced 2026-03-25 05:41:41 +00:00
Files
samples/experimental/web_dashboard/lib/src/pages/dashboard.dart
2022-05-17 02:53:27 -07:00

71 lines
1.9 KiB
Dart

// Copyright 2020, the Flutter project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../api/api.dart';
import '../app.dart';
import '../widgets/category_chart.dart';
class DashboardPage extends StatelessWidget {
const DashboardPage({super.key});
@override
Widget build(BuildContext context) {
var appState = Provider.of<AppState>(context);
return FutureBuilder<List<Category>>(
future: appState.api!.categories.list(),
builder: (context, futureSnapshot) {
if (!futureSnapshot.hasData) {
return const Center(
child: CircularProgressIndicator(),
);
}
return StreamBuilder<List<Category>>(
initialData: futureSnapshot.data,
stream: appState.api!.categories.subscribe(),
builder: (context, snapshot) {
if (snapshot.data == null) {
return const Center(
child: CircularProgressIndicator(),
);
}
return Dashboard(snapshot.data);
},
);
},
);
}
}
class Dashboard extends StatelessWidget {
final List<Category>? categories;
const Dashboard(this.categories, {super.key});
@override
Widget build(BuildContext context) {
var api = Provider.of<AppState>(context).api;
return Scrollbar(
child: GridView(
gridDelegate: const SliverGridDelegateWithMaxCrossAxisExtent(
childAspectRatio: 2,
maxCrossAxisExtent: 500,
),
children: [
...categories!.map(
(category) => Card(
child: CategoryChart(
category: category,
api: api,
),
),
)
],
),
);
}
}