0

How can I implement AppTranslations widget with checkbox?, I want to select one of the language by click on the box on the right side of the listtile

I try use CheckBoxListTilie but without success :(

i want something like this:

enter image description here

code:

class _LanguageSelectorState extends State<LanguageSelector> {
  static final List<String> languagesList = application.supportedLanguages;
  static final List<String> languageCodesList =
      application.supportedLanguagesCodes;


  final Map<dynamic, dynamic> languagesMap = {
    languagesList[0]: languageCodesList[0],
    languagesList[1]: languageCodesList[1],
  };

  @override
  Widget build(BuildContext context) {
    return Scaffold(
          backgroundColor: Colors.white,
          appBar: AppBar(
            backgroundColor: Colors.white,
            iconTheme: IconThemeData(color: Colors.black),
            title: Text(AppTranslations.of(context).text("settings_language"), style: TextStyle(color: Colors.black, letterSpacing: 1)),
            elevation: 0.0,
            centerTitle: true,
            bottom: PreferredSize(child: Container(color: Colors.black, height: 0.1), preferredSize: Size.fromHeight(0.1),),
          ),
      body: _buildLanguagesList(),
    );
  }

  String selectedLanguage = '';

  _buildLanguagesList() {
    return ListView.builder(
      itemCount: languagesList.length,
      itemBuilder: (context, index) {
        return _buildLanguageItem(languagesList[index]);
      },
    );
  }

  _buildLanguageItem(String language) {
    // return ListTile(
    //   onTap: () {
    //     selectedLanguage = language;
    //     application.onLocaleChanged(Locale(languagesMap[language]));
    //   },
    //   title: Text(language),
    //   leading: Icon(Icons.flag),
    // );
    bool _value = false;
    return CheckboxListTile(
      title: Text(language),
      value: _value,
      onChanged: (value) {
        _value = value;
        application.onLocaleChanged(Locale(languagesMap[language]));
      },
      controlAffinity: ListTileControlAffinity.trailing,
    );
  }
}

Right now I have only simply centred list :(

thanks for any help

1 Answer 1

2

Just use CheckboxListTile, here is an example

CheckboxListTile(
        title: Text("This is the label text"),
        value: _value,
        onChanged: (newValue) {...},
        controlAffinity: ListTileControlAffinity.trailing,
      ),

And here is complete example using CheckboxListTile:

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Checkbox Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: CheckboxPage(),
    );
  }
}

class CheckboxPage extends StatefulWidget {
  @override
  _CheckboxPageState createState() => _CheckboxPageState();
}

class _CheckboxPageState extends State<CheckboxPage> {
  bool _value = false;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: CheckboxListTile(
          title: Text("This is the label text"),
          value: _value,
          onChanged: (newValue) {
            setState(() {
              _value = newValue;
            });
          },
          controlAffinity: ListTileControlAffinity.trailing,
        ),
      ),
    );
  }
}
Sign up to request clarification or add additional context in comments.

8 Comments

I tried to use it but I can not cope with the combination of my function to choose the right language, could you show in your comment how it should be done?
i edit my _buildLanguageItem function in main post, in this way my checkbocx does not work
_value = value; application.onLocaleChanged(Locale(languagesMap[language])); should be called from setState
look closely how I implemented onChange
I did, but my square is still empty when clicked
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.