Flutter:如何将DraggableScrollableActuator.reset和DraggableScrollableSheet中的ScrollController用于嵌套列表?

时间:2020-04-22 07:44:26

标签: flutter

我收到此错误。

The following assertion was thrown building _InheritedResetNotifier(dirty):
flutter: ScrollController attached to multiple scroll views.
flutter: 'package:flutter/src/widgets/scroll_controller.dart':
flutter: Failed assertion: line 111 pos 12: '_positions.length == 1'

似乎我可以将DraggableScrollableSheet的ScrollController附加到嵌套列表,也可以使用DraggableScrollableActuator.reset(context)来重置范围。我不明白为什么我不能两者都做。这是一个错误吗?有其他解决方法吗?

这是我的代码:

class InterfaceSelector extends StatefulWidget {
  @override
  InterfaceSelectorState createState() => InterfaceSelectorState();
}

class InterfaceSelectorState extends State<InterfaceSelector> {
  double initialChildSize = 0.5;
  double minChildSize = 0.5;
  double maxChildSize = 0.7;

  @override
  Widget build(BuildContext context) {
    return DraggableScrollableActuator(
      child: DraggableScrollableSheet(
        initialChildSize: initialChildSize,
        minChildSize: minChildSize,
        maxChildSize: maxChildSize,
        builder: (BuildContext ctx, ScrollController scrollController) {
          return SingleChildScrollView(
            controller: scrollController,
            child: Container(
              height: MediaQuery.of(context).size.longestSide * .7,
              color: Theme.of(ctx).primaryColor,
              child: DefaultTabController(
                length: 4,
                child: Column(
                  children: <Widget>[
                    Material(
                      color: Theme.of(ctx).accentColor,
                      child: TabBar(
                        tabs: [
                          Tab(text: "One"),
                          Tab(text: "Two"),
                          Tab(text: "Three"),
                          Tab(text: "Four"),
                        ],
                      ),
                    ),
                    Expanded(
                      child: TabBarView(
                        children: [
                          One(),
                          Two(),
                          Three(scrollController),
                          Four(),
                        ],
                      ),
                    ),
                  ],
                ),
              ),
            ),
          );
        },
      ),
    );
  }
}

class Three extends StatefulWidget {
  ScrollController scrollController;
  Three(this.scrollController);
  @override
  _ThreeState createState() => _ThreeState();
}

class _ThreeState extends State<Three> with AutomaticKeepAliveClientMixin {
  @override
  bool get wantKeepAlive => true;

  @override
  Widget build(BuildContext context) {
    Stuff stuff = Provider.of<Stuff>(context);
    return Column(
      children: <Widget>[
        MaterialButton(onPressed: () {
          DraggableScrollableActuator.reset(context);
        }),
        Expanded(
          child: GridView.builder(
            controller: widget.scrollController,
            padding: const EdgeInsets.all(10),
            itemCount: stuff.all.length,
            itemBuilder: (ctx, i) => ChangeNotifierProvider.value(
              value: stuff.all[i],
              key: UniqueKey(),
              child: StuffCard(i, stuff.all[i], stuff),
            ),
            gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
              crossAxisCount: 3,
              crossAxisSpacing: 10,
              mainAxisSpacing: 10,
            ),
          ),
        ),
      ],
    );
  }
}

1 个答案:

答案 0 :(得分:1)

您的import dash_core_components as dcc import dash_html_components as html from dash.dependencies import Input, Output import plotly.graph_objs as go from django_plotly_dash import DjangoDash from datetime import datetime, timedelta from home.models import data app = DjangoDash('example') app.layout = html.Div(children =[ dcc.Interval( id='my_interval', disabled=False, interval=1000, n_intervals=0 ), dcc.Graph( id='chart', ) ]) @app.callback( Output(component_id='chart',component_property='figure'), [Input(component_id='my_interval', component_property='n_intervals')] ) def update_value(num): model_data = data.objects.values_list('temp','time') x_values = [] y_values = [] for values in model_data: x_values.append(values[1]- timedelta(hours=7)) y_values.append(values[0]) fig=go.Figure( data=[go.Scatter(x=x_values, y=y_values, mode='lines', name='markers')], layout={'uirevision':True}, ) return (fig) 不需要滚动-您的GridView提供了滚动。不要将SingleChildScrollView传递给scrollController,而是在网格生成器中设置GridView

physics: NeverScrollableScrollPhysics(),