Flutter: reversed stacking for a listView - TagMerge
5Flutter: reversed stacking for a listViewFlutter: reversed stacking for a listView

Flutter: reversed stacking for a listView

Asked 1 years ago
1
5 answers

You can reverse the ListView and while populating data use maxLength-index-1.

body: ListView.builder(
  itemCount: 20,
  reverse: true,
  itemBuilder: (context, index) {
    return Align(
      child: Card(
        color:
            Colors.primaries[random.nextInt(Colors.primaries.length)]
                [random.nextInt(9) * 100],
        child: Container(
          width: 100,
          child: Text('Item ${20 - index - 1}',
              style: TextStyle(fontSize: 24)),
        ),
      ),
      heightFactor: 0.6,
    );
  },
)

enter image description here

Source: link

0

It's a hack but you can reverse your List and ListView as well and use them.

I am assuming you must be getting the list dynamically so you need to reverse a list first as well to show it in proper order.

Example:

List<MaterialColor> testColor = Colors.primaries.reversed.toList();

and use it in a list like below:

ListView.builder(
  itemCount: testColor.length,
  shrinkWrap: true,
  reverse: true,
  itemBuilder: (context, index) {
    return Align(
      child: Card(
        color: testColor[random.nextInt(testColor.length)]
            [random.nextInt(9) * 100],
        child: Container(
          width: 100,
          child: Text(
            'Item ${testColor.length - index - 1}',
            style: TextStyle(fontSize: 24),
          ),
        ),
      ),
      heightFactor: 0.6,
    );
  },
),

I have additionally set shrinkWrap: true so if your list is of limited length so it won't take an entire space of the screen.

Source: link

0

Open main.dart and replace the code with the following:
import 'package:flutter/material.dart';
    
    void main() => runApp(MyApp());
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          debugShowCheckedModeBanner: false,
          title: 'ListViews',
          theme: ThemeData(
            primarySwatch: Colors.teal,
          ),
          home: Scaffold(
            appBar: AppBar(title: Text('ListViews')),
            body: BodyLayout(),
          ),
        );
      }
    }
    
    class BodyLayout extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return _myListView(context);
      }
    }
    
    // replace this function with the code in the examples
    Widget _myListView(BuildContext context) {
      return ListView();
    }
Replace _myListView() with the following:
Widget _myListView(BuildContext context) {
      return ListView(
        children: <Widget>[
          ListTile(
            title: Text('Sun'),
          ),
          ListTile(
            title: Text('Moon'),
          ),
          ListTile(
            title: Text('Star'),
          ),
        ],
      );
    }
If you want to add dividers between the rows then use the ListTile.divideTiles constructor.
Widget _myListView(BuildContext context) {
      return ListView(
        children: ListTile.divideTiles(
          context: context,
          tiles: [
            ListTile(
              title: Text('Sun'),
            ),
            ListTile(
              title: Text('Moon'),
            ),
            ListTile(
              title: Text('Star'),
            ),
          ],
        ).toList(),
      );
    }
Replace _myListView() with the following:
Widget _myListView(BuildContext context) {
      
      // backing data
      final europeanCountries = ['Albania', 'Andorra', 'Armenia', 'Austria', 
        'Azerbaijan', 'Belarus', 'Belgium', 'Bosnia and Herzegovina', 'Bulgaria',
        'Croatia', 'Cyprus', 'Czech Republic', 'Denmark', 'Estonia', 'Finland',
        'France', 'Georgia', 'Germany', 'Greece', 'Hungary', 'Iceland', 'Ireland',
        'Italy', 'Kazakhstan', 'Kosovo', 'Latvia', 'Liechtenstein', 'Lithuania',
        'Luxembourg', 'Macedonia', 'Malta', 'Moldova', 'Monaco', 'Montenegro',
        'Netherlands', 'Norway', 'Poland', 'Portugal', 'Romania', 'Russia',
        'San Marino', 'Serbia', 'Slovakia', 'Slovenia', 'Spain', 'Sweden', 
        'Switzerland', 'Turkey', 'Ukraine', 'United Kingdom', 'Vatican City'];
        
      return ListView.builder(
        itemCount: europeanCountries.length,
        itemBuilder: (context, index) {
          return ListTile(
            title: Text(europeanCountries[index]),
          );
        },
      );
      
    }
Replace _myListView() with the following:
Widget _myListView(BuildContext context) {
      return ListView.builder(
        itemBuilder: (context, index) {
          return ListTile(
            title: Text('row $index'),
          );
        },
      );
    }

Source: link

0

Download the installation bundle by clicking this link
$ cd ~/desiredfolder
$ unzip ~/Downloads/fluttermacos2.0.2-stable.zip
Unzip and cd into the desired folder: $ cd ~/desiredfolder $ unzip ~/Downloads/fluttermacos2.0.2-stable.zip
$ export PATH="$PATH:DIRTOYOUR_FLUTTER/flutter/bin"
This command will download the Flutter SDK and run diagnostics to determine if everything is good to go. At the end of the run, you may have this result:
[!] Android Studio (version 4.1)
    ✗ Flutter plugin not installed; this adds Flutter specific functionality.
    ✗ Dart plugin not installed; this adds Dart specific functionality.
[!] Connected device
    ! No devices available
! Doctor found issues in 4 categories.
Now, go back to your terminal, and scaffold a Flutter project:
flutter create myapp
Run the Flutter project:
flutter run

Source: link

0

assignment
ListView(
  padding: const EdgeInsets.all(8),
  children: <Widget>[
    Container(
      height: 50,
      color: Colors.amber[600],
      child: const Center(child: Text('Entry A')),
    ),
    Container(
      height: 50,
      color: Colors.amber[500],
      child: const Center(child: Text('Entry B')),
    ),
    Container(
      height: 50,
      color: Colors.amber[100],
      child: const Center(child: Text('Entry C')),
    ),
  ],
)
assignment
final List<String> entries = <String>['A', 'B', 'C'];
final List<int> colorCodes = <int>[600, 500, 100];

ListView.builder(
  padding: const EdgeInsets.all(8),
  itemCount: entries.length,
  itemBuilder: (BuildContext context, int index) {
    return Container(
      height: 50,
      color: Colors.amber[colorCodes[index]],
      child: Center(child: Text('Entry ${entries[index]}')),
    );
  }
);
assignment
final List<String> entries = <String>['A', 'B', 'C'];
final List<int> colorCodes = <int>[600, 500, 100];

ListView.separated(
  padding: const EdgeInsets.all(8),
  itemCount: entries.length,
  itemBuilder: (BuildContext context, int index) {
    return Container(
      height: 50,
      color: Colors.amber[colorCodes[index]],
      child: Center(child: Text('Entry ${entries[index]}')),
    );
  },
  separatorBuilder: (BuildContext context, int index) => const Divider(),
);
assignment
ListView(
  shrinkWrap: true,
  padding: const EdgeInsets.all(20.0),
  children: const <Widget>[
    Text("I'm dedicating every day to you"),
    Text('Domestic life was never quite my style'),
    Text('When you smile, you knock me out, I fall apart'),
    Text('And I thought I was so smart'),
  ],
)
assignment
CustomScrollView(
  shrinkWrap: true,
  slivers: <Widget>[
    SliverPadding(
      padding: const EdgeInsets.all(20.0),
      sliver: SliverList(
        delegate: SliverChildListDelegate(
          <Widget>[
            const Text("I'm dedicating every day to you"),
            const Text('Domestic life was never quite my style'),
            const Text('When you smile, you knock me out, I fall apart'),
            const Text('And I thought I was so smart'),
          ],
        ),
      ),
    ),
  ],
)

Source: link

Recent Questions on flutter

    Programming Languages