导航和颤振状态面临问题

时间:2020-07-15 06:16:37

标签: flutter dart bloc

我是新手。 我正在尝试创建一个基于flutter的应用,该应用可以浏览android设备中的文件。

当我尝试从导航页面返回时,这里遇到了问题。 另外我正在设置状态“正在加载”,该状态不会得到反映。

添加代码

main.dart

import 'package:permission_handler/permission_handler.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'bloc/execute_bloc.dart';
import 'package:flutter/material.dart';

import 'dirLister2.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  Map<Permission, PermissionStatus> statuses = await [
    Permission.location,
    Permission.storage,
  ].request();
  print(statuses[Permission.storage]);
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'Flutter Demo',
        theme: ThemeData.dark(),
        home: Scaffold(
          body: BlocProvider(
            create: (context) => ExecuteBloc(),
            child: MyHomePage(),
          ),
        ));
  }
}

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Container(
          child: Expanded(
            child: Center(
              child: RaisedButton(
                  child: Text('SDCard'),
                  onPressed: () => Navigator.of(context).push(
                        MaterialPageRoute(
                          builder: (context) =>
                              ASDAD(data: '/storage/7CAC-1214/'),
                        ),
                      )),
            ),
          ),
        )
      ],
    );
  }
}

dirLister.dart

import 'package:flutter_bloc/flutter_bloc.dart';
import 'bloc/execute_bloc.dart';
import 'package:flutter/material.dart';
import 'dirLister2.dart';

class DirLister extends StatelessWidget {
  final String path;
  DirLister(this.path);
  @override
  Widget build(BuildContext context) {
    context.bloc<ExecuteBloc>().add(DirClick(path));
    return BlocBuilder<ExecuteBloc, ExecuteState>(builder: (context, state) {
      return ListView.builder(
          scrollDirection: Axis.vertical,
          itemCount: state.dirList.length,
          itemBuilder: (context, index) {
            String newPath = state.startPath + state.dirList[index] + '/';

            return GestureDetector(
                child: Container(
                  child: Row(
                    children: [
                      Icon(
                        Icons.folder,
                        size: 40,
                      ),
                      Flexible(
                        child: Text(
                          state.dirList[index],
                          style: TextStyle(fontSize: 20),
                        ),
                      ),
                    ],
                  ),
                  decoration: BoxDecoration(border: Border.all()),
                ),
                onTap: () {
                  context.bloc<ExecuteBloc>().add(
                      DirClick(state.startPath + state.dirList[index] + '/'));
                  Navigator.of(context).push(
                    MaterialPageRoute(
                      builder: (context) => ASDAD(data: newPath),
                    ),
                  );
                });
          });
    });
  }
}

dirLister2.dart

import 'package:flutter_bloc/flutter_bloc.dart';
import 'bloc/execute_bloc.dart';
import 'package:flutter/material.dart';
import 'dirLister.dart';

class ASDAD extends StatelessWidget {
  final String data;
  ASDAD({Key key, this.data}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: BlocProvider(
        create: (context) => ExecuteBloc(),
        child: DirLister(data),
      ),
    );
  }
}

因此,除此之外,我的代码中还有Bloc逻辑部分。 钙,你建议我的导航方式是否正确?

提前谢谢!

0 个答案:

没有答案