【问题标题】:Flutter DataTable select RowFlutter DataTable 选择行
【发布时间】:2020-12-16 00:06:18
【问题描述】:

我在 Flutter 中遇到了 DataTable 的问题。当用户单击其中一个时,我想更改行的状态,但我发现了如何在 DataRow 内部使用 selected: true 来“手动”执行此操作。

如何通过更改状态让用户只选择一行,然后取消选择其余行?

content: Column(
               children: <Widget>[
                 DataTable(
                   columns: [
                     DataColumn(label: Text('Language')),
                     DataColumn(label: Text('Translation')),
                   ],
                   rows: [
                     DataRow(selected: true, cells: [
                       DataCell(
                         Text(
                           "RO",
                           textAlign: TextAlign.left,
                           style: TextStyle(fontWeight: FontWeight.bold),
                         ),
                         onTap: () {
                           setState(() {
                             color = Colors.lightBlueAccent;

                           });
                         },
                       ),
                       DataCell(
                         TextField(
                           decoration: InputDecoration(
                               border: InputBorder.none, hintText: 'paine'),
                         ),
                       ),
                     ]),
                     DataRow(cells: [
                       DataCell(
                           Text(
                             "EN",
                             textAlign: TextAlign.left,
                             style: TextStyle(fontWeight: FontWeight.bold),
                           ), onTap: () {
                         print('EN is clicked');
                       }),
                       DataCell(
                         TextField(
                           decoration: InputDecoration(
                               border: InputBorder.none, hintText: 'bread'),
                         ),
                       ),
                     ]),
                     DataRow(cells: [
                       DataCell(
                           Text(
                             "FR",
                             textAlign: TextAlign.left,
                             style: TextStyle(fontWeight: FontWeight.bold),
                           ), onTap: () {
                         print('FR is clicked');
                       }),
                       DataCell(
                         TextField(
                           decoration: InputDecoration(
                               border: InputBorder.none, hintText: 'pain'),
                         ),
                       ),
                     ]),
                   ],
                 ),
               ],
             ),

【问题讨论】:

    标签: flutter dart mobile row datarow


    【解决方案1】:

    当我按下DataRow 时需要调用某些东西时,我执行了以下操作:

    DataRow(
    onSelectChanged(_) {
    // do something here
    }
    cells: [],
    ),
    

    也许这会有所帮助?

    编辑:这是一个如何选择/取消选择行的简单示例

    import 'package:flutter/material.dart';
    
    class Stack1 extends StatefulWidget {
      @override
      _Stack1State createState() => _Stack1State();
    }
    
    class _Stack1State extends State<Stack1> {
      bool row1Selected = false;
      bool row2Selected = false;
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: DataTable(
            columns: [
              DataColumn(
                label: Text('My Data'),
              ),
            ],
            rows: [
              DataRow(
                selected: row1Selected,
                onSelectChanged: (value) {
                  setState(() {
                    row1Selected = value;
                  });
                },
                cells: [
                  DataCell(
                    Text('Data 1'),
                  ),
                ],
              ),
              DataRow(
                selected: row2Selected,
                onSelectChanged: (value) {
                  setState(() {
                    row2Selected = value;
                  });
                },
                cells: [
                  DataCell(
                    Text('Data 2'),
                  ),
                ],
              ),
            ],
          ),
        );
      }
    }
    

    【讨论】:

    • 我怎样才能改变方法内的“选定”模式?或者改变 DataRow 的颜色,任何东西都会有用..
    • @AlexandruBuruiana 我现在不确定,我现在会尝试找到解决方案。
    【解决方案2】:

    您可以在下面复制过去运行的完整代码
    您可以在selected 中使用条件,像这样0 == selectedIndex 其中0 是DataRow 索引
    并将selectedIndex 更改为onSelectChanged
    代码sn-p

        int selectedIndex = -1;
        ... 
        rows: [
        DataRow(
            selected: 0 == selectedIndex,
            onSelectChanged: (val) {
              setState(() {
                selectedIndex = 0;
              });
            },
            ...
        DataRow(
            selected: 1 == selectedIndex,
            onSelectChanged: (val) {
              setState(() {
                selectedIndex = 1;
              });
            },
    

    工作演示

    完整代码

    import 'package:flutter/material.dart';
    
    void main() {
      runApp(MyApp());
    }
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Demo',
          theme: ThemeData(
            primarySwatch: Colors.blue,
            visualDensity: VisualDensity.adaptivePlatformDensity,
          ),
          home: MyHomePage(title: 'Flutter Demo Home Page'),
        );
      }
    }
    
    class MyHomePage extends StatefulWidget {
      MyHomePage({Key key, this.title}) : super(key: key);
    
      final String title;
    
      @override
      _MyHomePageState createState() => _MyHomePageState();
    }
    
    class _MyHomePageState extends State<MyHomePage> {
      Color color;
      int selectedIndex = -1;
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text(widget.title),
          ),
          body: Center(
            child: Column(
              children: <Widget>[
                DataTable(
                  onSelectAll: (val) {
                    setState(() {
                      selectedIndex = -1;
                    });
                  },
                  columns: [
                    DataColumn(label: Text('Language')),
                    DataColumn(label: Text('Translation')),
                  ],
                  rows: [
                    DataRow(
                        selected: 0 == selectedIndex,
                        onSelectChanged: (val) {
                          setState(() {
                            selectedIndex = 0;
                          });
                        },
                        cells: [
                          DataCell(
                            Text(
                              "RO",
                              textAlign: TextAlign.left,
                              style: TextStyle(fontWeight: FontWeight.bold),
                            ),
                            onTap: () {
                              setState(() {
                                color = Colors.lightBlueAccent;
                              });
                            },
                          ),
                          DataCell(
                            TextField(
                              decoration: InputDecoration(
                                  border: InputBorder.none, hintText: 'paine'),
                            ),
                          ),
                        ]),
                    DataRow(
                        selected: 1 == selectedIndex,
                        onSelectChanged: (val) {
                          setState(() {
                            selectedIndex = 1;
                          });
                        },
                        cells: [
                          DataCell(
                              Text(
                                "EN",
                                textAlign: TextAlign.left,
                                style: TextStyle(fontWeight: FontWeight.bold),
                              ), onTap: () {
                            print('EN is clicked');
                          }),
                          DataCell(
                            TextField(
                              decoration: InputDecoration(
                                  border: InputBorder.none, hintText: 'bread'),
                            ),
                          ),
                        ]),
                    DataRow(
                        selected: 2 == selectedIndex,
                        onSelectChanged: (val) {
                          setState(() {
                            selectedIndex = 2;
                          });
                        },
                        cells: [
                          DataCell(
                              Text(
                                "FR",
                                textAlign: TextAlign.left,
                                style: TextStyle(fontWeight: FontWeight.bold),
                              ), onTap: () {
                            print('FR is clicked');
                          }),
                          DataCell(
                            TextField(
                              decoration: InputDecoration(
                                  border: InputBorder.none, hintText: 'pain'),
                            ),
                          ),
                        ]),
                  ],
                ),
              ],
            ),
          ),
        );
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-10
      • 2016-07-29
      • 2013-11-07
      • 1970-01-01
      • 1970-01-01
      • 2018-04-10
      • 1970-01-01
      相关资源
      最近更新 更多