如何在小部件之间传递数据

时间:2020-10-17 07:49:11

标签: flutter dart

我有以下代码,可以从url正确提取数据,将其显示在DataTable中,并获取所点击行的idext。

我需要使用此索引并在小部件的背面显示相关信息,我在TweenAnimationBuilder上滑动,但是不确定如何在两张面孔之间发送数据,或者是否有其他方法可以执行此操作我在寻找:

import 'package:flutter/material.dart';
import 'dart:convert';
import 'dart:io';
import 'dart:async';
import 'package:csv/csv.dart';

void main() {
  runApp(App());
}

class App extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: HomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class HomePage extends StatefulWidget {
  HomePage({Key key, this.title}) : super(key: key);
  final String title;

  @override
  _AppState createState() => _AppState();
}

Future<List<List<dynamic>>> fetchUserData() async {
  final request = await HttpClient().getUrl(Uri.parse(
      'https://docs.google.com/spreadsheets/d/e/2PACX-1vQvf9tp4-fETDJbC-HRmRKvVFAXEAGO4lrYPpVeiYkB6nqqXdSs3CjX0eBMvjIoEeX9_qU6K2RWmzVk/pub?gid=0&single=true&output=csv'));
  final response = await request.close();
  List<List<dynamic>> rowsAsListOfValues;
  await for (final csvString in response.transform(const Utf8Decoder())) {
    rowsAsListOfValues = const CsvToListConverter().convert(csvString);
  }
  return rowsAsListOfValues;
}

class _AppState extends State<HomePage> {
  List<List<dynamic>> rowsAsListOfValues;
  // ScrollController _controller;

  @override
  void didChangeDependencies() async {
    super.didChangeDependencies();
    rowsAsListOfValues = await fetchUserData();
    super.setState(() {}); // to update widget data
  }

  @override
  void initState() {
    super.initState();
   // _controller = ScrollController();
   // _controller = new ScrollController()..addListener(_scrollListener);
  }
/*
  void _scrollListener() {
    if (_controller.position.extentAfter <= 0.0 &&
        _controller.offset >= _controller.position.maxScrollExtent &&
        !_controller.position.outOfRange) {
      print("call fetch method again");
    }
  } */

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            rowsAsListOfValues == null
                ? Text("Loading data...")
                : Text(
              '', // $rowsAsListOfValues
            ),
            rowsAsListOfValues == null
                ? CircularProgressIndicator()
                : DataTable(
                showCheckboxColumn: false, // <-- this is important
                columns: const <DataColumn>[
                  DataColumn(
                    label: Text(
                      'City',
                      style: TextStyle(fontStyle: FontStyle.italic),
                    ),
                  ),
                  DataColumn(
                    label: Text(
                      'Branches',
                      style: TextStyle(fontStyle: FontStyle.italic),
                    ),
                  ),
                ],
                rows: List.generate(rowsAsListOfValues.length - 1, (index) {
                  return DataRow(
                    onSelectChanged: (bool selected) {
                      if (selected) {
                        print('index-selected: ${rowsAsListOfValues[index + 1][0]}');
                      }
                    },
                    cells: <DataCell>[
                      // DataCell(Text('${rowsAsListOfValues[index + 1][0]}')), // Index
                      DataCell(Text('${rowsAsListOfValues[index + 1][1]}')),  // city
                      DataCell(Text('${rowsAsListOfValues[index + 1][2]}')),  // branch
                    ],
                  );
                }),
            ),
          ],
        ),
      ),
    );
  }
}

1 个答案:

答案 0 :(得分:1)

也许您可以使用此https://pub.dev/packages/flippable_box flutter插件,该插件为您提供了定义正面和背面不同视图的选项。您可以使用_isFlipped变量来维护要显示的一侧。

FlippableBox(
    front: Data table for front view,
    back: Display selected row,
    isFlipped: _isFlipped,
)