改进行的ListView的总体布局

时间:2019-07-10 03:38:41

标签: android flutter

我正在尝试像这样实现行的ListView: enter image description here

这是我的代码:

import 'package:flutter/material.dart';
import 'package:good_driver_app/common/ExpandedButton.dart';

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

  // This widget is the home page of your application. It is stateful, meaning
  // that it has a State object (defined below) that contains fields that affect
  // how it looks.

  // This class is the configuration for the state. It holds the values (in this
  // case the title) provided by the parent (in this case the App widget) and
  // used by the build method of the State. Fields in a Widget subclass are
  // always marked "final".

//  final String title;

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

class _ServicePetrolPage extends State<ServicePetrolPage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      // This call to setState tells the Flutter framework that something has
      // changed in this State, which causes it to rerun the build method below
      // so that the display can reflect the updated values. If we changed
      // _counter without calling setState(), then the build method would not be
      // called again, and so nothing would appear to happen.
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    // This method is rerun every time setState is called, for instance as done
    // by the _incrementCounter method above.
    //
    // The Flutter framework has been optimized to make rerunning build methods
    // fast, so that you can just rebuild anything that needs updating rather
    // than having to individually change instances of widgets.
    return Scaffold(
      appBar: AppBar(
        // Here we take the value from the MyHomePage object that was created by
        // the App.build method, and use it to set our appbar title.
        title: Text('Petrol'),
      ),
      body: Container(
        child: ListView(
          children: <Widget>[
            Container(
              padding: EdgeInsets.all(10),
              child: Row(
                children: <Widget>[
                  Expanded(
                    child: Container(
                      padding: EdgeInsets.all(15.0),
                      height: 50.0,
                      child: Row (
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: <Widget>[
                          Flexible(
                            child: new Text(
                              "Logo",
                            ),
                          ),
                          Flexible(
                            child: new Text(
                              "Location",
                            ),
                          ),
                          Flexible(
                            child: new Text(
                              "Distance",
                            ),
                          ),
                          Flexible(
                            child: new Text(
                              "\$",
                            ),
                          ),
                          Flexible(
                            child: new Text(
                              "Facilities",
                            ),
                          ),

                        ],
                      ),
                    ),
                  )
                ],
              ),
            ),
            Container(
              padding: EdgeInsets.all(10),
              child: Row(
                children: <Widget>[
                  Expanded(
                    child: Container(
                      padding: EdgeInsets.all(15.0),
                      height: 50.0,
                      child: Row (
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: <Widget>[
                          Flexible(
                            child: new Text(
                              "Logo 1",
                            ),
                          ),
                          Flexible(
                            child: new Text(
                              "Johnson Rd",
                            ),
                          ),
                          Flexible(
                            child: new Text(
                              "0.2 KM",
                            ),
                          ),
                          Flexible(
                            child: new Text(
                              "\$12.45",
                            ),
                          ),
                          Flexible(
                            child: new Text(
                              "ATM, Restaurant",
                            ),
                          ),

                        ],
                      ),
                    ),
                  )
                ],
              ),
            ),
            Container(
              padding: EdgeInsets.all(10),
              child: Row(
                children: <Widget>[
                  Expanded(
                    child: Container(
                      padding: EdgeInsets.all(15.0),
                      height: 50.0,
                      child: Row (
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: <Widget>[
                          Flexible(
                            child: new Text(
                              "Logo 2",
                            ),
                          ),
                          Flexible(
                            child: new Text(
                              "Hennessy Rd",
                            ),
                          ),
                          Flexible(
                            child: new Text(
                              "0.5 KM",
                            ),
                          ),
                          Flexible(
                            child: new Text(
                              "\$12.88",
                            ),
                          ),
                          Flexible(
                            child: new Text(
                              "ATM",
                            ),
                          ),

                        ],
                      ),
                    ),
                  )
                ],
              ),
            ),
            Container(
              padding: EdgeInsets.all(10),
              child: Row(
                children: <Widget>[
                  Expanded(
                    child: Container(
                      padding: EdgeInsets.all(15.0),
                      height: 50.0,
                      child: Row (
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: <Widget>[
                          Flexible(
                            child: new Text(
                              "Logo 3",
                            ),
                          ),
                          Flexible(
                            child: new Text(
                              "Lockhart Rd",
                            ),
                          ),
                          Flexible(
                            child: new Text(
                              "1.2 KM",
                            ),
                          ),
                          Flexible(
                            child: new Text(
                              "\$12.88",
                            ),
                          ),
                          Flexible(
                            child: new Text(
                              "Toilet",
                            ),
                          ),

                        ],
                      ),
                    ),
                  )
                ],
              ),
            ),
            Container(
              padding: EdgeInsets.all(10),
              child: Row(
                children: <Widget>[
                  Expanded(
                    child: Container(
                      padding: EdgeInsets.all(15.0),
                      height: 50.0,
                      child: Row (
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: <Widget>[
                          Flexible(
                            child: new Text(
                              "Logo 4",
                            ),
                          ),
                          Flexible(
                            child: new Text(
                              "Canal Rd",
                            ),
                          ),
                          Flexible(
                            child: new Text(
                              "1.2 KM",
                            ),
                          ),
                          Flexible(
                            child: new Text(
                              "\$12.90",
                            ),
                          ),
                          Flexible(
                            child: new Text(
                              "Restaurant, Toilet",
                            ),
                          ),

                        ],
                      ),
                    ),
                  )
                ],
              ),
            ),
          ],
        ),
      ),

    );
  }
}

这是结果:

enter image description here

我的问题是:

  1. 如何在单元格之间放置水平间隙?
  2. 如何缩小行间距?

2 个答案:

答案 0 :(得分:0)

1-添加一个Container并将height设置为1(您想要的颜色)(在每个容器行之间)。

  Container(
              height: 1,
              color: Colors.black,
            ),

2-减少填充,我将将此padding: EdgeInsets.all(10)更改为此padding: EdgeInsets.symmetric(horizontal: 10, vertical: 2)

最终代码:

 @override
  Widget build(BuildContext context) {
    // This method is rerun every time setState is called, for instance as done
    // by the _incrementCounter method above.
    //
    // The Flutter framework has been optimized to make rerunning build methods
    // fast, so that you can just rebuild anything that needs updating rather
    // than having to individually change instances of widgets.
    return Scaffold(
      appBar: AppBar(
        // Here we take the value from the MyHomePage object that was created by
        // the App.build method, and use it to set our appbar title.
        title: Text('Petrol'),
      ),
      body: Container(
        child: ListView(
          children: <Widget>[
            Container(
              padding: EdgeInsets.all(10),
              child: Row(
                children: <Widget>[
                  Expanded(
                    child: Container(
                      padding: EdgeInsets.all(15.0),
                      height: 50.0,
                      child: Row(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: <Widget>[
                          Flexible(
                            child: new Text(
                              "Logo",
                            ),
                          ),
                          Flexible(
                            child: new Text(
                              "Location",
                            ),
                          ),
                          Flexible(
                            child: new Text(
                              "Distance",
                            ),
                          ),
                          Flexible(
                            child: new Text(
                              "\$",
                            ),
                          ),
                          Flexible(
                            child: new Text(
                              "Facilities",
                            ),
                          ),
                        ],
                      ),
                    ),
                  )
                ],
              ),
            ),
            Container(
              height: 1,
              color: Colors.black,
            ),
            Container(
              padding: EdgeInsets.all(10),
              child: Row(
                children: <Widget>[
                  Expanded(
                    child: Container(
                      padding:
                          EdgeInsets.symmetric(horizontal: 10, vertical: 2),
                      height: 50.0,
                      child: Row(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: <Widget>[
                          Flexible(
                            child: new Text(
                              "Logo 1",
                            ),
                          ),
                          Flexible(
                            child: new Text(
                              "Johnson Rd",
                            ),
                          ),
                          Flexible(
                            child: new Text(
                              "0.2 KM",
                            ),
                          ),
                          Flexible(
                            child: new Text(
                              "\$12.45",
                            ),
                          ),
                          Flexible(
                            child: new Text(
                              "ATM, Restaurant",
                            ),
                          ),
                        ],
                      ),
                    ),
                  )
                ],
              ),
            ),
            Container(
              height: 1,
              color: Colors.black,
            ),
            Container(
              padding: EdgeInsets.symmetric(horizontal: 10, vertical: 2),
              child: Row(
                children: <Widget>[
                  Expanded(
                    child: Container(
                      padding: EdgeInsets.all(15.0),
                      height: 50.0,
                      child: Row(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: <Widget>[
                          Flexible(
                            child: new Text(
                              "Logo 2",
                            ),
                          ),
                          Flexible(
                            child: new Text(
                              "Hennessy Rd",
                            ),
                          ),
                          Flexible(
                            child: new Text(
                              "0.5 KM",
                            ),
                          ),
                          Flexible(
                            child: new Text(
                              "\$12.88",
                            ),
                          ),
                          Flexible(
                            child: new Text(
                              "ATM",
                            ),
                          ),
                        ],
                      ),
                    ),
                  )
                ],
              ),
            ),
            Container(
              height: 1,
              color: Colors.black,
            ),
            Container(
              padding: EdgeInsets.symmetric(horizontal: 10, vertical: 2),
              child: Row(
                children: <Widget>[
                  Expanded(
                    child: Container(
                      padding: EdgeInsets.all(15.0),
                      height: 50.0,
                      child: Row(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: <Widget>[
                          Flexible(
                            child: new Text(
                              "Logo 3",
                            ),
                          ),
                          Flexible(
                            child: new Text(
                              "Lockhart Rd",
                            ),
                          ),
                          Flexible(
                            child: new Text(
                              "1.2 KM",
                            ),
                          ),
                          Flexible(
                            child: new Text(
                              "\$12.88",
                            ),
                          ),
                          Flexible(
                            child: new Text(
                              "Toilet",
                            ),
                          ),
                        ],
                      ),
                    ),
                  )
                ],
              ),
            ),
            Container(
              height: 1,
              color: Colors.black,
            ),
            Container(
              padding: EdgeInsets.symmetric(horizontal: 10, vertical: 2),
              child: Row(
                children: <Widget>[
                  Expanded(
                    child: Container(
                      padding: EdgeInsets.all(15.0),
                      height: 50.0,
                      child: Row(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: <Widget>[
                          Flexible(
                            child: new Text(
                              "Logo 4",
                            ),
                          ),
                          Flexible(
                            child: new Text(
                              "Canal Rd",
                            ),
                          ),
                          Flexible(
                            child: new Text(
                              "1.2 KM",
                            ),
                          ),
                          Flexible(
                            child: new Text(
                              "\$12.90",
                            ),
                          ),
                          Flexible(
                            child: new Text(
                              "Restaurant, Toilet",
                            ),
                          ),
                        ],
                      ),
                    ),
                  )
                ],
              ),
            ),
            Container(
              height: 1,
              color: Colors.black,
            ),
          ],
        ),
      ),
    );
  }

答案 1 :(得分:0)

这就是我要做的。

ServicePetrolPage

int mergeSortInvCount(vector<int> &arr, int izq, int der);
int mergeInvCount(vector<int> &arr, int izq, int mitad, int der);

void invCountRecursivo(vector<int> &arr, int n){



    int numInversiones = mergeSortInvCount(arr, 1, n);
    cout << "Num inversiones:" << numInversiones << endl;
    for(int i=0; i < n; i++){

        cout<<arr[i]<<endl;
    }
}

int mergeSortInvCount(vector<int> &arr, int izq, int der){

    int invCount = 0;

    if(izq < der){

        int mitad = (izq + der)/2;

        invCount = mergeSortInvCount(arr, izq, mitad);
        invCount += mergeSortInvCount(arr, mitad+1, der);
        invCount += mergeInvCount(arr, izq, mitad, der);
    }

    return invCount;
}

int infinito = numeric_limits<int>::max();

int mergeInvCount(vector<int> &arr, int izq, int mitad, int der){

    int n1 = mitad - izq + 1;
    int n2 = der - mitad;

    int invCount = 0;

    vector<int> vectorIzq;
    vector<int> vectorDer;

    for(int k=0;k<n1;k++){

        vectorIzq.push_back(arr[izq+k-1]);
    }

    vectorIzq.push_back(infinito);

    for(int k=0;k<n2;k++){

        vectorDer.push_back(arr[mitad+k]);
    }

    vectorDer.push_back(infinito);

    int i = 0;
    int j = 0;

    for(int k = izq; k <= der; k++){

        if(vectorIzq[i] <= vectorDer[j]){

            arr[k] = vectorIzq[i];
            i++;
        }
        else{

            arr[k] = vectorDer[j];
            j++;
            invCount += (mitad - i);
        }
    }

    return invCount;
}

DataView

class ServicePetrolPage extends StatefulWidget {
  @override
  _ServicePetrolPage createState() => _ServicePetrolPage();
}

class _ServicePetrolPage extends State<ServicePetrolPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Petrol'),
      ),
      body: Container(
        child: ListView(
          children: <Widget>[
            DataView(
              distance: 'Distance',
              facilities: ['Facilities'],
              location: 'Location',
              logo: ' Logo ',
              price: '',
            ),
            DataView(
              logo: 'Logo 1',
              location: 'Johnson Rd',
              distance: '0.2 KM',
              facilities: ['ATM', 'Restaurant'],
              price: '12.45',
            ),
            DataView(
              logo: 'Logo 2',
              location: 'Hennessy Rd',
              distance: '0.5 KM',
              facilities: ['ATM'],
              price: '12.88',
            ),
            DataView(
              logo: 'Logo 3',
              location: 'Lockhart Rd',
              distance: '1.5 KM',
              facilities: ['Toilet'],
              price: '12.88',
            ),
            DataView(
              logo: 'Logo 4',
              location: 'Canal Rd',
              distance: '1.2 KM',
              facilities: ['Restaurant', 'Toilet'],
              price: '12.90',
            ),
          ],
        ),
      ),
    );
  }
}
  

由于我使用的是列表展开语法,因此您必须更新自己的   pubspec.yaml中的SDK约束

     

pubspec.yaml

class DataView extends StatelessWidget {
  final String logo;
  final String location;
  final String distance;
  final String price;
  final List<String> facilities;

  DataView({
    this.location,
    this.distance,
    this.facilities,
    this.logo,
    this.price,
  });

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.all(10),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: <Widget>[
          Text("$logo"),
          Expanded(
            flex: 2,
            child: Center(child: Text("$location")),
          ),
          Expanded(
            flex: 1,
            child: Center(child: Text("$distance")),
          ),
          Expanded(
            flex: 1,
            child: Center(child: Text("\$$price")),
          ),
          Expanded(
            flex: 1,
            child: Wrap(
              alignment: WrapAlignment.center,
              children: <Widget>[
                if (facilities != null)
                  ...facilities.map((facility) {
                    return Padding(
                      padding: const EdgeInsets.all(0.0),
                      child: Text(
                        '$facility ',
                        overflow: TextOverflow.ellipsis,
                      ),
                    );
                  }).toList(),
              ],
            ),
          ),
        ],
      ),
    );
  }
}