1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Flutter] How to update FolderLabel after selecting a different folder

Discussão em 'Mobile' iniciado por Stack, Outubro 1, 2024 às 12:13.

  1. Stack

    Stack Membro Participativo

    I am running Flutter on Windows.

    I have a default folder with the default folder label. After I start the program, click a button, pick a different folder, then I need to see the folder label updated to reflect the new folder selected. Currently, this is not working.

    Here is my code:

    import 'package:flutter/material.dart';
    import 'package:file_picker/file_picker.dart';
    import 'package:provider/provider.dart';

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

    class MyApp extends StatelessWidget {
    const MyApp({Key? key}) : super(key: key);
    @override
    Widget build(BuildContext context) {
    return MaterialApp(title: 'Flutter Demo',theme: ThemeData(primarySwatch: Colors.blue),home: const MyHomePage(),);
    }
    }

    class MyHomePage extends StatefulWidget {
    const MyHomePage({Key? key}) : super(key: key);
    @override
    State<MyHomePage> createState() => MyHomePageState();
    }

    class MyHomePageState extends State<MyHomePage> with ChangeNotifier {
    String? myDirectory = 'C:/Users/Home/Desktop';

    Future<String> getFolder() async {
    String? myfolder='';
    myfolder == await FilePicker.platform.getDirectoryPath();
    return myfolder;
    }

    String? updateFolderLabel() {
    String abc = '';
    getFolder().then((result){
    abc = result;
    });
    notifyListeners();
    return abc;
    }

    String? getFolderLabel() {
    if (updateFolderLabel()!.isEmpty) return myDirectory;
    if (updateFolderLabel()!.isNotEmpty) return updateFolderLabel();
    }

    @override
    Widget build(BuildContext context) {
    Widget myfolderLabel = Text(getFolderLabel()!);
    Widget myButton = SizedBox(width: 200,height: 80,child: Row(mainAxisAlignment: MainAxisAlignment.center,mainAxisSize: MainAxisSize.min,children: [
    Expanded(child: TextButton(onPressed: updateFolderLabel,style: TextButton.styleFrom(backgroundColor: Colors.blue,side: BorderSide(width: 1.0)),child: Text("Open Folder",style: TextStyle(color: Colors.white))))]));
    return MaterialApp(home: Scaffold(appBar: AppBar(title: Text('Hello Flutter')),resizeToAvoidBottomInset: true,
    body: LayoutBuilder(builder: (BuildContext ctx, BoxConstraints constraints){
    return Container(child: SingleChildScrollView(child: Column(mainAxisAlignment: MainAxisAlignment.spaceEvenly, mainAxisSize: MainAxisSize.min, children: <Widget>[
    myfolderLabel,
    myButton,
    ])));
    })
    ));
    }
    }

    Continue reading...

Compartilhe esta Página