Giter VIP home page Giter VIP logo

Comments (10)

diegoveloper avatar diegoveloper commented on August 16, 2024

from flutter_keyboard_actions.

gary-c-chang avatar gary-c-chang commented on August 16, 2024

My code is structured the same way as the example code as per 2.1.2 https://pub.dev/packages/keyboard_actions/versions/2.1.2

In particular FormKeyboardActions widget is built in a separate class to where KeyboardActionsConfig is built, because my keyboard fields dynamically change depending on which type of form is being presented to the user. The example code for 2.1.2 gets around this by providing the static method FormKeyboardActions.setKeyboardActions(), which will update the previously built FormKeyboardActionsState with the update config. This is what I use to update the config. I'd just like this method to be put back. I finally got 3.1.2 working by copying the code inside FormKeyboardActions.setKeyboardActions() and putting it into my code (after renaming FormXX to XX).

The source code for 3.1.2 keyboard_actions.dart, line 70 sort of refers to this, but it seems incomplete:
/// Can be accessed statically via [] and [] to update with the latest and greatest [KeyboardActionsConfig].

3.1.2 as it currently stands doesn't allow for easy update of the config, whereas 2.1.2 did. 3.1.2 also forces the caller to define the config as soon as the KeyboardActions is created in the widget tree, whereas 2.1.2 allow the config to be defined later.

from flutter_keyboard_actions.

diegoveloper avatar diegoveloper commented on August 16, 2024

from flutter_keyboard_actions.

gary-c-chang avatar gary-c-chang commented on August 16, 2024

That's not true, as when I build KeyboardActions, I do NOT have my config yet (see example 2.1.2 code - they are in separate classes) so my outer class does not know what keyboard actions to display yet. I need KeyboardActions at the outer level because putting it in the lower level causes sizing and layout issues. Anyway, don't bother as I have found a workaround as mentioned. Your suggestion forces the caller to build the KeyboardActions widget at the same time as the config which is not always possible for more complex use cases.

from flutter_keyboard_actions.

zmGitHub avatar zmGitHub commented on August 16, 2024

+1

from flutter_keyboard_actions.

diegoveloper avatar diegoveloper commented on August 16, 2024

from flutter_keyboard_actions.

gary-c-chang avatar gary-c-chang commented on August 16, 2024

`
// Here is the 2.1.2 example that my actual code follows in terms of class separation.
// I have modified it to work with 3.1.2. See my comments starting GCC
// Thanks Diego

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

//Full screen
class ScaffoldTest extends StatelessWidget {
@OverRide
Widget build(BuildContext context) {
return Scaffold(
resizeToAvoidBottomInset: true,
appBar: AppBar(
title: Text("Keyboard Actions Sample"),
),
body: KeyboardActions(
config: KeyboardActionsConfig(actions: []), // GCC this was not needed in 2.1.2
child: Content(),
),
);
}
}

//Dialog

/// Displays our [FormKeyboardActions] nested in a [AlertDialog].
class DialogTest extends StatelessWidget {
@OverRide
Widget build(BuildContext context) {
return Scaffold(
resizeToAvoidBottomInset: true,
appBar: AppBar(
title: Text("Keyboard Actions Sample"),
),
body: Builder(builder: (context) {
return Center(
child: FlatButton(
color: Colors.blue,
child: Text('Launch dialog'),
onPressed: () => _launchInDialog(context),
),
);
}));
}

_launchInDialog(BuildContext context) {
showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: Text('Dialog test'),
content: KeyboardActions(autoScroll: true, child: Content()),
actions: [
FlatButton(
child: Text('Ok'),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
},
);
}
}

class Content extends StatefulWidget {
@OverRide
_ContentState createState() => _ContentState();
}

class _ContentState extends State {
final FocusNode _nodeText1 = FocusNode();
final FocusNode _nodeText2 = FocusNode();
final FocusNode _nodeText3 = FocusNode();
final FocusNode _nodeText4 = FocusNode();
final FocusNode _nodeText5 = FocusNode();

/// Creates the [KeyboardActionsConfig] to hook up the fields
/// and their focus nodes to our [FormKeyboardActions].
KeyboardActionsConfig _buildConfig(BuildContext context) {
return KeyboardActionsConfig(
keyboardActionsPlatform: KeyboardActionsPlatform.ALL,
keyboardBarColor: Colors.grey[200],
nextFocus: true,
actions: [
KeyboardAction(
focusNode: _nodeText1,
),
KeyboardAction(
focusNode: _nodeText2,
closeWidget: Padding(
padding: EdgeInsets.all(8.0),
child: Icon(Icons.close),
),
),
KeyboardAction(
focusNode: _nodeText3,
onTapAction: () {
showDialog(
context: context,
builder: (context) {
return AlertDialog(
content: Text("Custom Action"),
actions: [
FlatButton(
child: Text("OK"),
onPressed: () => Navigator.of(context).pop(),
)
],
);
});
},
),
KeyboardAction(
focusNode: _nodeText4,
displayCloseWidget: false,
),
KeyboardAction(
focusNode: _nodeText5,
closeWidget: Padding(
padding: EdgeInsets.all(5.0),
child: Text("CLOSE"),
),
),
],
);
}

@OverRide
void initState() {
// Configure keyboard actions

/**********
 * GCC Ideally the 3.1.2 library would provide this static method that was available in version 2.1.2
 KeyboardActions.setKeyboardActions(context, _buildConfig(context));
 */

// This is my workaround - just copied and slightly modified from the body of 2.1.2's FormKeyboardActions.setKeyboardActions:

final KeyboardActionstate state = context.findAncestorStateOfType<KeyboardActionstate>();

if (state == null) {
  throw FlutterError(
      'Context does not contain a FormKeyboardActions ancestor: see Scaffold.of for reference.');
}
state.setConfig(_buildConfig(context));
super.initState();

}

@OverRide
Widget build(BuildContext context) {
return Center(
child: Padding(
padding: const EdgeInsets.all(15.0),
child: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
TextField(
keyboardType: TextInputType.number,
focusNode: _nodeText1,
decoration: InputDecoration(
hintText: "Input Number",
),
),
TextField(
keyboardType: TextInputType.text,
focusNode: _nodeText2,
decoration: InputDecoration(
hintText: "Input Text with Custom Close Widget",
),
),
TextField(
keyboardType: TextInputType.number,
focusNode: _nodeText3,
decoration: InputDecoration(
hintText: "Input Number with Custom Action",
),
),
TextField(
keyboardType: TextInputType.text,
focusNode: _nodeText4,
decoration: InputDecoration(
hintText: "Input Text without Close Widget",
),
),
TextField(
keyboardType: TextInputType.number,
focusNode: _nodeText5,
decoration: InputDecoration(
hintText: "Input Number with Custom Close Widget",
),
),
],
),
),
),
);
}
}
`

from flutter_keyboard_actions.

diegoveloper avatar diegoveloper commented on August 16, 2024

Why don't you remove the keyboard actions from here:

return Scaffold(
      resizeToAvoidBottomInset: true,
      appBar: AppBar(
        title: Text("Keyboard Actions Sample"),
      ),
      body:  Content(),
    );

and add it inside your Content widget.

  @override
  Widget build(BuildContext context) {
    return KeyboardActions(
      config: _buildConfig(context),
      child: Center(
        child: Padding(
          padding: const EdgeInsets.all(15.0),
          child: SingleChildScrollView(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.stretch,
              children: [
                TextField(
                  keyboardType: TextInputType.number,
                  focusNode: _nodeText1,
                  decoration: InputDecoration(
                    hintText: "Input Number",
                  ),
                ),

from flutter_keyboard_actions.

gary-c-chang avatar gary-c-chang commented on August 16, 2024

As I've mentioned a few times in the thread above, I cannot do that as KeyboardActions widget at the lower level causes layout and placement issues / crashes. I've tried many different configuration of widgets (but either my form gets centered on screen rather than top aligned etc). As I've also mentioned, your solution needs separation between building the widget, and the configuration changing to handle dynamic sets of input fields.

from flutter_keyboard_actions.

diegoveloper avatar diegoveloper commented on August 16, 2024

from flutter_keyboard_actions.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.