为什么该应用程序中令人耳目一新的拉力不起作用?

发布于 2025-02-05 16:51:08 字数 5174 浏览 3 评论 0原文

我正在用flutter 2.10.5和飞镖2.16.2构建我的应用程序。 当我尝试刷新演示内容时,什么也不会发生。我有多个针对不同内容的导航路线。因此,演示是一个小小的复杂。

main.dart包括应用程序的基本代码。我使用Navdrawer小部件来构建不同的页面。每个路由都在导航中定义。

到目前为止,我的代码是:

import 'dart:core';
import 'package:english_words/english_words.dart';
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  // This widget is the root of the application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Demo Company',
      theme: ThemeData(),
      debugShowCheckedModeBanner: false,
      home: const HomePage(title: 'Demo Company'),
    );
  }
}

class _HomePageState extends State<HomePage> {
  @override
  initState() {
    super.initState();
  }

  Widget _infoTile(String title, String subtitle) {
    return ListTile(
      title: Text(title),
      subtitle: Text(subtitle.isEmpty ? 'Not set' : subtitle),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      drawer: const NavDrawer(),
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          children: <Widget>[
            _infoTile('App name', 'Demo App....'),
            // Multiple Liste Tiles...
          ],
        ),
      ),
    );
  }
}

//----------------------------------------------------------------------
// navigation.dart
//----------------------------------------------------------------------

class NavDrawer extends StatelessWidget {
  const NavDrawer({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Drawer(
      child: ListView(
        padding: EdgeInsets.zero,
        children: <Widget>[
          DrawerHeader(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: const <Widget>[
                Text(
                  'Navigation',
                  style: TextStyle(color: Colors.white, fontSize: 30),
                ),
                SizedBox(height: 30.0),
                Text('Firstname', style: TextStyle(color: Colors.black, fontSize: 15)),
                Text('Accountname', style: TextStyle(color: Colors.black, fontSize: 15)),
              ],
            ),
          ),
          ListTile(
            leading: const Icon(Icons.notifications),
            title: const Text('Demo'),
            onTap: () {
              Navigator.push(
                context,
                Demo.route(),
              );
            },
          ),
          // Multiple Navigation List Tiles...
        ],
      ),
    );
  }
}


//----------------------------------------------------------------------
// demo.dart
//----------------------------------------------------------------------

class HomePage extends StatefulWidget {
  const HomePage({Key? key, required this.title}) : super(key: key);
  final String title;

  @override
  State<HomePage> createState() => _HomePageState();
}

class Demo extends StatefulWidget {
  const Demo({Key? key}) : super(key: key);

  static Route route() {
    return CupertinoPageRoute(builder: (_) => const Demo());
  }

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

class _DemoState extends State<Demo> {
  final _data = <WordPair>[];

  @override
  void initState() {
    super.initState();
    _data.addAll(generateWordPairs().take(20));
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Woolha.com Flutter Tutorial'),
      ),
      body: _buildList(),
    );
  }

  Widget _buildList() {
    return RefreshIndicator(
      onRefresh: _refreshData,
      child: ListView.builder(
        padding: const EdgeInsets.all(20.0),
        itemBuilder: (context, index) {
          WordPair wordPair = _data[index];

          return _buildListItem(wordPair.asString, context);
        },
        itemCount: _data.length,
      ),
    );
  }

  Widget _buildListItem(String word, BuildContext context) {
    return Card(
      child: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Text(word),
      ),
    );
  }

  Future _refreshData() async {
    await Future.delayed(const Duration(seconds: 3));
    _data.clear();
    _data.addAll(generateWordPairs().take(20));

    setState(() {});
  }
}

class ShowMessages extends StatelessWidget {
  final String type;
  final Color color;

  const ShowMessages({Key? key, required this.type, required this.color}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ListView(
        //color: color,
        physics: const AlwaysScrollableScrollPhysics(),
        children: [
          ListTile(
            title: Text(
              type,
              style: Theme.of(context).textTheme.bodyText1,
            ),
          ),
        ]);
  }
}

将此代码复制到 dartpad

是什么问题?

I'm building my app with Flutter 2.10.5 and Dart 2.16.2.
When i try to refresh the demo content whith a pull, nothing happens. I have multiple navigation routes for different content. So the demo is a litte bit complex.

The main.dart includes the basic code for the app. I use the NavDrawer Widget to build the different pages. Every route is defined in the navigation.dart file, which reference to the content widgets.

My code so far is:

import 'dart:core';
import 'package:english_words/english_words.dart';
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  // This widget is the root of the application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Demo Company',
      theme: ThemeData(),
      debugShowCheckedModeBanner: false,
      home: const HomePage(title: 'Demo Company'),
    );
  }
}

class _HomePageState extends State<HomePage> {
  @override
  initState() {
    super.initState();
  }

  Widget _infoTile(String title, String subtitle) {
    return ListTile(
      title: Text(title),
      subtitle: Text(subtitle.isEmpty ? 'Not set' : subtitle),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      drawer: const NavDrawer(),
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          children: <Widget>[
            _infoTile('App name', 'Demo App....'),
            // Multiple Liste Tiles...
          ],
        ),
      ),
    );
  }
}

//----------------------------------------------------------------------
// navigation.dart
//----------------------------------------------------------------------

class NavDrawer extends StatelessWidget {
  const NavDrawer({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Drawer(
      child: ListView(
        padding: EdgeInsets.zero,
        children: <Widget>[
          DrawerHeader(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: const <Widget>[
                Text(
                  'Navigation',
                  style: TextStyle(color: Colors.white, fontSize: 30),
                ),
                SizedBox(height: 30.0),
                Text('Firstname', style: TextStyle(color: Colors.black, fontSize: 15)),
                Text('Accountname', style: TextStyle(color: Colors.black, fontSize: 15)),
              ],
            ),
          ),
          ListTile(
            leading: const Icon(Icons.notifications),
            title: const Text('Demo'),
            onTap: () {
              Navigator.push(
                context,
                Demo.route(),
              );
            },
          ),
          // Multiple Navigation List Tiles...
        ],
      ),
    );
  }
}


//----------------------------------------------------------------------
// demo.dart
//----------------------------------------------------------------------

class HomePage extends StatefulWidget {
  const HomePage({Key? key, required this.title}) : super(key: key);
  final String title;

  @override
  State<HomePage> createState() => _HomePageState();
}

class Demo extends StatefulWidget {
  const Demo({Key? key}) : super(key: key);

  static Route route() {
    return CupertinoPageRoute(builder: (_) => const Demo());
  }

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

class _DemoState extends State<Demo> {
  final _data = <WordPair>[];

  @override
  void initState() {
    super.initState();
    _data.addAll(generateWordPairs().take(20));
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Woolha.com Flutter Tutorial'),
      ),
      body: _buildList(),
    );
  }

  Widget _buildList() {
    return RefreshIndicator(
      onRefresh: _refreshData,
      child: ListView.builder(
        padding: const EdgeInsets.all(20.0),
        itemBuilder: (context, index) {
          WordPair wordPair = _data[index];

          return _buildListItem(wordPair.asString, context);
        },
        itemCount: _data.length,
      ),
    );
  }

  Widget _buildListItem(String word, BuildContext context) {
    return Card(
      child: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Text(word),
      ),
    );
  }

  Future _refreshData() async {
    await Future.delayed(const Duration(seconds: 3));
    _data.clear();
    _data.addAll(generateWordPairs().take(20));

    setState(() {});
  }
}

class ShowMessages extends StatelessWidget {
  final String type;
  final Color color;

  const ShowMessages({Key? key, required this.type, required this.color}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ListView(
        //color: color,
        physics: const AlwaysScrollableScrollPhysics(),
        children: [
          ListTile(
            title: Text(
              type,
              style: Theme.of(context).textTheme.bodyText1,
            ),
          ),
        ]);
  }
}

Copy this code to DartPad

What is wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初熏 2025-02-12 16:51:09

对我来说,这个代码...有效
我将其复制到Dartpad中,然后将DEV工具(F12)&GT中的开发工具复制到;设备仿真&GT;响应迅速。您可以使用拉动来刷新。
当然,这是使用Web视图和鼠标无法使用的。我相信不支持这种手势。

Well for me this code... works
I copied it into Dartpad, then Dev Tools in browser (F12) > Device Emulation > Responsive. And you can use pull to refresh.
Of course this doesn't work using web view and mouse. I believe this gesture is not supported.
enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文