Home > other >  Unable to put FormBuillderCheckbox inside Row widget
Unable to put FormBuillderCheckbox inside Row widget

Time:12-06

I am trying to add a T&C check box in a form (making use of flutter_form_builder package), and would like to allow the user to tap on the label to launch a url to display T&C. Within FormBuilder widget, I put a FormBuilderCheckbox and a InkWell inside a Row widget. Pages of runtime error. The checkbox displays normally if the Row widget is removed.

import 'package:flutter/material.dart';
import 'package:flutter_form_builder/flutter_form_builder.dart';
import 'package:url_launcher/url_launcher.dart';

class hauntedhouse extends StatefulWidget {
  //const ({Key? key}) : super(key: key);
  @override
  _hauntedhouseState createState() => _hauntedhouseState();
}

class _hauntedhouseState extends State<hauntedhouse> {
  final _formKey = GlobalKey<FormBuilderState>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Hanunted House'),
        centerTitle: true,
        backgroundColor: Colors.blue[400],
      ),
      body: FormBuilder(
        key: _formKey,
        onChanged: () {
          _formKey.currentState!.save();
          debugPrint(_formKey.currentState!.value.toString());
        },
        autovalidateMode: AutovalidateMode.disabled,
        skipDisabled: true,
        child: Row(
          children: [
            FormBuilderCheckbox(
              name: 't_and_c',
              title: Text('term and conditions'),
            ),
            InkWell(
              child: Text('terms and conditions'),
              onTap: () async => launchUrl(Uri.parse('https://www.youtube.com/')),
            ),
          ],
        ),
      ),
    );
  }
}

CodePudding user response:

Wrap FormBuilderCheckbox widget with Expanded

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

  @override
  State createState() => _hauntedhouseState();
}

class _hauntedhouseState extends State<hauntedhouse> {
  final _formKey = GlobalKey<FormBuilderState>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Hanunted House'),
        centerTitle: true,
        backgroundColor: Colors.blue[400],
      ),
      body: FormBuilder(
        key: _formKey,
        onChanged: () {
          _formKey.currentState!.save();
          debugPrint(_formKey.currentState!.value.toString());
        },
        autovalidateMode: AutovalidateMode.disabled,
        skipDisabled: true,
        child: Row(
          children: [
            Expanded(
              child: FormBuilderCheckbox(
                name: 't_and_c',
                title: Text('term and conditions'),
              ),
            ),
            InkWell(
              child: Text('terms and conditions'),
              onTap: () async =>
                  launchUrl(Uri.parse('https://www.youtube.com/')),
            ),
          ],
        ),
      ),
    );
  }
}
  • Related