mirror of
https://github.com/flutter/samples.git
synced 2025-11-08 13:58:47 +00:00
[linting_tool] Implement saving rules to DB (#860)
This commit is contained in:
committed by
GitHub
parent
1818925286
commit
bbb8e342f1
@@ -91,7 +91,19 @@ class _NavViewState extends State<_NavView> {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
var textTheme = Theme.of(context).textTheme;
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text(
|
||||
'Flutter Linting Tool',
|
||||
style: textTheme.subtitle2!.copyWith(
|
||||
color: textTheme.bodyText1!.color,
|
||||
),
|
||||
),
|
||||
toolbarHeight: 38.0,
|
||||
backgroundColor: Colors.white,
|
||||
brightness: Brightness.light,
|
||||
),
|
||||
body: Row(
|
||||
children: [
|
||||
LayoutBuilder(
|
||||
|
||||
@@ -4,9 +4,12 @@
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_markdown/flutter_markdown.dart';
|
||||
import 'package:linting_tool/model/profile.dart';
|
||||
import 'package:linting_tool/model/profiles_store.dart';
|
||||
import 'package:linting_tool/model/rule.dart';
|
||||
import 'package:linting_tool/theme/app_theme.dart';
|
||||
import 'package:linting_tool/theme/colors.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
class LintExpansionTile extends StatefulWidget {
|
||||
final Rule rule;
|
||||
@@ -127,8 +130,30 @@ class _LintExpansionTileState extends State<LintExpansionTile> {
|
||||
alignment: Alignment.centerRight,
|
||||
child: ElevatedButton(
|
||||
child: const Text('Add to profile'),
|
||||
onPressed: () {
|
||||
// TODO(abd99): Iplement adding to a profile.
|
||||
onPressed: () async {
|
||||
ProfileType? destinationProfileType =
|
||||
await showDialog<ProfileType>(
|
||||
context: context,
|
||||
builder: (context) {
|
||||
return const _ProfileTypeDialog();
|
||||
},
|
||||
);
|
||||
if (destinationProfileType == ProfileType.newProfile) {
|
||||
showDialog<String>(
|
||||
context: context,
|
||||
builder: (context) {
|
||||
return _NewProfileDialog(rule: rule);
|
||||
},
|
||||
);
|
||||
} else if (destinationProfileType ==
|
||||
ProfileType.existingProfile) {
|
||||
showDialog<String>(
|
||||
context: context,
|
||||
builder: (context) {
|
||||
return _ExistingProfileDialog(rule: rule);
|
||||
},
|
||||
);
|
||||
}
|
||||
},
|
||||
),
|
||||
),
|
||||
@@ -139,3 +164,145 @@ class _LintExpansionTileState extends State<LintExpansionTile> {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
enum ProfileType {
|
||||
newProfile,
|
||||
existingProfile,
|
||||
}
|
||||
|
||||
class _ProfileTypeDialog extends StatelessWidget {
|
||||
const _ProfileTypeDialog({
|
||||
Key? key,
|
||||
}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return AlertDialog(
|
||||
actionsPadding: const EdgeInsets.only(
|
||||
left: 16.0,
|
||||
right: 16.0,
|
||||
bottom: 16.0,
|
||||
),
|
||||
title: const Text('Select Profile Type'),
|
||||
actions: [
|
||||
ElevatedButton(
|
||||
onPressed: () {
|
||||
Navigator.pop(context, ProfileType.existingProfile);
|
||||
},
|
||||
child: const Text('Existing Profile'),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () {
|
||||
Navigator.pop(context, ProfileType.newProfile);
|
||||
},
|
||||
child: const Text('Create new profile'),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _NewProfileDialog extends StatelessWidget {
|
||||
final Rule rule;
|
||||
const _NewProfileDialog({
|
||||
required this.rule,
|
||||
Key? key,
|
||||
}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
String name = '';
|
||||
final _formKey = GlobalKey<FormState>();
|
||||
|
||||
return AlertDialog(
|
||||
title: const Text('Create new lint profile'),
|
||||
content: Form(
|
||||
key: _formKey,
|
||||
autovalidateMode: AutovalidateMode.onUserInteraction,
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const Text('Profile Name'),
|
||||
TextFormField(
|
||||
onChanged: (value) {
|
||||
name = value;
|
||||
},
|
||||
validator: (value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
return 'Name cannot be empty.';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
actionsPadding: const EdgeInsets.all(16.0),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () {
|
||||
Navigator.pop(context);
|
||||
},
|
||||
child: const Text('Cancel'),
|
||||
),
|
||||
ElevatedButton(
|
||||
onPressed: () async {
|
||||
if (_formKey.currentState!.validate()) {
|
||||
var newProfile = RulesProfile(
|
||||
name: name,
|
||||
rules: [rule],
|
||||
);
|
||||
await Provider.of<ProfilesStore>(context, listen: false)
|
||||
.addToNewProfile(newProfile);
|
||||
Navigator.pop(context);
|
||||
}
|
||||
},
|
||||
child: const Text('Save'),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _ExistingProfileDialog extends StatelessWidget {
|
||||
const _ExistingProfileDialog({
|
||||
Key? key,
|
||||
required this.rule,
|
||||
}) : super(key: key);
|
||||
|
||||
final Rule rule;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
var profilesStore = Provider.of<ProfilesStore>(context);
|
||||
var savedProfiles = profilesStore.savedProfiles;
|
||||
return AlertDialog(
|
||||
title: const Text('Select a lint profile'),
|
||||
content: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: List.generate(
|
||||
savedProfiles.length,
|
||||
(index) => ListTile(
|
||||
title: Text(savedProfiles[index].name),
|
||||
onTap: () async {
|
||||
await profilesStore.addToExistingProfile(
|
||||
savedProfiles[index], rule);
|
||||
Navigator.pop(context);
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
actionsPadding: const EdgeInsets.all(16.0),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () {
|
||||
Navigator.pop(context);
|
||||
},
|
||||
child: const Text('Cancel'),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
132
experimental/linting_tool/lib/widgets/saved_rule_tile.dart
Normal file
132
experimental/linting_tool/lib/widgets/saved_rule_tile.dart
Normal file
@@ -0,0 +1,132 @@
|
||||
// Copyright 2021 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_markdown/flutter_markdown.dart';
|
||||
import 'package:linting_tool/model/rule.dart';
|
||||
import 'package:linting_tool/theme/app_theme.dart';
|
||||
import 'package:linting_tool/theme/colors.dart';
|
||||
|
||||
class SavedRuleTile extends StatefulWidget {
|
||||
final Rule rule;
|
||||
const SavedRuleTile({
|
||||
required this.rule,
|
||||
Key? key,
|
||||
}) : super(key: key);
|
||||
|
||||
@override
|
||||
_SavedRuleTileState createState() => _SavedRuleTileState();
|
||||
}
|
||||
|
||||
class _SavedRuleTileState extends State<SavedRuleTile> {
|
||||
var isExpanded = false;
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
var theme = Theme.of(context);
|
||||
var textTheme = theme.textTheme;
|
||||
final rule = widget.rule;
|
||||
final incompatibleString =
|
||||
rule.incompatible.isNotEmpty ? rule.incompatible.join(', ') : 'none';
|
||||
final setsString = rule.sets.isNotEmpty ? rule.sets.join(', ') : 'none';
|
||||
|
||||
// TODO(abd99): Add option to remove rule from profile.
|
||||
// TODO(abd99): Add right click functionality.
|
||||
return ExpansionTile(
|
||||
collapsedBackgroundColor: AppColors.white50,
|
||||
title: Text(
|
||||
rule.name,
|
||||
style: textTheme.subtitle1!.copyWith(
|
||||
fontWeight: FontWeight.w700,
|
||||
),
|
||||
),
|
||||
subtitle: Text(
|
||||
rule.description,
|
||||
style: textTheme.caption!,
|
||||
),
|
||||
initiallyExpanded: isExpanded,
|
||||
onExpansionChanged: (value) {
|
||||
setState(() {
|
||||
isExpanded = value;
|
||||
});
|
||||
},
|
||||
expandedAlignment: Alignment.centerLeft,
|
||||
childrenPadding: const EdgeInsets.symmetric(
|
||||
horizontal: 16.0,
|
||||
vertical: 8.0,
|
||||
),
|
||||
backgroundColor: AppColors.white50,
|
||||
maintainState: true,
|
||||
expandedCrossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text.rich(
|
||||
TextSpan(
|
||||
children: [
|
||||
TextSpan(
|
||||
text: 'Group:',
|
||||
style: textTheme.subtitle2,
|
||||
),
|
||||
TextSpan(
|
||||
text: ' ${rule.group}',
|
||||
),
|
||||
],
|
||||
),
|
||||
textAlign: TextAlign.left,
|
||||
),
|
||||
Text.rich(
|
||||
TextSpan(
|
||||
children: [
|
||||
TextSpan(
|
||||
text: 'Maturity:',
|
||||
style: textTheme.subtitle2,
|
||||
),
|
||||
TextSpan(
|
||||
text: ' ${rule.maturity}',
|
||||
),
|
||||
],
|
||||
),
|
||||
textAlign: TextAlign.left,
|
||||
),
|
||||
Text.rich(
|
||||
TextSpan(
|
||||
children: [
|
||||
TextSpan(
|
||||
text: 'Incompatible:',
|
||||
style: textTheme.subtitle2,
|
||||
),
|
||||
TextSpan(
|
||||
text: ' $incompatibleString',
|
||||
),
|
||||
],
|
||||
),
|
||||
textAlign: TextAlign.left,
|
||||
),
|
||||
Text.rich(
|
||||
TextSpan(
|
||||
children: [
|
||||
TextSpan(
|
||||
text: 'Sets:',
|
||||
style: textTheme.subtitle2,
|
||||
),
|
||||
TextSpan(
|
||||
text: ' $setsString',
|
||||
),
|
||||
],
|
||||
),
|
||||
textAlign: TextAlign.left,
|
||||
),
|
||||
const SizedBox(
|
||||
height: 16.0,
|
||||
),
|
||||
MarkdownBody(
|
||||
data: rule.details,
|
||||
selectable: true,
|
||||
styleSheet: AppTheme.buildMarkDownTheme(theme),
|
||||
),
|
||||
const SizedBox(
|
||||
height: 16.0,
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user