dart 如何更改Flutter中的默认ScrollPhysics?

ndasle7k  于 2023-07-31  发布在  Flutter
关注(0)|答案(3)|浏览(130)

我需要将应用程序中几乎每个可滚动小部件的ScrollPhysics更改为BouncingScrollPhysics()。我试图找到一种方法来做到这一点,而不添加physics属性无处不在,但我还没有找到一个好的方法。一个解决方案是使用flutter_platform_widgets并将initialPlatform设置为iOS,但这也会改变很多其他事情。
有没有人知道这是可能的,如果是这样的话,怎么办?

tp5buhyn

tp5buhyn1#

你可以复制粘贴下面运行完整的代码
您可以扩展ScrollBehavior并放入MaterialAppbuilder
在演示代码中,iOS, macOS, android将使用BouncingScrollPhysics
代码片段

class ScrollBehaviorModified extends ScrollBehavior {
  const ScrollBehaviorModified();
  @override
  ScrollPhysics getScrollPhysics(BuildContext context) {
    switch (getPlatform(context)) {
      case TargetPlatform.iOS:
      case TargetPlatform.macOS:
      case TargetPlatform.android:
        return const BouncingScrollPhysics();
      case TargetPlatform.fuchsia:
      case TargetPlatform.linux:
      case TargetPlatform.windows:
        return const ClampingScrollPhysics();
    }
    return null;
  }
}
...
builder: (context, widget) {
        return ScrollConfiguration(
            behavior: ScrollBehaviorModified(), child: widget);
      },

字符串
工作演示


的数据
全码

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class ScrollBehaviorModified extends ScrollBehavior {
  const ScrollBehaviorModified();
  @override
  ScrollPhysics getScrollPhysics(BuildContext context) {
    switch (getPlatform(context)) {
      case TargetPlatform.iOS:
      case TargetPlatform.macOS:
      case TargetPlatform.android:
        return const BouncingScrollPhysics();
      case TargetPlatform.fuchsia:
      case TargetPlatform.linux:
      case TargetPlatform.windows:
        return const ClampingScrollPhysics();
    }
    return null;
  }
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      builder: (context, widget) {
        return ScrollConfiguration(
            behavior: ScrollBehaviorModified(), child: widget);
      },
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

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

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Expanded(
              child: ListView.separated(
                itemBuilder: (BuildContext context, int index) {
                  return Text('Item$index');
                },
                separatorBuilder: (BuildContext context, int index) {
                  return Divider();
                },
                itemCount: 50,
              ),
            ),
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headline4,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}

d5vmydt9

d5vmydt92#

另一个没有新类的解决方案??

MaterialApp(
  scrollBehavior: ScrollConfiguration.of(context).copyWith(
    physics: const BouncingScrollPhysics(
      parent: AlwaysScrollableScrollPhysics(),
    ),
  ),
  // ...
);

字符串

p5fdfcr1

p5fdfcr13#

您特定问题的简单解决方案是=> inside ThemeData

platform: TargetPlatform.iOS,

字符串
Snapshot of the Theme code

相关问题