添加ListView.builder

时间:2019-07-16 00:40:26

标签: flutter dart

我正在尝试使displayAccoutList()可滚动。

我尝试使用ListView,但是没有用。我正在考虑使用ListView.builder,但我不知道如何为accountItems()创建数据

在此处输入代码import'package:flutter / material.dart'; 导入'app_drawer.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Accounts',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Account(),
    );
  }
}

class Account extends StatefulWidget {
  @override
  _AccountState createState() => _AccountState();
}

class _AccountState extends State<Account> {
  Card topArea() =>
      Card(
        margin: EdgeInsets.all(10.0),
        elevation: 1.0,
        shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.all(Radius.circular(50.0))),
        child: Container(
            decoration: BoxDecoration(
                gradient: RadialGradient(
                    colors: [Color(0xFFFF5722), Color(0xFFFF5722)])),
            padding: EdgeInsets.all(5.0),
            // color: Color(0xFF015FFF),
            child: Column(
              children: <Widget>[
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: <Widget>[
                    IconButton(
                      icon: Icon(
                        Icons.arrow_back,
                        color: Colors.white,
                      ),
                      onPressed: () {},
                    ),
                    Text("Savings",
                        style: TextStyle(color: Colors.white, fontSize: 20.0)),
                    IconButton(
                      icon: Icon(
                        Icons.arrow_forward,
                        color: Colors.white,
                      ),
                      onPressed: () {},
                    )
                  ],
                ),
                Center(
                  child: Padding(
                    padding: EdgeInsets.all(5.0),
                    child: Text(r"£ " "100,943.33",
                        style: TextStyle(color: Colors.white, fontSize: 24.0)),
                  ),
                ),
                SizedBox(height: 35.0),
              ],
            )),
      );

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        drawer: AppDrawer(),
        appBar: AppBar(
          iconTheme: IconThemeData(
            color: Colors.grey, //change your color here
          ),
          backgroundColor: Colors.white,
          elevation: 0.0,
          title: Text(
            "Accounts",
            style: TextStyle(color: Colors.black),
          ),
          centerTitle: true,
          actions: <Widget>[
            IconButton(
              icon: Icon(
                Icons.search,
                color: Colors.grey,
              ),
              onPressed: () {},
            )
          ],
        ),
        body: Container(
          color: Colors.white,
          child: Column(
            children: <Widget>[
              topArea(),
              SizedBox(
                height: 40.0,
                child: Icon(Icons.refresh,
                  size: 35.0,
                  color: Color(0xFFFF5722),
                ),
              ),
              displayAccoutList()
            ],
          ),
        ),
        bottomNavigationBar: BottomAppBar(
          elevation: 0.0,
          child: Container(
            margin: EdgeInsets.symmetric(vertical: 20.0),
            child: Row(
              mainAxisSize: MainAxisSize.max,
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: <Widget>[
                FlatButton(
                  padding:
                  EdgeInsets.symmetric(vertical: 12.0, horizontal: 30.0),
                  shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(0.0)),
                  color: Color(0xFFFF5722),
                  // borderSide: BorderSide(color: Color(0xFF015FFF), width: 1.0),
                  onPressed: () {},
                  child: Text("ACTIVITY"),
                ),
                OutlineButton(
                  padding:
                  EdgeInsets.symmetric(vertical: 12.0, horizontal: 28.0),
                  shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(0.0)),
                  borderSide: BorderSide(color: Color(0xFFFF5722), width: 1.0),
                  onPressed: () {},
                  child: Text("STATEMENTS"),
                ),
                OutlineButton(
                  padding:
                  EdgeInsets.symmetric(vertical: 12.0, horizontal: 28.0),
                  shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(0.0)),
                  borderSide: BorderSide(color: Color(0xFFFF5722), width: 1.0),
                  onPressed: () {},
                  child: Text("DETAILS"),
                )
              ],
            ),
          ),
        )
    );
  }
}

Container accountItems(String item, String charge, String dateString,
    String type,
    {Color oddColour = Colors.white}) =>
    Container(
      decoration: BoxDecoration(color: oddColour),
      padding:
      EdgeInsets.only(top: 20.0, bottom: 20.0, left: 5.0, right: 5.0),
      child: Column(
        children: <Widget>[
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: <Widget>[
              Text(item, style: TextStyle(fontSize: 16.0)),
              Text(charge, style: TextStyle(fontSize: 16.0))
            ],
          ),
          SizedBox(
            height: 10.0,
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: <Widget>[
              Text(dateString,
                  style: TextStyle(color: Colors.grey, fontSize: 14.0)),
              Text(type, style: TextStyle(color: Colors.grey, fontSize: 14.0))
            ],
          ),
        ],
      ),
    );

displayAccoutList() {
  return Container(
    margin: EdgeInsets.all(15.0),
    child: Column(
      children: <Widget>[
        accountItems("M KING", r"+ £ 4,946.00", "15-07-19", "Credit",
            oddColour: const Color(0xFFF7F7F9)),
        accountItems(
            "Gordon Street Tenants", r"+ £ 5,428.00", "15-07-19", "Credit"),
        accountItems("Amazon EU", r"+ £ 746.00", "15-07-19", "Credit",
            oddColour: const Color(0xFFF7F7F9)),
        accountItems(
            "Floww LTD", r"+ £ 5,526.00", "15-07-19", "Credit"),
        accountItems(
            "KLM", r"- £ 2,500.00", "10-07-19", "Payment",
            oddColour: const Color(0xFFF7F7F9)),
      ],
    ),
  );

`

另一页app_drawer.dart

`

import 'package:flutter/material.dart';

class AppDrawer extends StatefulWidget {
  @override
  _AppDrawerState createState() => _AppDrawerState();
}

class _AppDrawerState extends State<AppDrawer> {
  @override
  Widget build(BuildContext context) {
    return SizedBox(
      width: 160.0,
      child: Drawer(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            Padding(
              padding: EdgeInsets.only(top: 50.0),
              child: FlatButton.icon(
                icon: Icon(
                  Icons.arrow_back,
                  color: Colors.grey,
                ),
                onPressed: null,
                label: Text("Back",
                    style: TextStyle(
                        fontWeight: FontWeight.w400,
                        fontSize: 16.0,
                        color: Colors.black)),
                color: Colors.black,
              ),
            ),
            buildMenuItem(Icons.account_balance, "ACCOUNTS",
                opacity: 1.0, color: Color(0xFFFF6E40)),
            Divider(),
            buildMenuItem(Icons.compare_arrows, "TRANSFER"),
            Divider(),
            buildMenuItem(Icons.receipt, "STATEMENTS"),
            Divider(),
            buildMenuItem(Icons.attach_money, "PAYMENTS"),
            Divider(),
            buildMenuItem(Icons.sentiment_satisfied, "INVESTMENTS"),
            Divider(),
            buildMenuItem(Icons.phone, "SUPPORT"),
            Divider()
          ],
        ),
      ),
    );
  }

  Opacity buildMenuItem(IconData icon, String title,
      {double opacity = 0.3, Color color = Colors.black}) {
    return Opacity(
      opacity: opacity,
      child: Center(
        child: Column(
          children: <Widget>[
            SizedBox(
              height: 20.0,
            ),
            Icon(
              icon,
              size: 50.0,
              color: color,
            ),
            SizedBox(
              height: 10.0,
            ),
            Text(title,
                style: TextStyle(
                    fontWeight: FontWeight.w500, fontSize: 14.0, color: color)),
            SizedBox(
              height: 10.0,
            ),
          ],
        ),
      ),
    );
  }
}

`

要能够滚动浏览该部分并进行无限量交易

1 个答案:

答案 0 :(得分:0)

我想这就是你要的。

在此代码段中,我将Language定义为具有属性languagerating的类。

然后,我在无状态小部件TheList中创建了语言列表。

最后,我使用listView.builder来映射列表的长度,并显示文本以显示语言的属性。

让我知道您是否有任何疑问!

import 'package:flutter/material.dart';

class Language {
  const Language({this.language,this.rating});

  final String language;
  final String rating;
}

class TheList extends StatelessWidget {
  // Builder methods rely on a set of data, such as a list.
  final List<Language> _languages = [
    const Language(language: "flutter", rating: "amazing"),
    const Language(language: "javascript", rating: "stellar"),
    const Language(language: "java", rating: "sucks"),
  ];

  // First, make your build method like normal.
  // Instead of returning Widgets, return a method that returns widgets.
  // Don't forget to pass in the context!
  @override
  Widget build(BuildContext context) {
    return _buildList(context);
  }

  // A builder method almost always returns a ListView.
  // A ListView is a widget similar to Column or Row.
  // It knows whether it needs to be scrollable or not.
  // It has a constructor called builder, which it knows will
  // work with a List.

  ListView _buildList(context) {
    return ListView.builder(
      // Must have an item count equal to the number of items!
      itemCount: _languages.length,
      // A callback that will return a widget.
      itemBuilder: (context, index) {
        // In our case, a Language and its rating for each language in the list.
        return Text(_languages[index].language + ": " + _languages[index].rating);
      },
    );
  }
}