flutter 我想让斐波那契数列无穷大

dl5txlt9  于 2023-01-02  发布在  Flutter
关注(0)|答案(2)|浏览(196)

hello guys这个程序在列表视图中显示Fibonacci系列,但它只在Fibonacci系列中显示前两个数字1 - 1该数字等于最后两个数字的总和示例5等于3+2和8等于3+5我想程序显示无限Fibonacci系列我该怎么做才能使列表无限Fibonacci系列?有代码知道只在列表中显示1-1的原因

import 'package:flutter/material.dart';

void main() async {
  final numbers = FibonacciNumbers();

  runApp(
    MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        appBar: AppBar(
          title: Text('Fibonacci List'),
        ),
        body: FibonacciListView(numbers),
      ),
    ),
  );
}

class FibonacciNumbers {
  final cache = {0: BigInt.from(1), 1: BigInt.from(1)};
  
  BigInt get(int i) {
    if (!cache.containsKey(i)) {
      cache[i] = get(i - 1) + get(i - 2);
    }

    return cache[i]!;
  }
}

class FibonacciListView extends StatelessWidget {
  final FibonacciNumbers numbers;
  const FibonacciListView(this.numbers, {super.key});
  
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.black,
        title: Text('Fibonacci List'),
      ),
      body: ListView.builder(
        itemCount: numbers.cache.length,
        itemBuilder: (context, i) {
          return ListTile(
            title: Text('${numbers.get(i).toString()}'),
            onTap: () {
              final snack = SnackBar(
                content: Text('${numbers.get(i)} is #$i in the Fibonacci sequence!'),
              );
              ScaffoldMessenger.of(context).showSnackBar(snack);
            },
          );
        },
      ),
    );
  }
}

显示斐波那契数列0,1,1,2,3,5,8,13等等。

1bqhqjot

1bqhqjot1#

你的代码唯一的问题是你提供了列表的长度,即numbers.cache.length。正如@mmcdon20在评论中提到的,如果你删除它,你的代码将运行得很好。
在你的解决方案中还有另一个问题,snackbar消息应该考虑列表索引从0开始,而真实的世界的序列从索引1开始。
修改了你的代码

import 'package:flutter/material.dart';

void main(args) {
  WidgetsFlutterBinding.ensureInitialized();
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData.dark(useMaterial3: true),
      home: const FibonacciSequencePage(),
    );
  }
}

class FibonacciNumbers {
  FibonacciNumbers();

  final cache = {0: BigInt.from(1), 1: BigInt.from(1)};

  BigInt get(int i) {
    if (!cache.containsKey(i)) {
      return cache[i] = get(i - 1) + get(i - 2);
    }

    return cache[i]!;
  }
}

class FibonacciSequencePage extends StatelessWidget {
  const FibonacciSequencePage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Fibonacci Sequence'),
      ),
      body: FibonacciSequenceListView(numbers: FibonacciNumbers()),
    );
  }
}

class FibonacciSequenceListView extends StatelessWidget {
  final FibonacciNumbers numbers;

  const FibonacciSequenceListView({super.key, required this.numbers});

  @override
  Widget build(BuildContext context) {
    return ListView.builder(itemBuilder: (context, index) {
      return ListTile(
        title: Text(numbers.get(index).toString()),
        onTap: () {
          final snack = SnackBar(
            content: Text(
                '${numbers.get(index)} is #${index + 1} in the Fibonacci sequence!'),
          );
          ScaffoldMessenger.of(context).showSnackBar(snack);
        },
      );
    });
  }
}
eanckbw9

eanckbw92#

你需要编辑很多东西。我编辑了你的代码。这是100%的工作解决方案。

import 'package:flutter/material.dart';

void main() async {
  final numbers = FibonacciNumbers();

  runApp(
    MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        appBar: AppBar(
          backgroundColor: Colors.black,
          title: const Text('Fibonacci List'),
        ),
        body: FibonacciListView(numbers),
      ),
    ),
  );
}

class FibonacciNumbers {
  final fibList = [BigInt.from(1), BigInt.from(1)];

  void addElementsToList(int n) {
    print("elements are added");
    for (int i = 0; i < n; i++) {
      fibList.add(
          fibList.last + fibList[fibList.length - 2]);
    }
  }
}

class FibonacciListView extends StatefulWidget {
  final FibonacciNumbers numbers;
  const FibonacciListView(this.numbers, {super.key});

  @override
  State<FibonacciListView> createState() => _FibonacciListViewState();
}

class _FibonacciListViewState extends State<FibonacciListView> {
  final scrollController = ScrollController();

  @override
  void initState() {
    super.initState();
    widget.numbers.addElementsToList(20);
    scrollController.addListener(_listenerMethod);
  }

  void _listenerMethod() {
    if (scrollController.position.maxScrollExtent -
            scrollController.position.pixels <=
        100) {
      setState(() {
        widget.numbers.addElementsToList(20);
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
      itemCount: widget.numbers.fibList.length,
      controller: scrollController,
      itemBuilder: (context, i) {
        return ListTile(
          title: Text('${widget.numbers.fibList[i]}'),
          onTap: () {
            final snack = SnackBar(
              content: Text('${widget.numbers.fibList[i]} is #$i in the Fibonacci sequence!'),
            );
            ScaffoldMessenger.of(context).showSnackBar(snack);
          },
        );
      },
    );
  }
}

顺便说一句,我删除了一个应用程序栏和一个脚手架对象。因为你把脚手架放在另一个脚手架里面在你的代码有问题。

相关问题