Флаттер - Как скрыть или установить видимую табличку? - PullRequest
0 голосов
/ 06 мая 2019

Я использую виджет Flutter Table. Я хочу скрыть одну из строк таблицы на основе определенных условий.

Я пробовал виджет видимости над Tablerow, но он не позволяет. Я также пробовал булево условие, например. бул? Tablerow (...): Tablerow (), кажется, не работает должным образом, поскольку дал нулевое исключение - вероятно, Tablerow пуст.

child: Table(
          border: TableBorder.all(width: 1.0, color: Colors.black),
          children: [

            TableRow(
              children: [
                TableCell(
                    child: Row(
                        children: <Widget>[
                          new Padding(
                            padding: const EdgeInsets.all(5.0),
                            child: new Text('ID1'),
                          ),
                        ]
                    )
                ),

                TableCell(
                    child: Row(
                        children: <Widget>[
                          new Padding(
                            padding: const EdgeInsets.all(5.0),
                            child: new Text('Name1'),
                          ),
                        ]
                    )
                )
              ]
            ),

            TableRow(
                children: [
                  TableCell(
                      child: Row(
                          children: <Widget>[
                            new Padding(
                              padding: const EdgeInsets.all(5.0),
                              child: new Text('ID2'),
                            ),
                          ]
                      )
                  ),

                  TableCell(
                      child: Row(
                          children: <Widget>[
                            new Padding(
                              padding: const EdgeInsets.all(5.0),
                              child: new Text('Name2'),
                            ),
                          ]
                      )
                  )
                ]
            ),


            TableRow(
                children: [
                  TableCell(
                      child: Row(
                          children: <Widget>[
                            new Padding(
                              padding: const EdgeInsets.all(5.0),
                              child: new Text('ID3'),
                            ),
                          ]
                      )
                  ),

                  TableCell(
                      child: Row(
                          children: <Widget>[
                            new Padding(
                              padding: const EdgeInsets.all(5.0),
                              child: new Text('Name3'),
                            ),
                          ]
                      )
                  )
                ]
            ),

          ],
        )

UPDATE:

bool_row ? TableRow(
                children: [
                  TableCell(
                      child: Row(
                          children: <Widget>[
                            new Padding(
                              padding: const EdgeInsets.all(5.0),
                              child: new Text('ID2'),
                            ),
                          ]
                      )
                  ),

                  TableCell(
                      child: Row(
                          children: <Widget>[
                            new Padding(
                              padding: const EdgeInsets.all(5.0),
                              child: new Text('Name2'),
                            ),
                          ]
                      )
                  )
                ]
            ) : TableRow(),

Используя логический метод, bool_row имеет значение false, 2-е условие «TableRow ()» выдает ошибку:
: метод 'any' был вызван для null.
: получатель: ноль
: пробный вызов: любой (закрытие: (виджет) => bool)

Я не знаю, как создать пустую невидимую TableRow.

На основании приведенного выше кода, если я хочу скрыть или установить 2-ую строку как невидимую. Как мне этого добиться?

Заранее спасибо!

1 Ответ

1 голос
/ 06 мая 2019

Вы можете сделать так, как показано ниже, чтобы программно скрыть и показать строку таблицы, где мы взяли логическую переменную visibilityTableRow, и на основании этого мы решили передать что-то в ячейку таблицы:

import 'package:flutter/material.dart';

class MyTable extends StatefulWidget {
  createState() {
    return StateKeeper();
  }
}

class StateKeeper extends State<MyTable> {

  bool visibilityTableRow = true;

  void _changed() {
    setState(() {
      if(visibilityTableRow){
        visibilityTableRow = false;
      }else{
        visibilityTableRow = true;
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      primary: true,
      appBar: AppBar(
        title: Text("Table View"),
      ),

      body: Column(
        children: <Widget>[

          Table(
            border: TableBorder.all(width: 1.0, color: Colors.black),
            children: [

              TableRow(
                  children: [
                    TableCell(
                        child: Row(
                            children: <Widget>[
                              new Padding(
                                padding: const EdgeInsets.all(5.0),
                                child: new Text('ID1'),
                              ),
                            ]
                        )
                    ),

                    TableCell(
                        child: Row(
                            children: <Widget>[
                              new Padding(
                                padding: const EdgeInsets.all(5.0),
                                child: new Text('Name1'),
                              ),
                            ]
                        )
                    )
                  ]
              ),

              visibilityTableRow ? TableRow(
                  children: [
                    TableCell(
                        child: Row(
                            children: <Widget>[
                              new Padding(
                                padding: const EdgeInsets.all(5.0),
                                child: new Text('ID2'),
                              ),
                            ]
                        )
                    ),

                    TableCell(
                        child: Row(
                            children: <Widget>[
                              new Padding(
                                padding: const EdgeInsets.all(5.0),
                                child: new Text('Name2'),
                              ),
                            ]
                        )
                    )
                  ]
              ): new TableRow(
                  children: [
                    TableCell(
                        child: Row(
                            children: <Widget>[
                              new Container(),
                            ]
                        )
                    ),

                    TableCell(
                        child: Row(
                            children: <Widget>[
                              new Container(),
                            ]
                        )
                    )
                  ]
              ),


              TableRow(
                  children: [
                    TableCell(
                        child: Row(
                            children: <Widget>[
                              new Padding(
                                padding: const EdgeInsets.all(5.0),
                                child: new Text('ID3'),
                              ),
                            ]
                        )
                    ),

                    TableCell(
                        child: Row(
                            children: <Widget>[
                              new Padding(
                                padding: const EdgeInsets.all(5.0),
                                child: new Text('Name3'),
                              ),
                            ]
                        )
                    )
                  ]
              ),

            ],
          ),


          RaisedButton(
            child: Text("Hide/Show Table Row"),
            onPressed: () => _changed(),
          ),
        ],
      )
    );
  }
}
...