滚动视图无响应

时间:2020-10-14 11:49:11

标签: flutter scrollview

我的Scrollview没有响应,有人可以告诉我代码中缺少的内容吗

请给我建议我如何添加滚动视图侦听器。

import 'package:flutter/material.dart';
import 'package:share/share.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:url_launcher/url_launcher.dart';
import '../main.dart';
import 'favourite_articles.dart';
import 'coin_system.dart';

class Settings extends StatefulWidget {
  @override
  _SettingsState createState() => _SettingsState();
}

class _SettingsState extends State<Settings> {
  bool _notification = false;
  ScrollController _controller;
  @override
  void initState() {
    super.initState();
    checkNotificationSetting();
  }

  checkNotificationSetting() async {
    final prefs = await SharedPreferences.getInstance();
    final key = 'notification';
    final value = prefs.getInt(key) ?? 0;
    if (value == 0) {
      setState(() {
        _notification = false;
      });
    } else {
      setState(() {
        _notification = true;
      });
    }
  }

  saveNotificationSetting(bool val) async {
    final prefs = await SharedPreferences.getInstance();
    final key = 'notification';
    final value = val ? 1 : 0;
    prefs.setInt(key, value);
    if (value == 1) {
      setState(() {
        _notification = true;
      });
    } else {
      setState(() {
        _notification = false;
      });
    }
    Future.delayed(const Duration(milliseconds: 500), () {
      Navigator.of(context).pushReplacement(
          MaterialPageRoute(builder: (BuildContext context) => MyHomePage()));
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
        centerTitle: true,
        title: Text(
          'More',
          style: TextStyle(
              color: Colors.black,
              fontWeight: FontWeight.bold,
              fontSize: 20,
              fontFamily: 'Poppins'),
        ),
        elevation: 5,
        backgroundColor: Colors.white,
        actions: <Widget>[
          IconButton(
              icon: Icon(Icons.mail),
              color: Colors.black,
              tooltip: 'Song Request',
              onPressed: () {
                Navigator.push(
                  context,
                  MaterialPageRoute(
                    builder: (context) => CoinSystem(),
                  ),
                );
              })
        ],
      ),
      body: Container(
        decoration: BoxDecoration(color: Colors.white),
        child: SingleChildScrollView(
          controller: _controller,
          scrollDirection: Axis.vertical,
          child: Column(
            children: <Widget>[
              Container(
                alignment: Alignment.center,
                padding: EdgeInsets.fromLTRB(0, 20, 0, 10),
                child: Image(
                  image: AssetImage('assets/icon.png'),
                  height: 50,
                ),
              ),
              Container(
                alignment: Alignment.center,
                padding: EdgeInsets.fromLTRB(0, 10, 0, 20),
                child: Text(
                  "Version 2.1.0 \n ",
                  textAlign: TextAlign.center,
                  style: TextStyle(height: 1.6, color: Colors.black87),
                ),
              ),
              Divider(
                height: 10,
                thickness: 2,
              ),
              //ListWheelScrollView(
              ListView(
                //itemExtent: 75,
                shrinkWrap: true,
                children: <Widget>[
                  InkWell(
                    onTap: () {
                      Navigator.push(
                        context,
                        MaterialPageRoute(
                          builder: (context) => FavouriteArticles(),
                        ),
                      );
                    },
                    child: ListTile(
                      leading: Image.asset(
                        "assets/more/favourite.png",
                        width: 30,
                      ),
                      title: Text('Favourite'),
                      subtitle: Text("See the saved songs"),
                    ),
                  ),

                  //Song Request Code
                  InkWell(
                    onTap: () {
                      Navigator.push(
                        context,
                        MaterialPageRoute(
                          builder: (context) => CoinSystem(),
                        ),
                      );
                    },
                    child: ListTile(
                      leading: Image.asset(
                        "assets/more/songrequest.png",
                        width: 30,
                      ),
                      title: Text('Songs Request'),
                      subtitle: Text("Request your favourite songs"),
                    ),
                  ),

                  //Song Request Code End

                  
                  
                  
                  ListTile(
                    leading: Image.asset(
                      "assets/more/notification.png",
                      width: 30,
                    ),
                    isThreeLine: true,
                    title: Text('Notification'),
                    subtitle: Text("Change notification preference"),
                    trailing: Switch(
                        onChanged: (val) async {
                          await saveNotificationSetting(val);
                        },
                        value: _notification),
                  ),
                ],
              )
            ],
          ),
        ),
      ),
      //),
    );
    //);
  }
}

因此,我尝试了SingleChildScrollView,因为我有Container和Listview。但是Listview对横向模式下的滚动操作没有响应。

我添加了ScrollController _controller;我是否必须创建_controller类来侦听滚动操作?

2 个答案:

答案 0 :(得分:0)

据我了解,您希望能够获得2次滚动。 1.使用SingleChildScrollView并在该Widget内,您希望能够滚动最底层,因此使用ListView。为了使其工作,您必须将ListView放在具有一定高度的小部件内。此实现的示例是:

SingleChildScrollView(
      scrollDirection: Axis.vertical,
      child: Column(
        children: <Widget>[
          SizedBox(child: Text('Upper scrollable'), height: 450),
          Divider(
            height: 10,
            thickness: 2,
          ),
          Container(
            height: 350,
            child: ListView(
              shrinkWrap: true,
              children: <Widget>[
                Container(
                  color:Colors.red,
                  child: SizedBox(child: Text('Bottom scrollable'), height: 450),
                ),
              ],
            ),
          )
        ],
      ),
    ),

如果您不想使用2滚动,请不要在SingleChildScrollView中使用ListView。

答案 1 :(得分:0)

  1. ListView不能用SingleChildScrollView包裹起来将其删除
  2. 使用扩展的小部件环绕ListView

尝试两种选择之一。